13 changed files with 439 additions and 162 deletions
@ -1,63 +1,75 @@ |
|||||
import router from './router' |
import router from "./router"; |
||||
import store from './store' |
import store from "./store"; |
||||
import { Message } from 'element-ui' |
import { Message } from "element-ui"; |
||||
import NProgress from 'nprogress' |
import NProgress from "nprogress"; |
||||
import 'nprogress/nprogress.css' |
import "nprogress/nprogress.css"; |
||||
import { getToken } from '@/utils/auth' |
import { getToken } from "@/utils/auth"; |
||||
import { isPathMatch } from '@/utils/validate' |
import { isPathMatch } from "@/utils/validate"; |
||||
import { isRelogin } from '@/utils/request' |
import { isRelogin } from "@/utils/request"; |
||||
|
|
||||
NProgress.configure({ showSpinner: false }) |
NProgress.configure({ showSpinner: false }); |
||||
|
|
||||
const whiteList = ['/login', '/register'] |
const whiteList = ["/login", "/register"]; |
||||
|
|
||||
const isWhiteList = (path) => { |
const isWhiteList = (path) => { |
||||
return whiteList.some(pattern => isPathMatch(pattern, path)) |
return whiteList.some((pattern) => isPathMatch(pattern, path)); |
||||
} |
}; |
||||
|
|
||||
|
const hasHomeRoute = (routes) => { |
||||
|
return Array.isArray(routes) && routes.some((route) => route && route.path === "/" && route.redirect === "/index"); |
||||
|
}; |
||||
|
|
||||
router.beforeEach((to, from, next) => { |
router.beforeEach((to, from, next) => { |
||||
NProgress.start() |
NProgress.start(); |
||||
if (getToken()) { |
if (getToken()) { |
||||
to.meta.title && store.dispatch('settings/setTitle', to.meta.title) |
if (to.meta.title) { |
||||
/* has token*/ |
store.dispatch("settings/setTitle", to.meta.title); |
||||
if (to.path === '/login') { |
} |
||||
next({ path: '/' }) |
if (to.path === "/login") { |
||||
NProgress.done() |
next({ path: "/" }); |
||||
} else if (isWhiteList(to.path)) { |
NProgress.done(); |
||||
next() |
return; |
||||
} else { |
|
||||
if (store.getters.roles.length === 0) { |
|
||||
isRelogin.show = true |
|
||||
// 判断当前用户是否已拉取完user_info信息
|
|
||||
store.dispatch('GetInfo').then(() => { |
|
||||
isRelogin.show = false |
|
||||
store.dispatch('GenerateRoutes').then(accessRoutes => { |
|
||||
// 根据roles权限生成可访问的路由表
|
|
||||
router.addRoutes(accessRoutes) // 动态添加可访问路由表
|
|
||||
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
|
|
||||
}) |
|
||||
}).catch(err => { |
|
||||
store.dispatch('LogOut').then(() => { |
|
||||
Message.error(err) |
|
||||
next({ path: '/' }) |
|
||||
}) |
|
||||
}) |
|
||||
} else { |
|
||||
next() |
|
||||
} |
|
||||
} |
} |
||||
} else { |
|
||||
// 没有token
|
|
||||
if (isWhiteList(to.path)) { |
if (isWhiteList(to.path)) { |
||||
// 在免登录白名单,直接进入
|
next(); |
||||
next() |
return; |
||||
|
} |
||||
|
if (store.getters.roles.length === 0) { |
||||
|
isRelogin.show = true; |
||||
|
store.dispatch("GetInfo").then(() => { |
||||
|
isRelogin.show = false; |
||||
|
store.dispatch("GenerateRoutes").then((accessRoutes) => { |
||||
|
router.addRoutes(accessRoutes); |
||||
|
if (!hasHomeRoute(accessRoutes) && to.path !== "/no-permission") { |
||||
|
next({ path: "/no-permission", replace: true }); |
||||
|
return; |
||||
|
} |
||||
|
next({ ...to, replace: true }); |
||||
|
}); |
||||
|
}).catch((err) => { |
||||
|
store.dispatch("LogOut").then(() => { |
||||
|
Message.error(err); |
||||
|
next({ path: "/" }); |
||||
|
}); |
||||
|
}); |
||||
|
return; |
||||
|
} |
||||
|
next(); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (isWhiteList(to.path)) { |
||||
|
next(); |
||||
|
} else { |
||||
|
if (to.path === "/no-permission") { |
||||
|
next("/login"); |
||||
} else { |
} else { |
||||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`) // 否则全部重定向到登录页
|
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`); |
||||
NProgress.done() |
|
||||
} |
} |
||||
|
NProgress.done(); |
||||
} |
} |
||||
}) |
}); |
||||
|
|
||||
router.afterEach(() => { |
router.afterEach(() => { |
||||
NProgress.done() |
NProgress.done(); |
||||
}) |
}); |
||||
|
|||||
@ -1,33 +1,94 @@ |
|||||
const $math = require('mathjs') |
const Big = require('big.js') |
||||
export const math = { |
|
||||
add (a,b) { |
let mathInstance = null |
||||
return $math.format( |
let mathPromise = null |
||||
$math.add( |
|
||||
$math.bignumber(a),$math.bignumber(b) |
const BIG_METHOD_MAP = { |
||||
) |
add: 'plus', |
||||
) |
subtract: 'minus', |
||||
}, |
multiply: 'times', |
||||
subtract(a,b) { |
divide: 'div' |
||||
return $math.format( |
} |
||||
$math.subtract( |
|
||||
$math.bignumber(a),$math.bignumber(b) |
function normalizeMathModule(mod) { |
||||
) |
return mod && mod.default ? mod.default : mod |
||||
) |
} |
||||
}, |
|
||||
multiply (a,b) { |
function createBig(value) { |
||||
return $math.format( |
return new Big(value) |
||||
$math.multiply( |
} |
||||
$math.bignumber(a),$math.bignumber(b) |
|
||||
) |
function formatBigResult(result) { |
||||
) |
return result.toFixed() |
||||
}, |
} |
||||
divide(a,b) { |
|
||||
return $math.format( |
function calculateWithBig(type, a, b) { |
||||
$math.divide( |
const left = createBig(a) |
||||
$math.bignumber(a),$math.bignumber(b) |
const right = createBig(b) |
||||
) |
return formatBigResult(left[BIG_METHOD_MAP[type]](right)) |
||||
) |
} |
||||
} |
|
||||
|
function calculateWithMathjs(type, a, b) { |
||||
|
return mathInstance.format( |
||||
|
mathInstance[type]( |
||||
|
mathInstance.bignumber(a), |
||||
|
mathInstance.bignumber(b) |
||||
|
) |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
function calculate(type, a, b) { |
||||
|
if (mathInstance) { |
||||
|
return calculateWithMathjs(type, a, b) |
||||
|
} |
||||
|
return calculateWithBig(type, a, b) |
||||
} |
} |
||||
|
|
||||
|
export function loadMathjs() { |
||||
|
if (mathInstance) { |
||||
|
return Promise.resolve(mathInstance) |
||||
|
} |
||||
|
if (!mathPromise) { |
||||
|
mathPromise = import(/* webpackChunkName: "mathjs" */ 'mathjs') |
||||
|
.then(mod => { |
||||
|
mathInstance = normalizeMathModule(mod) |
||||
|
return mathInstance |
||||
|
}) |
||||
|
.catch(err => { |
||||
|
mathPromise = null |
||||
|
throw err |
||||
|
}) |
||||
|
} |
||||
|
return mathPromise |
||||
|
} |
||||
|
|
||||
|
export function warmupMathjs() { |
||||
|
const run = () => { |
||||
|
loadMathjs().catch(() => {}) |
||||
|
} |
||||
|
if (typeof window !== 'undefined' && typeof window.requestIdleCallback === 'function') { |
||||
|
window.requestIdleCallback(run, { timeout: 1500 }) |
||||
|
return |
||||
|
} |
||||
|
if (typeof window !== 'undefined') { |
||||
|
window.setTimeout(run, 0) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export const math = { |
||||
|
add(a, b) { |
||||
|
return calculate('add', a, b) |
||||
|
}, |
||||
|
subtract(a, b) { |
||||
|
return calculate('subtract', a, b) |
||||
|
}, |
||||
|
multiply(a, b) { |
||||
|
return calculate('multiply', a, b) |
||||
|
}, |
||||
|
divide(a, b) { |
||||
|
return calculate('divide', a, b) |
||||
|
}, |
||||
|
ready() { |
||||
|
return loadMathjs() |
||||
|
} |
||||
|
} |
||||
|
|||||
Loading…
Reference in new issue