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.
138 lines
4.3 KiB
138 lines
4.3 KiB
import Vue from 'vue'
|
|
import Router from 'vue-router'
|
|
import store from '@/store'
|
|
import { constantRouterMap } from './router.config.js'
|
|
import { checkWxmpRegister, login } from '@/api/user'
|
|
import { title } from '@/config'
|
|
|
|
const whiteList = ['/login', '/register', '/errorPage','/selectAgency']
|
|
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) => {
|
|
let urlParams = getQueryParams()
|
|
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') {
|
|
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')) {
|
|
store.dispatch('getUserInfo').then((res) => {
|
|
if (res.agencyId) {
|
|
next()
|
|
} else {
|
|
if (to.path === '/selectAgency') {
|
|
next()
|
|
} else {
|
|
console.log(to)
|
|
next({ path: '/selectAgency' })
|
|
}
|
|
}
|
|
})
|
|
} else {
|
|
if (getQueryStringByName('code')) {
|
|
login({ wxCode: getQueryStringByName('code'),appId:'wx1078fa1e99424de9'}).then(res => {
|
|
localStorage.setItem('token', res.token)
|
|
localStorage.setItem('userId', res.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, ':{}')
|
|
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 {
|
|
if (whiteList.includes(to.path)) {
|
|
next()
|
|
} else {
|
|
next({ path: '/login', query: { redirect: to.path, params: JSON.stringify(to.query) } })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
function getQueryParams() {
|
|
const url = window.location.href
|
|
const paramArr = url.slice(url.indexOf('?') + 1).split('&')
|
|
const params = {}
|
|
paramArr.map(param => {
|
|
const [key, val] = param.split('=')
|
|
params[key] = decodeURIComponent(val)
|
|
})
|
|
return params
|
|
}
|
|
|
|
export default router
|
|
|