UNPKG

xlb-main-login

Version:

``` yarn install ```

56 lines (52 loc) 1.63 kB
import Vue from 'vue' import router from './router' import store from './store' import { ACCESS_TOKEN } from '@/store/mutation-types' import { forward, initRouter } from './forward' const whiteList = ['login', 'register', 'forget', 'password', 'respwd', 'authDevelop', 'qiankun'] // no redirect whitelist const getParam = function (name) { const path = location.href const reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i') if (reg.test(path)) { return unescape(RegExp.$2.replace(/\+/g, ' ')) } return '' } // 前置守卫 router.beforeEach((to, from, next) => { // 终止前一页的请求 store.state.requestCancels = [] const redirectUrl = store.state.auth?.redirectUrl || '' const token = getParam('access_token') || window.sessionStorage.getItem('access_token') if (whiteList.includes(to.name)) { // 在免登录白名单,直接进入 next() } else if (Vue.ls.get(ACCESS_TOKEN) || token) { /* has token */ // next() if (token) { window.sessionStorage.setItem('access_token', token) Vue.ls.set(ACCESS_TOKEN, token) } else { window.sessionStorage.removeItem('access_token') } initRouter() if (to.name !== 'home') { forward(to, from, next) } else { next() } } else if (redirectUrl !== '') { location.href = redirectUrl + '?redirectUrl=' + location.href } else { const loginPath = { path: '/login', } if (to.fullPath !== '/') { loginPath.query = { redirect: to.fullPath, } } next(loginPath) } })