work_admin/src/router/index.js
weipengfei 6443174f14 更新
2024-01-16 10:57:29 +08:00

207 lines
6.2 KiB
JavaScript

import Vue from 'vue'
import store from '@/store'
import Router from 'vue-router'
import Index from '@/views/index'
import Home from './home';
import {getStore, setStore} from "../assets/js/storage";
import {createRoute, isTokenExpired} from "../assets/js/utils";
import config from "../config/config";
import {refreshAccessToken} from "../api/common/common";
import dataV from "@/views/datav/dataV";
import copyV from "@/views/datav/copyV";
let HOME_PAGE = config.HOME_PAGE;
const currentOrganization = getStore('currentOrganization', true);
if (currentOrganization) {
HOME_PAGE = HOME_PAGE + '/' + currentOrganization.code;
}
Vue.use(Router);
const routes = [].concat(
Home
);
// const router = new Router({
// routes: routers
// });
function checkPropertyValue(obj, targetValue) {
// 检查当前对象是否是目标值
if (obj === targetValue) {
return true;
}
// 检查当前对象是否为对象类型
if (typeof obj === 'object') {
// 遍历对象的所有属性
for (let key in obj) {
// 递归调用检查属性值
if (checkPropertyValue(obj[key], targetValue)) {
return true;
}
}
}
// 如果没有找到目标值,则返回 false
return false;
}
const menu = getStore('menu', true);
if (menu) {
menu.forEach(function (v) {
if(checkPropertyValue(v, 'datasv'))return;
if(checkPropertyValue(v, 'copysv'))return;
routes.push(createRoute(v));
if (v.children) {
v.children.forEach(function (v2) {
routes.push(createRoute(v2));
if (v2.children) {
v2.children.forEach(function (v3) {
routes.push(createRoute(v3));
});
}
});
}
});
}
const router = new Router({
routes: [
{
path: '/',
name: 'index',
component: Index,
children: routes
},
{
name: 'datasv',
path: '/datasv',
component: dataV
},
{
name: 'copysv',
path: '/copysv',
component: copyV
},
// {
// name: 'login',
// path: '/login',
// component: resolve => require(['@/views/login'], resolve),
// meta: {model: 'Login'},
// },
{
name: 'member',
path: '/member',
component: resolve => require(['@/components/layout/UserLayout'], resolve),
meta: {model: 'Login'},
children: [
{
path: 'login',
name: 'login',
component: () => import(/* webpackChunkName: "user" */ '@/views/member/login'),
meta: {model: 'Login'},
},
{
path: 'register',
name: 'register',
component: () => import(/* webpackChunkName: "user" */ '@/views/member/Register'),
meta: {model: 'Login'},
},
{
path: 'forgot',
name: 'forgot',
component: () => import(/* webpackChunkName: "user" */ '@/views/member/forgot'),
meta: {model: 'Login'},
},
// {
// path: 'register-result',
// name: 'registerResult',
// component: () => import(/* webpackChunkName: "user" */ '@/views/member/RegisterResult')
// }
]
},
{
name: 'install',
path: '/install',
component: resolve => require(['@/views/error/install'], resolve),
meta: {model: 'error'},
},
{
name: 'resetEmail',
path: '/reset/email',
component: resolve => require(['@/views/reset/email'], resolve),
meta: {model: 'error'},
},
{
name: '404',
path: '/404',
component: resolve => require(['@/views/error/404'], resolve),
meta: {model: 'error'},
},
{
name: '403',
path: '/403',
component: resolve => require(['@/views/error/403'], resolve),
meta: {model: 'error'},
},
{
name: '500',
path: '/500',
component: resolve => require(['@/views/error/500'], resolve),
meta: {model: 'error'},
},
]
});
router.beforeEach((to, from, next) => {
// console.log(to);
let tokenList = getStore('tokenList', true);
if (tokenList) {
let refreshToken = tokenList.refreshToken;
let accessTokenExp = tokenList.accessTokenExp;
//判断accessToken即将到期后刷新token
if (accessTokenExp && isTokenExpired(accessTokenExp)) {
refreshAccessToken(refreshToken).then(res => {
tokenList.accessToken = res.data.accessToken;
tokenList.accessTokenExp = res.data.accessTokenExp;
setStore('tokenList', tokenList);
}).catch((() => {
store.state.logged = false;
store.state.userInfo = null;
return false;
}));
}
}
if (to.name === 'datasv' || to.path === '/datasv' || to.name === 'copysv' || to.path === '/copysv') {
console.log('跳转', to);
next();
return false;
}
//页面中转
if (to.name === 'index' || to.path === '/index' || to.path === '/') {
next({path: HOME_PAGE});
return false;
}
//无效页面跳转至首页
if (!to.name && from.meta.model !== 'Login' && to.path !== HOME_PAGE) {
next({path: '/404'});
return false;
}
if (to.meta.model === 'Login' && store.state.logged) {
next({path: HOME_PAGE});
return false;
}
if (!store.state.logged && to.meta.model !== 'Login' && to.meta.model !== 'error') {
next({
name: 'login',
query: {redirect: to.fullPath}
});
return false;
}
next();
});
router.afterEach(route => {
//预留
// window.scrollTo(0,0)
});
export default router