tourl跳转

master
xy 1 year ago
parent 69ea32fa46
commit 5d7190b1f9

@ -3,6 +3,7 @@ import store from './store'
import {
Message
} from 'element-ui'
import { deepCopy } from '@/utils'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {
@ -29,7 +30,6 @@ router.beforeEach(async (to, from, next) => {
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
console.log(to.fullPath)
await store.dispatch('user/resetToken')
next(to.fullPath)
@ -41,7 +41,7 @@ router.beforeEach(async (to, from, next) => {
// determine whether the user has obtained his permission roles through getInfo
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {
next()
to.query.tourl ? next(to.query.tourl) : next()
} else {
try {
// get user info
@ -58,10 +58,11 @@ router.beforeEach(async (to, from, next) => {
// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
next({
...to,
replace: true
})
let resetTo = deepCopy(to)
resetTo.path = to.query.tourl || to.path
resetTo.replace = true
console.log(resetTo)
next(resetTo)
} catch (error) {
console.log(error)
// remove token and go to login page to re-login
@ -78,12 +79,25 @@ router.beforeEach(async (to, from, next) => {
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
if (to.query.token && to.query.tp) {
try {
await store.dispatch('user/loginskip', {
token: to.query.token,
tp: to.query.tp,
loginId: to.query.loginId
})
next(to.query.tourl || '/')
} catch (e) {
next('/login')
}
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
})
router.afterEach(() => {

@ -135,3 +135,45 @@ export function moneyFormatter(money,precision=2){
export function moneyRecovery(money){
return parseFloat(money.replace(/[^\d\.-]/g, ""));
}
export function deepCopy(data) {
//string,number,bool,null,undefined,symbol
//object,array,date
if (data && typeof data === "object") {
//针对函数的拷贝
if (typeof data === "function") {
let tempFunc = data.bind(null);
tempFunc.prototype = deepCopy(data.prototype);
return tempFunc;
}
switch (Object.prototype.toString.call(data)) {
case "[object String]":
return data.toString();
case "[object Number]":
return Number(data.toString());
case "[object Boolean]":
return Boolean(data.toString());
case "[object Date]":
return new Date(data.getTime());
case "[object Array]":
let arr = [];
for (let i = 0; i < data.length; i++) {
arr[i] = deepCopy(data[i]);
}
return arr;
//js自带对象或用户自定义类实例
case "[object Object]":
let obj = {};
for (let key in data) {
//会遍历原型链上的属性方法可以用hasOwnProperty来控制 obj.hasOwnProperty(prop)
obj[key] = deepCopy(data[key]);
}
return obj;
}
} else {
//string,number,bool,null,undefined,symbol
return data;
}
}

Loading…
Cancel
Save