xuruiqian
2025-06-04 17ce21955b4e3402d3d5868b52e50bfdd55bc572
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
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;