| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- // router.js
- import { RouterMount, createRouter, runtimeQuit } from '@/js_sdk/hhyang-uni-simple-router/uni-simple-router';
- import tools from "@/service/tools";
- import {userWxAppletLogin} from '@/api/user.js'
- const router = createRouter({
- platform: process.env.VUE_APP_PLATFORM,
- routerErrorEach:({type,level,...args})=>{
- // console.log({type,level,...args})
- // #ifdef APP-PLUS
- if(type===3){
- router.$lockStatus=false;
- tools.onBackPress()
- }
- // #endif
- },
- routes: [...ROUTES]
- });
- //全局路由前置守卫
- router.beforeEach( async (to, from, next) => {
-
- if(to.query.shopCode){
- uni.setStorageSync("invite_code", to.query.shopCode);
- }
- if(to.query.memberCode){
- uni.setStorageSync("memberCode", to.query.memberCode);
- }
-
- let isWeiXin = tools.isWx();
-
- if(isWeiXin){
- let userToken = uni.getStorageSync('token_str')
- if(userToken){
- next();
- }else{
- const href = window.location.href;
- if (href.indexOf('?code=') > -1 || href.indexOf('&code=') > -1){
- const urlArr = href.split('?')
- const rightUrlArr = urlArr[1].split('#/')
- const queryObj = {}
- rightUrlArr[0].split('&').map((item) => {
- const splitStr = item.split('=')
- return {
- key: splitStr[0],
- value: splitStr[1],
- }
- }).forEach((item) => {
- queryObj[item.key] = item.value
- })
- if(urlArr.length>=3){
- rightUrlArr[1]= "?"+urlArr[2]
- }
- let from = {
- code: queryObj.code,
- wx_type:1,
- invite_code:uni.getStorageSync("invite_code"),
- memberCode:uni.getStorageSync("memberCode"),
- }
- await userWxAppletLogin(from).then((res)=>{
- if (res.code === 1) {
- uni.setStorageSync('userName',res.data.name)
- uni.setStorageSync('token_str',res.data.token_str)
- uni.setStorageSync('m_id',res.data.m_id)
- next()
- } else {
- tools.error(res.msg);
- }
- })
- }else{
- uni.removeStorageSync('token_str')
- uni.removeStorageSync('m_id')
- tools.wxLogin();
- }
- }
- }else{
- next()
- }
-
-
- });
- // 全局路由后置守卫
- router.afterEach((to, from) => {
- // console.log('后置守卫to------', to)
- })
- export {
- router,
- RouterMount
- }
|