UNPKG

xlb-main-login

Version:

``` yarn install ```

203 lines (190 loc) 6.23 kB
import store from '@/store' import vueRouter from 'vue-router' import router from '@/router' import { initRouterMap } from '@/router/router.config' import { RouterPath } from '@/router/dynamic/keyrouter' import resultList from '@/mock/auth/role' import resultOldList from '@/mock/auth/oldrole' import { ceategern, getoldInfo } from '@/api/config' import { getoldaccountList, gettxaccountList } from '@/api/login' import { getauth } from '@/api/weihome' const outPathKeys = Object.keys(RouterPath) const outPath = JSON.stringify(outPathKeys) let initNum = 0 // 路由跳转 async function forward(to, from, next) { // 获取导航栏信息 let redirect = decodeURIComponent(from.query.redirect || to.path) if (redirect.indexOf('?') > 0) { redirect = redirect.slice(0, redirect.indexOf('?')) } if (to.path === redirect) { if (outPath.indexOf(to.path.substring(1, to.path.length)) !== -1) { const outUrl = to.path.substring(1, to.path.length) const indexId = outPathKeys.findIndex((items) => items == outUrl) if (indexId !== -1) { next({ name: 'home', query: { redirectUrl: RouterPath[`${outUrl}`] } }) } else { next({ name: 'home' }) } } else { next() } } else { next({ path: redirect, }) } } function readNodes(nodes = [], arr = []) { for (const item of nodes) { if (item.type == 1) { const currentRouter = { path: item.url, name: item.enname || '', component: router.options.constantRouterComponents[item.enname] || null, // meta: 页面标题, 菜单图标, 页面权限(供指令权限用,可去掉) meta: { keepAlive: item.name == '列表' ? false : false, mainId: item.parentId, id: item.id, title: item.name || '', icon: item.icon || 'xlb-qyyl', target: true, }, } arr.push(currentRouter) } if (item.childList && item.childList.length) { readNodes(item.childList, arr) } } return arr } async function initAccount() { // 先判断是不是特殊账号 test003这种的 特殊账号不做处理 (这个在切换的时候用,这里可以不用判断) // 调用老系统账号数据接口 // 判断当前账号是不是老系统账号 // 是 把vuex里的 hasSwitchPermission 改成 1 // 不是 把vuex里的 hasSwitchPermission 改成 2 let userName = localStorage.getItem('username') await getauth().then((res) => { //获取当前账号信息 let liebianji = '', liebiangao = '', fenxiaoTime = 0 if (res.success) { localStorage.setItem('username', res.data.createBy) localStorage.setItem('createTime:' + res.data.createBy, res.data.createTime) userName = res.data.createBy res.data.roleList.forEach((item) => { if (item.roleId == 45) { liebianji = item.leftTime } if (item.roleId == 46) { liebiangao = item.leftTime } }) fenxiaoTime = liebianji > liebiangao ? liebianji : liebiangao localStorage.setItem('fenxiaoTime', fenxiaoTime) } }) if (!ceategern.includes(userName)) { //判断不是特殊账号 await getoldaccountList().then((res) => { if (res && res.length > 0 && res.includes(userName)) { store.state.hasSwitchPermission = 1 store.state.menuType = 1 //老系统 } }) await gettxaccountList().then((res) => { if (res && res.length > 0 && res.includes(userName)) { //腾讯系统 store.state.hasSwitchPermission = 1 store.state.menuType = 2 } }) } } async function initRouter() { await initAccount() // 账户登录版本 1-2021 2-2022 3-2019 if (store.state.toggleVersion.permissionType !== 3) { store.dispatch('getPermission') } else { const roleList = await getOldInfo() store.dispatch('setUserPermission', roleList) } if (initNum !== 0) return store.dispatch('getPermission').then((res) => { const permission = res initNum++ const routerTree = readNodes(permission) const routerTreeTemp = readNodes(getOldInfo()) const listTemp = [...routerTree, ...routerTreeTemp] const dataTemp = {} let routerList = null routerList = listTemp.reduce((data, item) => { dataTemp[item.name] ? '' : (dataTemp[item.name] = true && data.push(item)) return data }, []) for (let i = 0; i < initRouterMap.length; i++) { if (initRouterMap[i].name && initRouterMap[i].name === 'home') { initRouterMap[i].children = [...routerList] break } } router.options.routes = [] router.matcher = new vueRouter().matcher router.addRoutes(initRouterMap) }) } // 切换版本时候重新刷新动态路由 export async function refreshRouter() { return store.dispatch('getPermission').then((res) => { const routerTree = readNodes(res) const routerTreeTemp = readNodes(getOldInfo()) const listTemp = [...routerTree, ...routerTreeTemp] const dataTemp = {} let routerList = null routerList = listTemp.reduce((data, item) => { dataTemp[item.name] ? '' : (dataTemp[item.name] = true && data.push(item)) return data }, []) for (let i = 0; i < initRouterMap.length; i++) { if (initRouterMap[i].name && initRouterMap[i].name === 'home') { initRouterMap[i].children = [...routerList] break } } console.log(7333, initRouterMap) router.options.routes = [] router.matcher = new vueRouter().matcher router.addRoutes(initRouterMap) }) } function nextFilter(to, next) { if (to.matched.length === 0) { // 如果未匹配到路由查询是否为老系统路由 const toRouter = to.path.substr(1) const RouterPathKey = Object.keys(RouterPath) if (RouterPathKey.includes(toRouter)) { next(RouterPath[toRouter.toString()]) return } next('/404') } else { next() } } function getOldInfo() { let roleList if (getoldInfo.includes(store.state.user_link)) { roleList = resultOldList.roleList.value } else { roleList = resultList.roleList.value } return roleList } export { forward, initRouter }