index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {
  2. getUrlParam,
  3. getWeixinRedirectURI,
  4. isWeiXin
  5. } from '@/utils'
  6. import * as API_WeiXin from '@/apis/weixin.js'
  7. import carhelp from '@/utils/mixin.js'
  8. import Vue from 'vue'
  9. import uniCrazyRouter from "uni-crazy-router";
  10. Vue.use(uniCrazyRouter)
  11. //** 有bug, 第一次直接敲链接访问, 是访问bobo-router 前置守卫
  12. //** 后续操作,访问的是 uniCrazyRouter的前置守卫
  13. uniCrazyRouter.beforeEach(async (to, from, next) => {
  14. // 逻辑代码
  15. console.log("beforeEach")
  16. routerBeforeEach(to, from, next);
  17. })
  18. uniCrazyRouter.afterEach((to, from) => {
  19. // 逻辑代码
  20. console.log("afterEach")
  21. if(process.car.NODE_ENV=='dev'||process.car.NODE_ENV=='test'){
  22. uni.setNavigationBarTitle({
  23. title:'车信达('+process.car.NODE_ENV+')',
  24. })
  25. }
  26. })
  27. uniCrazyRouter['on' + 'Error']((to, from) => {
  28. // 逻辑代码
  29. console.log("Error")
  30. })
  31. import Router from './bobo-router'
  32. Vue.use(Router)
  33. // 路由配置 页面中全部使用this.$Router来操作路由,以实现路由的全局管理
  34. const router = new Router()
  35. // 路由全局拦截器 在这里处理登录、授权等相关操作
  36. router.beforeEach(function(to, from, next) {
  37. console.log('前置守卫')
  38. if (process.car.SIMPLE_RUN) {
  39. if (!carhelp.getOpenId()) {
  40. carhelp.setOpenId("test")
  41. }
  42. routerBeforeEach(to, from, next);
  43. } else {
  44. if (!carhelp.getOpenId()) {
  45. if (isWeiXin()) {
  46. getOpenId();
  47. }
  48. } else {
  49. const code = getUrlParam('code');
  50. if(code){
  51. var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  52. window.location = linkUrl; //隐藏参数
  53. }
  54. routerBeforeEach(to, from, next);
  55. }
  56. }
  57. })
  58. function getOpenId() {
  59. const code = getUrlParam('code');
  60. if (!code) {
  61. var url = document.URL;
  62. window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  63. } else {
  64. API_WeiXin.getDataByCode(code).then(response => {
  65. carhelp.setOpenId(response.data.openid)
  66. var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  67. window.location = linkUrl; //隐藏参数
  68. //return Promise.resolve(response.openid);
  69. }).catch(error => {
  70. console.log(error);
  71. });
  72. }
  73. }
  74. function routerBeforeEach(to, from, next) {
  75. next();
  76. }
  77. // 路由后置拦截器
  78. router.afterEach(function(to, from) {
  79. console.log('后置守卫')
  80. })
  81. // 路由跳转出错处理
  82. router.onError(function(e) {
  83. console.log('错误:', e.message || '路由跳转失败')
  84. })
  85. export default router