index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if(to.url==from.url){
  17. var url="pages/index/index";
  18. if(to.url.indexOf("pages/my")==0){
  19. url="pages/my/index";
  20. }
  21. if(to.url.indexOf("pages/news")==0){
  22. url="pages/news/index";
  23. }
  24. if(to.url.indexOf("pages/remind")==0){
  25. url="pages/remind/index";
  26. }
  27. window.location.href=window.location.href.split('#')[0]+"#/"+url
  28. }else{
  29. routerBeforeEach(to, from, next);
  30. }
  31. })
  32. uniCrazyRouter.afterEach((to, from) => {
  33. // 逻辑代码
  34. console.log("afterEach")
  35. if(process.car.NODE_ENV=='dev'||process.car.NODE_ENV=='test'){
  36. uni.setNavigationBarTitle({
  37. title:'东胜掌上通勤('+process.car.NODE_ENV+')',
  38. })
  39. }
  40. })
  41. uniCrazyRouter['on' + 'Error']((to, from) => {
  42. // 逻辑代码
  43. console.log("Error")
  44. })
  45. import Router from './bobo-router'
  46. Vue.use(Router)
  47. // 路由配置 页面中全部使用this.$Router来操作路由,以实现路由的全局管理
  48. const router = new Router()
  49. // 路由全局拦截器 在这里处理登录、授权等相关操作
  50. router.beforeEach(function(to, from, next) {
  51. console.log('前置守卫')
  52. if (process.car.SIMPLE_RUN) {
  53. if (!carhelp.getOpenId()) {
  54. carhelp.setOpenId("test")
  55. }
  56. routerBeforeEach(to, from, next);
  57. } else {
  58. // if (!carhelp.getOpenId()) {
  59. // if (isWeiXin()) {
  60. // getOpenId();
  61. // }
  62. // } else {
  63. // }
  64. routerBeforeEach(to, from, next);
  65. }
  66. })
  67. function getOpenId() {
  68. const code = getUrlParam('code');
  69. if (!code) {
  70. var url = document.URL;
  71. window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  72. } else {
  73. API_WeiXin.getDataByCode(code).then(response => {
  74. carhelp.setOpenId(response.data.openid)
  75. //var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  76. //window.location = linkUrl; //隐藏参数
  77. //return Promise.resolve(response.openid);
  78. }).catch(error => {
  79. console.log(error);
  80. });
  81. }
  82. }
  83. function routerBeforeEach(to, from, next) {
  84. next();
  85. }
  86. // 路由后置拦截器
  87. router.afterEach(function(to, from) {
  88. console.log('后置守卫')
  89. })
  90. // 路由跳转出错处理
  91. router.onError(function(e) {
  92. console.log('错误:', e.message || '路由跳转失败')
  93. })
  94. export default router