index.js 3.0 KB

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