You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.9 KiB
97 lines
2.9 KiB
2 years ago
|
import Vue from 'vue'
|
||
|
import Router from 'vue-router'
|
||
|
import store from '@/store'
|
||
|
import { constantRouterMap } from './router.config.js'
|
||
|
import { login } from '@/api/user'
|
||
|
import { title } from '@/config'
|
||
|
|
||
|
const whiteList = ['/login']
|
||
|
const originalPush = Router.prototype.push
|
||
|
Router.prototype.push = function push(location, onResolve, onReject) {
|
||
|
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
|
||
|
return originalPush.call(this, location).catch(err => err)
|
||
|
}
|
||
|
|
||
|
Vue.use(Router)
|
||
|
|
||
|
const createRouter = () =>
|
||
|
new Router({
|
||
|
// mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath
|
||
|
base: '/epmet-work-wx',
|
||
|
// base: process.env.BASE_URL,
|
||
|
scrollBehavior: () => ({ y: 0 }),
|
||
|
routes: constantRouterMap
|
||
|
})
|
||
|
|
||
|
const router = createRouter()
|
||
|
|
||
|
export function resetRouter() {
|
||
|
const newRouter = createRouter()
|
||
|
router.matcher = newRouter.matcher // reset router
|
||
|
}
|
||
|
|
||
|
//根据参数名获取参数值
|
||
|
function getQueryStringByName(name) {
|
||
|
var result = location.search.match(new RegExp('[?&]' + name + '=([^&]+)', 'i'))
|
||
|
if (result == null || result.length < 1) {
|
||
|
return ''
|
||
|
}
|
||
|
return result[1]
|
||
|
}
|
||
|
|
||
|
function paramsFormat(params) {
|
||
|
let strArray = []
|
||
|
for (let key in params) {
|
||
|
if (params[key]) {
|
||
|
strArray.push(key + '=' + params[key])
|
||
|
}
|
||
|
}
|
||
|
return strArray.join('&')
|
||
|
}
|
||
|
|
||
|
router.beforeEach((to, from, next) => {
|
||
|
document.title = to.meta.title ? to.meta.title : title
|
||
|
if (localStorage.getItem('token')) {
|
||
|
store.dispatch('getUserInfo').then(() => {
|
||
|
next()
|
||
|
})
|
||
|
} 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 = 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 {
|
||
|
// 账号密码登录
|
||
|
if (whiteList.includes(to.path)) {
|
||
|
next()
|
||
|
} else {
|
||
|
next({ path: '/login', query: { redirect: to.path, params: JSON.stringify(to.query) } })
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
export default router
|