|
1 | | -import authService from "./authService"; |
2 | | -import type { NASLUserInfo } from "./authService"; |
3 | | -import Global from "../../global"; |
4 | | -import Config from "../../config"; |
| 1 | +import authService from './authService'; |
| 2 | +import type { NASLUserInfo } from './authService'; |
| 3 | +import Global from '../../global'; |
| 4 | +import Config from '../../config'; |
5 | 5 |
|
6 | 6 | function initAuth( |
7 | 7 | options: { |
8 | 8 | allowList?: string[]; |
9 | 9 | router?: any; |
10 | 10 | base?: string; |
| 11 | + configureAuthService?: (service: typeof authService) => void; |
11 | 12 | } = {}, |
12 | 13 | ) { |
| 14 | + // 留一个配置,可以改写authService的实现 |
| 15 | + if (typeof options.configureAuthService === 'function') { |
| 16 | + options.configureAuthService(authService); |
| 17 | + } |
| 18 | + |
13 | 19 | authService.start(); |
14 | 20 | options.allowList = options.allowList || []; |
15 | 21 | const router = options.router; |
16 | | - const base = (options.base || "").replace(/\/$/, ""); |
| 22 | + const base = (options.base || '').replace(/\/$/, ''); |
17 | 23 | /** |
18 | 24 | * 是否有当前路由下的子权限 |
19 | 25 | * 该方法只能在 Global 中调用 |
20 | 26 | * @param {*} subPath 子权限路径,如 /createButton/enabled |
21 | 27 | */ |
22 | 28 | authService.hasSub = function (subPath) { |
23 | 29 | const currentPath = base + router.currentRoute.path; |
24 | | - if (subPath[0] !== "/") subPath = "/" + subPath; |
| 30 | + if (subPath[0] !== '/') subPath = '/' + subPath; |
25 | 31 | return this.has(currentPath + subPath); |
26 | 32 | }; |
27 | 33 | authService.hasFullPath = function (path) { |
28 | | - if (path[0] !== "/") path = "/" + path; |
| 34 | + if (path[0] !== '/') path = '/' + path; |
29 | 35 | return this.has(base + path); |
30 | 36 | }; |
31 | 37 |
|
32 | 38 | /** |
33 | 39 | * 账号与权限中心 |
34 | 40 | */ |
35 | | - Config.globalProperties.set("$auth", authService); |
| 41 | + Config.globalProperties.set('$auth', authService); |
36 | 42 |
|
37 | 43 | return { |
38 | 44 | auth: authService, |
|
0 commit comments