index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. routerBeforeEach(to, from, next);
  40. } else {
  41. if (!carhelp.getOpenId()) {
  42. if (isWeiXin()) {
  43. getOpenId();
  44. }
  45. } else {
  46. const code = getUrlParam('code');
  47. if(code){
  48. var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  49. window.location = linkUrl; //隐藏参数
  50. }
  51. routerBeforeEach(to, from, next);
  52. }
  53. }
  54. })
  55. function getOpenId() {
  56. const code = getUrlParam('code');
  57. if (!code) {
  58. var url = document.URL;
  59. window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  60. } else {
  61. API_WeiXin.getDataByCode(code).then(response => {
  62. carhelp.setOpenId(response.data.openid)
  63. var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  64. window.location = linkUrl; //隐藏参数
  65. //return Promise.resolve(response.openid);
  66. }).catch(error => {
  67. console.log(error);
  68. });
  69. }
  70. }
  71. function routerBeforeEach(to, from, next) {
  72. next();
  73. }
  74. // 路由后置拦截器
  75. router.afterEach(function(to, from) {
  76. console.log('后置守卫')
  77. })
  78. // 路由跳转出错处理
  79. router.onError(function(e) {
  80. console.log('错误:', e.message || '路由跳转失败')
  81. })
  82. export default router