13 changed files with 439 additions and 162 deletions
@ -1,63 +1,75 @@ |
|||
import router from './router' |
|||
import store from './store' |
|||
import { Message } from 'element-ui' |
|||
import NProgress from 'nprogress' |
|||
import 'nprogress/nprogress.css' |
|||
import { getToken } from '@/utils/auth' |
|||
import { isPathMatch } from '@/utils/validate' |
|||
import { isRelogin } from '@/utils/request' |
|||
import router from "./router"; |
|||
import store from "./store"; |
|||
import { Message } from "element-ui"; |
|||
import NProgress from "nprogress"; |
|||
import "nprogress/nprogress.css"; |
|||
import { getToken } from "@/utils/auth"; |
|||
import { isPathMatch } from "@/utils/validate"; |
|||
import { isRelogin } from "@/utils/request"; |
|||
|
|||
NProgress.configure({ showSpinner: false }) |
|||
NProgress.configure({ showSpinner: false }); |
|||
|
|||
const whiteList = ['/login', '/register'] |
|||
const whiteList = ["/login", "/register"]; |
|||
|
|||
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) => { |
|||
NProgress.start() |
|||
NProgress.start(); |
|||
if (getToken()) { |
|||
to.meta.title && store.dispatch('settings/setTitle', to.meta.title) |
|||
/* has token*/ |
|||
if (to.path === '/login') { |
|||
next({ path: '/' }) |
|||
NProgress.done() |
|||
} else if (isWhiteList(to.path)) { |
|||
next() |
|||
} 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() |
|||
} |
|||
if (to.meta.title) { |
|||
store.dispatch("settings/setTitle", to.meta.title); |
|||
} |
|||
if (to.path === "/login") { |
|||
next({ path: "/" }); |
|||
NProgress.done(); |
|||
return; |
|||
} |
|||
} else { |
|||
// 没有token
|
|||
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 { |
|||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`) // 否则全部重定向到登录页
|
|||
NProgress.done() |
|||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`); |
|||
} |
|||
NProgress.done(); |
|||
} |
|||
}) |
|||
}); |
|||
|
|||
router.afterEach(() => { |
|||
NProgress.done() |
|||
}) |
|||
NProgress.done(); |
|||
}); |
|||
|
|||
@ -1,33 +1,94 @@ |
|||
const $math = require('mathjs') |
|||
export const math = { |
|||
add (a,b) { |
|||
return $math.format( |
|||
$math.add( |
|||
$math.bignumber(a),$math.bignumber(b) |
|||
) |
|||
) |
|||
}, |
|||
subtract(a,b) { |
|||
return $math.format( |
|||
$math.subtract( |
|||
$math.bignumber(a),$math.bignumber(b) |
|||
) |
|||
) |
|||
}, |
|||
multiply (a,b) { |
|||
return $math.format( |
|||
$math.multiply( |
|||
$math.bignumber(a),$math.bignumber(b) |
|||
) |
|||
) |
|||
}, |
|||
divide(a,b) { |
|||
return $math.format( |
|||
$math.divide( |
|||
$math.bignumber(a),$math.bignumber(b) |
|||
) |
|||
) |
|||
} |
|||
const Big = require('big.js') |
|||
|
|||
let mathInstance = null |
|||
let mathPromise = null |
|||
|
|||
const BIG_METHOD_MAP = { |
|||
add: 'plus', |
|||
subtract: 'minus', |
|||
multiply: 'times', |
|||
divide: 'div' |
|||
} |
|||
|
|||
function normalizeMathModule(mod) { |
|||
return mod && mod.default ? mod.default : mod |
|||
} |
|||
|
|||
function createBig(value) { |
|||
return new Big(value) |
|||
} |
|||
|
|||
function formatBigResult(result) { |
|||
return result.toFixed() |
|||
} |
|||
|
|||
function calculateWithBig(type, a, b) { |
|||
const left = createBig(a) |
|||
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