From 4ba0cd18de02f43beab002c2e59e8dc2a498e82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=AF=E5=B0=8F=E7=8E=8B=E5=91=80=5C24601?= <819653817@qq.com> Date: Tue, 17 Dec 2024 10:52:01 +0800 Subject: [PATCH] 13 --- src/router/index.js | 137 ++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 69 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 403915c..a7a9905 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -5,7 +5,7 @@ import { constantRouterMap } from './router.config.js' import { checkWxmpRegister, login } from '@/api/user' import { title } from '@/config' -const whiteList = ['/login', '/register', '/errorPage','/selectAgency','/houseQR','/event'] +const whiteList = ['/login', '/register', '/errorPage','/selectAgency','/houseQR','/event','/scanQrCode'] const originalPush = Router.prototype.push Router.prototype.push = function push(location, onResolve, onReject) { if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject) @@ -51,91 +51,90 @@ function paramsFormat(params) { router.beforeEach((to, from, next) => { let urlParams = getQueryParams() - console.log(urlParams,'路由参数'); if(urlParams.houseId){ console.log(urlParams,'初始路由参数'); store.commit('SET_HOUSE_ID', urlParams.houseId) }else{ store.commit('SET_HOUSE_ID', null) } - if(urlParams.unitId){ - store.commit('SET_UNIT_ID', urlParams.unitId) + if(urlParams.type && !localStorage.getItem('token')&&to.path !=='/scanQrCode' ){ + next('/scanQrCode') }else{ - store.commit('SET_UNIT_ID', null) - } - if (urlParams.appId) { - if (localStorage.getItem('appId') && urlParams.appId !== localStorage.getItem('appId')) { - localStorage.removeItem('token') - location.reload() - } - store.commit('SET_APP_ID', urlParams.appId) - localStorage.setItem('appId', urlParams.appId) - } else { - store.commit('SET_APP_ID', localStorage.getItem('appId')) - } - if (!localStorage.getItem('appId') && to.path !== '/errorPage'&& to.path != '/houseQR') { - if(to.path === '/errorPage'){ - next('/errorPage') - }else{ - next('/') - } - } else { - if (to.params.type) { - document.title = to.params.type === 'edit' ? '编辑' : '注册' + if (urlParams.appId) { + if (localStorage.getItem('appId') && urlParams.appId !== localStorage.getItem('appId')) { + localStorage.removeItem('token') + location.reload() + } + store.commit('SET_APP_ID', urlParams.appId) + localStorage.setItem('appId', urlParams.appId) } else { - document.title = to.meta.title ? to.meta.title : title + store.commit('SET_APP_ID', localStorage.getItem('appId')) } - if (localStorage.getItem('token') && to.path != '/houseQR') { - store.dispatch('getUserInfo').then((res) => { - if (res.agencyId) { - next() - } else { - if (to.path === '/selectAgency') { + if (!localStorage.getItem('appId') && to.path !== '/errorPage'&& to.path != '/houseQR' && to.path != '/scanQrCode') { + if(to.path === '/errorPage'){ + next('/errorPage') + }else{ + next('/') + } + } else { + if (to.params.type) { + document.title = to.params.type === 'edit' ? '编辑' : '注册' + } else { + document.title = to.meta.title ? to.meta.title : title + } + if (localStorage.getItem('token') && to.path != '/houseQR') { + store.dispatch('getUserInfo').then((res) => { + if (res.agencyId) { next() } else { - next({ path: '/selectAgency' }) + if (to.path === '/selectAgency') { + next() + } else { + next({ path: '/selectAgency' }) + } } - } - }).catch(err=>{ - localStorage.removeItem('token') - next({ path: '/login'}) - }) - } else { - if (getQueryStringByName('code') && to.path != '/houseQR') { - login({ wxCode: getQueryStringByName('code'),appId:'wx1078fa1e99424de9'}).then(res => { - localStorage.setItem('token', res.data.token) - localStorage.setItem('userId', res.data.userH5DTO.id) - let state = decodeURIComponent(getQueryStringByName('state')) - state = state.replace(/[\\\b\f\n\r\t]/g, '') - state = state.replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9]+)(['"])?:/g, '$1"$3":') - state = state - .replace(/":/g, '":"') - .replace(/,"/g, '","') - .replace(/},/g, '"},') - .replace(/}]/g, '"}]') - .replace(/}}/g, '"}}') - .replace(/"{/g, '{') - .replace(/:{"}/g, ':{}') - state = JSON.stringify({ path: '/', query: {} }); - let path = `${location.origin}${location.pathname === '/' ? '' : location.pathname}` - if (state && state != 'undefined') { - state = JSON.parse(state) - console.log(state, 'state') - console.log(`${path}/#${state.path}?${paramsFormat(state.query)}`, 'sss') - location.href = `${path}/#${state.path}?${paramsFormat(state.query)}` - } else { - location.href = path - } + }).catch(err=>{ + localStorage.removeItem('token') + next({ path: '/login'}) }) - } else { - if (whiteList.includes(to.path)) { - next() + } else { + if (getQueryStringByName('code') && to.path != '/houseQR' && to.path != '/scanQrCode') { + login({ wxCode: getQueryStringByName('code'),appId:'wx1078fa1e99424de9'}).then(res => { + localStorage.setItem('token', res.data.token) + localStorage.setItem('userId', res.data.userH5DTO.id) + let state = decodeURIComponent(getQueryStringByName('state')) + state = state.replace(/[\\\b\f\n\r\t]/g, '') + state = state.replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9]+)(['"])?:/g, '$1"$3":') + state = state + .replace(/":/g, '":"') + .replace(/,"/g, '","') + .replace(/},/g, '"},') + .replace(/}]/g, '"}]') + .replace(/}}/g, '"}}') + .replace(/"{/g, '{') + .replace(/:{"}/g, ':{}') + state = JSON.stringify({ path: '/', query: {} }); + let path = `${location.origin}${location.pathname === '/' ? '' : location.pathname}` + if (state && state != 'undefined') { + state = JSON.parse(state) + console.log(state, 'state') + console.log(`${path}/#${state.path}?${paramsFormat(state.query)}`, 'sss') + location.href = `${path}/#${state.path}?${paramsFormat(state.query)}` + } else { + location.href = path + } + }) } else { - next({ path: '/login', query: { redirect: to.path, params: JSON.stringify(to.query) } }) + if (whiteList.includes(to.path)) { + next() + } else { + next({ path: '/login', query: { redirect: to.path, params: JSON.stringify(to.query) } }) + } } } } - } + } + }) function getQueryParams() {