import {
|
createRouter,
|
createWebHistory,
|
createWebHashHistory,
|
createMemoryHistory,
|
} from 'vue-router'
|
import {GetMenus} from "@/api/admin/system";
|
import {GetAccountWithRolesAndPermissions} from "@/api/admin/security";
|
import {saveLocalStorage, getLocalStorage} from '@/utils/common'
|
import Cookies from "js-cookie";
|
import * as elementplus from "element-plus";
|
import i18n from "../../public/assets/i18n/i18n";
|
import NProgress from 'nprogress'
|
import 'nprogress/nprogress.css'
|
|
NProgress.configure({showSpinner: false}, {ease: 'ease', speed: 600});
|
|
import License from '../views/License.vue'
|
import LicenseSecrecy from '../views/LicenseSecrecy.vue'
|
import LayoutAdmin from '../views/admin/Layout.vue'
|
import AdminLogin from '../views/admin/Login.vue'
|
import AccountActivation from '../views/admin/AccountActivation.vue'
|
import Register from '../views/admin/Register.vue'
|
import RetrievePassword from '../views/admin/RetrievePassword.vue'
|
import Workbench from '../views/admin/Workbench.vue'
|
import AdminProfile from '../views/admin/UserProfile.vue'
|
|
import masterdataRoutes from './routedata/masterdata';
|
import QuestionSubjectKnowledgePoint from '../views/admin/question/QuestionSubjectKnowledgePoint.vue'
|
import QuestionPaper from '../views/admin/question/QuestionPaper.vue'
|
import QuestionPaperQuestionList from '../views/admin/question/QuestionPaperQuestionList.vue'
|
|
import OrganizationDepartment from '../views/admin/organization/OrganizationDepartment.vue'
|
import OrganizationMajor from '../views/admin/organization/OrganizationMajor.vue'
|
import OrganizationGrade from '../views/admin/organization/OrganizationGrade.vue'
|
import OrganizationGradeSubjectRelation from '../views/admin/question/OrganizationGradeSubjectRelation.vue'
|
import ActiveOrderDetail from '../views/admin/order/ActiveOrderDetail.vue'
|
|
import MobileQuestion from '../views/mobile/question/question.vue'
|
|
const routePaths = {
|
Workbench: '/admin/workbench',
|
Navigation: '/admin/workbench',
|
AdminLogin: '/admin/login',
|
AccountActivation: '/admin/accountactivation',
|
RetrievePassword: '/admin/retrievepassword',
|
Register: '/admin/register',
|
License: '/license',
|
LicenseSecrecy: '/license/secrecy',
|
AdminProfile: '/admin/profile',
|
Error404: '/404',
|
Error500: '/500',
|
};
|
|
const modules = import.meta.glob('../views/**/**/*.vue')
|
const routes = [
|
{
|
path: '/:catchAll(.*)',
|
redirect: '/404',
|
},
|
{
|
path: routePaths.AdminLogin,
|
name: 'AdminLogin',
|
component: AdminLogin,
|
},
|
{
|
path: routePaths.AccountActivation,
|
name: 'AccountActivation',
|
component: AccountActivation,
|
},
|
{
|
path: routePaths.RetrievePassword,
|
name: 'RetrievePassword',
|
component: RetrievePassword,
|
},
|
{
|
path: routePaths.Register,
|
name: 'Register',
|
component: Register,
|
},
|
{
|
path: routePaths.License,
|
name: 'License',
|
component: License,
|
},
|
{
|
path: routePaths.LicenseSecrecy,
|
name: 'LicenseSecrecy',
|
component: LicenseSecrecy,
|
},
|
{
|
path: '/admin',
|
name: '/Admin',
|
component: LayoutAdmin,
|
redirect: routePaths.Workbench,
|
children: [
|
{
|
path: routePaths.Error404,
|
name: '404',
|
component: () => import(`@/views/error/404.vue`),
|
},
|
{
|
path: routePaths.Workbench,
|
name: 'Workbench',
|
component: Workbench,
|
meta: {
|
languagekey: 'Workbench',
|
icon: 'workbench'
|
},
|
},
|
{
|
path: routePaths.AdminProfile,
|
name: 'AdminProfile',
|
component: AdminProfile,
|
meta: {
|
languagekey: 'Profile',
|
icon: 'user'
|
},
|
},
|
{
|
path: '/admin/question/QuestionSubjectKnowledgePoint',
|
name: 'QuestionSubjectKnowledgePoint',
|
component: QuestionSubjectKnowledgePoint,
|
meta: {
|
keepAlive: true
|
}
|
},
|
{
|
path: '/admin/question/QuestionPaper',
|
name: 'QuestionPaper',
|
component: QuestionPaper,
|
meta: {
|
keepAlive: true
|
}
|
},
|
{
|
path: '/admin/question/QuestionPaperQuestionList',
|
name: 'QuestionPaperQuestionList',
|
component: QuestionPaperQuestionList,
|
meta: {
|
keepAlive: true
|
}
|
},
|
{
|
path: '/admin/organization/OrganizationDepartment',
|
name: 'OrganizationDepartment',
|
component: OrganizationDepartment,
|
meta: {
|
keepAlive: true
|
}
|
},
|
{
|
path: '/admin/organization/OrganizationMajor',
|
name: 'OrganizationMajor',
|
component: OrganizationMajor,
|
meta: {
|
keepAlive: true
|
}
|
},
|
{
|
path: '/admin/organization/OrganizationGrade',
|
name: 'OrganizationGrade',
|
component: OrganizationGrade,
|
meta: {
|
keepAlive: true
|
}
|
},
|
{
|
path: '/admin/organization/OrganizationGradeSubjectRelation',
|
name: 'OrganizationGradeSubjectRelation',
|
component: OrganizationGradeSubjectRelation,
|
meta: {
|
keepAlive: true
|
}
|
},
|
{
|
path: '/admin/order/ActiveOrderDetail',
|
name: 'ActiveOrderDetail',
|
component: ActiveOrderDetail,
|
meta: {
|
keepAlive: true
|
}
|
},
|
]
|
},
|
{
|
path: '/mobile/question',
|
name: 'Question',
|
component: MobileQuestion,
|
},
|
...masterdataRoutes.routes,
|
]
|
|
const routerHistory = createWebHistory(WGURL.namespace);
|
const routerHash = createWebHashHistory(WGURL.namespace);
|
const routerBbstract = createMemoryHistory(WGURL.namespace);
|
const router = createRouter({
|
history: routerHistory, routes: routes
|
});
|
|
router.routeDatas = {
|
masterdataRoutes,
|
}
|
|
let localstorageMenus;
|
let userid;
|
let token;
|
|
router.beforeEach(async (to, from, next) => {
|
NProgress.start();
|
|
//处理浏览器直接访问路由时路由找不到的问题
|
if (to.name === "404" && to.redirectedFrom.path) {
|
if (!checkToken() || !checkUser()) {
|
if (WGURL.UseSSO) {
|
window.location.href = `${WGURL.SSOLoginUri}?source=${window.location.href}`;
|
} else {
|
next({path: '/admin/login', replace: true});
|
}
|
|
NProgress.done();
|
return;
|
}
|
|
const hasRouters = await getAsyncRouters();
|
const hasPermission = await getAsyncUserWithRolesAndPermissions(userid);
|
if (hasRouters && hasPermission) {
|
if (router.getRoutes().filter(e => e.path === to.redirectedFrom.path).shift()) {
|
next({path: to.redirectedFrom.path});
|
} else {
|
next();
|
}
|
|
NProgress.done();
|
return;
|
} else {
|
elementplus.ElNotification({
|
title: 'Error',
|
message: `${i18n.t('message.AccessIsDenied')}`,
|
position: 'top-right',
|
duration: 5000,
|
type: 'error'
|
});
|
if (WGURL.UseSSO) {
|
window.location.href = `${WGURL.SSOLoginUri}?source=${window.location.href}`;
|
} else {
|
next({path: '/admin/login', replace: true});
|
}
|
|
NProgress.done();
|
return;
|
}
|
}
|
|
let frompath = from.path.toLocaleLowerCase();
|
let topath = to.path.toLocaleLowerCase();
|
|
if (!topath.startsWith('/admin/')) {
|
next();
|
|
NProgress.done();
|
return;
|
} else if (topath === '/admin/login') {
|
if (WGURL.UseSSO) {
|
window.location.href = `${WGURL.SSOLoginUri}?source=${window.location.href}`;
|
} else {
|
next();
|
}
|
|
NProgress.done();
|
return;
|
} else if (topath === '/admin/accountactivation' || topath === '/admin/retrievepassword' || topath === '/admin/register') {
|
next();
|
|
NProgress.done();
|
return;
|
} else if (topath !== '/admin/login') {
|
if (!checkToken() || !checkUser()) {
|
if (WGURL.UseSSO) {
|
window.location.href = `${WGURL.SSOLoginUri}?source=${window.location.href}`;
|
} else {
|
next({path: '/admin/login', replace: true});
|
}
|
|
NProgress.done();
|
return;
|
}
|
|
const hasRouters = await getAsyncRouters();
|
const hasPermission = await getAsyncUserWithRolesAndPermissions(userid);
|
if (hasRouters && hasPermission) {
|
next();
|
|
NProgress.done();
|
return;
|
} else {
|
elementplus.ElNotification({
|
title: 'Error',
|
message: `${i18n.t('message.AccessIsDenied')}`,
|
position: 'top-right',
|
duration: 5000,
|
type: 'error'
|
});
|
if (WGURL.UseSSO) {
|
window.location.href = `${WGURL.SSOLoginUri}?source=${window.location.href}`;
|
} else {
|
next({path: '/admin/login', replace: true});
|
}
|
|
NProgress.done();
|
return;
|
}
|
}
|
|
NProgress.done();
|
});
|
|
router.afterEach((to, from) => {
|
let frompath = from.path.toLocaleLowerCase();
|
let topath = to.path.toLocaleLowerCase();
|
|
if (topath.startsWith('/admin/') && topath !== '/admin/login') {
|
getAsyncRouters().then((resp) => {
|
|
});
|
}
|
});
|
|
router.onError(error => {
|
console.error(error);
|
});
|
|
async function getAsyncUserWithRolesAndPermissions(userid) {
|
let roles = getLocalStorage("roles");
|
let permissions = getLocalStorage("permissions");
|
|
if (!roles || roles === [] || roles.length === 0 || !permissions || permissions === [] || permissions.length === 0) {
|
const resp = await GetAccountWithRolesAndPermissions(userid);
|
|
let data = resp.data;
|
if (data && data.code === 200) {
|
let user = data.data;
|
let roles = data.data.roles;
|
let permissions = data.data.permissions;
|
|
saveLocalStorage("user", user);
|
saveLocalStorage("roles", roles);
|
saveLocalStorage("permissions", permissions);
|
|
return true;
|
} else {
|
return false;
|
}
|
} else {
|
return true;
|
}
|
}
|
|
async function getAsyncRouters() {
|
if (localstorageMenus && localstorageMenus.length > 0) {
|
initRouter(localstorageMenus);
|
return true;
|
}
|
|
localstorageMenus = getLocalStorage(`${WGURL.routeKey}-menus`);
|
if (localstorageMenus && localstorageMenus.length > 0) {
|
initRouter(localstorageMenus);
|
return true;
|
}
|
|
const resp = await GetMenus({});
|
if (resp && resp.data && resp.data.data && resp.data.data.length > 0) {
|
let menus = [];
|
let allMenus = resp.data.data;
|
let permissions = getLocalStorage('permissions');
|
|
if (permissions && permissions.length > 0) {
|
let paths = permissions.map(p => p.url);
|
allMenus.map(menu => {
|
if (paths.includes(menu.path)) {
|
if (menu.children) {
|
menu.children = menu.children.filter(e => paths.includes(e.path));
|
}
|
menus.push(menu);
|
}
|
});
|
}
|
|
saveLocalStorage(`${WGURL.routeKey}-menus`, menus);
|
|
initRouter(menus);
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
function initRouter(routerdbdata) {
|
let fmtRoutes = formatRoutes(routerdbdata)
|
fmtRoutes.forEach((r) => {
|
router.addRoute(r);
|
});
|
|
localStorage.setItem(`${WGURL.routeKey}-routes`, JSON.stringify(fmtRoutes));
|
}
|
|
function formatRoutes(data) {
|
let fmRoutes = [];
|
if (data != null && data.length > 0) {
|
data.forEach((d) => {
|
let {url, path, name, languagekey, meta, icon, children} = d;
|
if (children && children instanceof Array) {
|
children = formatRoutes(children);
|
}
|
let component = LayoutAdmin;
|
Object.keys(modules).forEach(key => {
|
if (key === '../views' + path + '.vue') {
|
component = modules[key];
|
}
|
});
|
let fmRouter = {
|
url: url, path: path, name: name, meta: {
|
languagekey: languagekey, icon: icon
|
}, children: children, component: component
|
}
|
fmRoutes.push(fmRouter);
|
});
|
}
|
|
return fmRoutes;
|
}
|
|
function checkToken() {
|
token = Cookies.get('Authorization', {domain: WGURL.cookieDomain});
|
if (token == null || token === '' || token === 'null') {
|
return false;
|
}
|
localStorage.setItem('Authorization', token);
|
|
return true;
|
}
|
|
function checkUser() {
|
userid = Cookies.get('userid', {domain: WGURL.cookieDomain});
|
if (userid == null || userid === '' || userid === 'null') {
|
return false;
|
}
|
localStorage.setItem('userid', userid);
|
|
return true;
|
}
|
|
export default router;
|