| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // 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('userNmae',res.data.name)
- uni.setStorageSync('token_str',res.data.token)
- uni.setStorageSync('m_id',res.data.m_id)
- next(to.path)
- } 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
- }
|