tourl跳转

master
xy 1 year ago
parent 69ea32fa46
commit 5d7190b1f9

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

@ -135,3 +135,45 @@ export function moneyFormatter(money,precision=2){
export function moneyRecovery(money){ export function moneyRecovery(money){
return parseFloat(money.replace(/[^\d\.-]/g, "")); 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