|
|
@ -5,7 +5,7 @@ import { constantRouterMap } from './router.config.js' |
|
|
|
import { checkWxmpRegister, login } from '@/api/user' |
|
|
|
import { title } from '@/config' |
|
|
|
|
|
|
|
const whiteList = ['/login'] |
|
|
|
const whiteList = ['/login', '/register', '/errorPage'] |
|
|
|
const originalPush = Router.prototype.push |
|
|
|
Router.prototype.push = function push(location, onResolve, onReject) { |
|
|
|
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject) |
|
|
@ -54,60 +54,71 @@ router.beforeEach((to, from, next) => { |
|
|
|
if (urlParams.appId) { |
|
|
|
store.commit('SET_APP_ID', urlParams.appId) |
|
|
|
localStorage.setItem('appId', urlParams.appId) |
|
|
|
if (localStorage.getItem('appId') && urlParams.appId !== localStorage.getItem('appId')) { |
|
|
|
localStorage.removeItem('token') |
|
|
|
location.reload() |
|
|
|
} |
|
|
|
} else { |
|
|
|
store.commit('SET_APP_ID', localStorage.getItem('appId')) |
|
|
|
} |
|
|
|
console.log(store.state.app.appId, 'store.state.app.appId') |
|
|
|
if (to.params.type) { |
|
|
|
document.title = to.params.type === 'edit' ? '编辑' : '注册' |
|
|
|
} else { |
|
|
|
document.title = to.meta.title ? to.meta.title : title |
|
|
|
} |
|
|
|
if (localStorage.getItem('token')) { |
|
|
|
store.dispatch('getUserInfo').then(() => { |
|
|
|
checkWxmpRegister({ |
|
|
|
userId: store.state.app.userInfo.userId |
|
|
|
}).then(res => { |
|
|
|
if (res) { |
|
|
|
next() |
|
|
|
} else { |
|
|
|
next('/register') |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
if (!localStorage.getItem('appId') && to.path !== '/errorPage') { |
|
|
|
next('/errorPage') |
|
|
|
} else { |
|
|
|
if (getQueryStringByName('code')) { |
|
|
|
// return
|
|
|
|
login({ wxCode: getQueryStringByName('code') }).then(res => { |
|
|
|
localStorage.setItem('token', res.token) |
|
|
|
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, ':{}') |
|
|
|
console.log(state) |
|
|
|
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 |
|
|
|
} |
|
|
|
if (to.params.type) { |
|
|
|
document.title = to.params.type === 'edit' ? '编辑' : '注册' |
|
|
|
} else { |
|
|
|
document.title = to.meta.title ? to.meta.title : title |
|
|
|
} |
|
|
|
if (localStorage.getItem('token')) { |
|
|
|
store.dispatch('getUserInfo').then(() => { |
|
|
|
checkWxmpRegister({ |
|
|
|
userId: store.state.app.userInfo.userId |
|
|
|
}).then(res => { |
|
|
|
if (res) { |
|
|
|
next() |
|
|
|
} else { |
|
|
|
if (to.path === '/register') { |
|
|
|
next() |
|
|
|
} else { |
|
|
|
console.log(to) |
|
|
|
next({ path: '/register', query: { redirect: to.path, params: JSON.stringify(to.query) } }) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} else { |
|
|
|
// 账号密码登录
|
|
|
|
if (whiteList.includes(to.path)) { |
|
|
|
next() |
|
|
|
if (getQueryStringByName('code')) { |
|
|
|
// return
|
|
|
|
login({ wxCode: getQueryStringByName('code') }).then(res => { |
|
|
|
localStorage.setItem('token', res.token) |
|
|
|
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, ':{}') |
|
|
|
console.log(state) |
|
|
|
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) } }) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|