index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. getOpenId();
  58. // if (isWeiXin()) {
  59. // }else{
  60. // alert("请使用微信登录")
  61. // }
  62. } else {
  63. routerBeforeEach(to, from, next);
  64. }
  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. var openId=response.data.openid;
  75. carhelp.setOpenId(response.data.openid)
  76. //var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
  77. //window.location = linkUrl; //隐藏参数
  78. //return Promise.resolve(response.openid);
  79. API_user.findByOpenId({
  80. openId:openId
  81. }).then(response2 => {
  82. if(response.data){
  83. var token = response2.data ? response2.data.token : '';
  84. carhelp.setToken(token);
  85. carhelp.setPersonInfo(response2.data.token)
  86. uni.switchTab({
  87. url: '/pages/index/index'
  88. });
  89. }
  90. }).catch(error => {
  91. console.log(error);
  92. })
  93. }).catch(error => {
  94. console.log(error);
  95. });
  96. }
  97. }
  98. function routerBeforeEach(to, from, next) {
  99. next();
  100. }
  101. // 路由后置拦截器
  102. router.afterEach(function(to, from) {
  103. console.log('后置守卫')
  104. })
  105. // 路由跳转出错处理
  106. router.onError(function(e) {
  107. console.log('错误:', e.message || '路由跳转失败')
  108. })
  109. export default router