import { getUrlParam, getWeixinRedirectURI, isWeiXin } from '@/utils' import * as API_WeiXin from '@/apis/weixin.js' import carhelp from '@/utils/mixin.js' import Vue from 'vue' import uniCrazyRouter from "uni-crazy-router"; Vue.use(uniCrazyRouter) //** 有bug, 第一次直接敲链接访问, 是访问bobo-router 前置守卫 //** 后续操作,访问的是 uniCrazyRouter的前置守卫 uniCrazyRouter.beforeEach(async (to, from, next) => { // 逻辑代码 console.log("beforeEach") if(to.url==from.url){ var url="pages/index/index"; if(to.url.indexOf("pages/my")==0){ url="pages/my/index"; } if(to.url.indexOf("pages/news")==0){ url="pages/news/index"; } if(to.url.indexOf("pages/remind")==0){ url="pages/remind/index"; } window.location.href=window.location.href.split('#')[0]+"#/"+url }else{ routerBeforeEach(to, from, next); } }) uniCrazyRouter.afterEach((to, from) => { // 逻辑代码 console.log("afterEach") if(process.car.NODE_ENV=='dev'||process.car.NODE_ENV=='test'){ uni.setNavigationBarTitle({ title:'车信达('+process.car.NODE_ENV+')', }) } }) uniCrazyRouter['on' + 'Error']((to, from) => { // 逻辑代码 console.log("Error") }) import Router from './bobo-router' Vue.use(Router) // 路由配置 页面中全部使用this.$Router来操作路由,以实现路由的全局管理 const router = new Router() // 路由全局拦截器 在这里处理登录、授权等相关操作 router.beforeEach(function(to, from, next) { console.log('前置守卫') if (process.car.SIMPLE_RUN) { if (!carhelp.getOpenId()) { carhelp.setOpenId("test") } routerBeforeEach(to, from, next); } else { if (!carhelp.getOpenId()) { if (isWeiXin()) { getOpenId(); } } else { routerBeforeEach(to, from, next); } } }) function getOpenId() { const code = getUrlParam('code'); if (!code) { var url = document.URL; window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url); } else { API_WeiXin.getDataByCode(code).then(response => { carhelp.setOpenId(response.data.openid) //var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, ''); //window.location = linkUrl; //隐藏参数 //return Promise.resolve(response.openid); }).catch(error => { console.log(error); }); } } function routerBeforeEach(to, from, next) { next(); } // 路由后置拦截器 router.afterEach(function(to, from) { console.log('后置守卫') }) // 路由跳转出错处理 router.onError(function(e) { console.log('错误:', e.message || '路由跳转失败') }) export default router