Skip to content

Commit 6629b41

Browse files
committed
perf: 优化获取权限逻辑
1 parent 5af23aa commit 6629b41

24 files changed

Lines changed: 92 additions & 115 deletions

File tree

‎src/api/routes.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { http } from "@/utils/http";
33
type Result = {
44
success: boolean;
55
data: Array<any>;
6+
auths: Array<any>;
67
};
78

89
export const getAsyncRoutes = () => {

‎src/components/RePlusCRUD/src/components/wangEditor.vue‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { message } from "@/utils/message";
77
import { formatBytes, getKeyList } from "@pureadmin/utils";
88
import { useI18n } from "vue-i18n";
99
import { IEditorConfig } from "@wangeditor/editor";
10-
import { hasAuth, hasGlobalAuth } from "@/router/utils";
10+
import { hasAuth } from "@/router/utils";
1111
1212
const messages = defineModel<string | object | any>();
1313
const editorRef = shallowRef();
@@ -62,10 +62,7 @@ const editorConfig: Partial<IEditorConfig> = {
6262
attachment: { menuKeys: [] }
6363
}
6464
};
65-
if (
66-
hasGlobalAuth("config:systemUploadFile") &&
67-
hasGlobalAuth("upload:systemUploadFile")
68-
) {
65+
if (hasAuth("config:systemUploadFile") && hasAuth("upload:systemUploadFile")) {
6966
toolbarConfig.insertKeys.keys = ["uploadAttachment"]; // “上传附件”菜单
7067
editorConfig.hoverbarKeys.attachment = {
7168
menuKeys: ["downloadAttachment"] // “下载附件”菜单

‎src/directives/auth/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { hasAuth } from "@/router/utils";
22
import type { Directive, DirectiveBinding } from "vue";
33

44
export const auth: Directive = {
5-
mounted(el: HTMLElement, binding: DirectiveBinding<string | Array<string>>) {
5+
mounted(el: HTMLElement, binding: DirectiveBinding<string>) {
66
const { value } = binding;
77
if (value) {
88
if (!hasAuth(value)) {

‎src/router/utils.ts‎

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
cloneDeep,
1313
intersection,
1414
isAllEmpty,
15-
isIncludeAllChildren,
16-
isString,
1715
storageLocal
1816
} from "@pureadmin/utils";
1917
import { getConfig } from "@/config";
@@ -152,7 +150,8 @@ function addPathMatch() {
152150
}
153151

154152
/** 处理动态路由(后端返回的路由) */
155-
function handleAsyncRoutes(routeList) {
153+
function handleAsyncRoutes(routeList, authList) {
154+
usePermissionStoreHook().handleWholeAuths(authList);
156155
if (routeList.length === 0) {
157156
usePermissionStoreHook().handleWholeMenus(routeList);
158157
} else {
@@ -203,25 +202,28 @@ function initRouter() {
203202
if (getConfig()?.CachingAsyncRoutes) {
204203
// 开启动态路由缓存本地localStorage
205204
const key = "async-routes";
205+
const authKey = "async-auths";
206206
const asyncRouteList = storageLocal().getItem(key) as any;
207+
const asyncAuthList = storageLocal().getItem(authKey) as any;
207208
if (asyncRouteList && asyncRouteList?.length > 0) {
208209
return new Promise(resolve => {
209-
handleAsyncRoutes(asyncRouteList);
210+
handleAsyncRoutes(asyncRouteList, asyncAuthList);
210211
resolve(router);
211212
});
212213
} else {
213214
return new Promise(resolve => {
214-
getAsyncRoutes().then(({ data }) => {
215-
handleAsyncRoutes(cloneDeep(data));
215+
getAsyncRoutes().then(({ data, auths }) => {
216+
handleAsyncRoutes(cloneDeep(data), auths);
216217
storageLocal().setItem(key, data);
218+
storageLocal().setItem(authKey, auths);
217219
resolve(router);
218220
});
219221
});
220222
}
221223
} else {
222224
return new Promise(resolve => {
223-
getAsyncRoutes().then(({ data }) => {
224-
handleAsyncRoutes(cloneDeep(data));
225+
getAsyncRoutes().then(({ data, auths }) => {
226+
handleAsyncRoutes(cloneDeep(data), auths);
225227
resolve(router);
226228
});
227229
});
@@ -365,26 +367,24 @@ function getAuths(): Array<string> {
365367
}
366368

367369
/** 是否有按钮级别的权限 */
368-
function hasAuth(value: string | Array<string>): boolean {
369-
if (!value) return false;
370-
/** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */
371-
const metaAuths = getAuths();
372-
if (!metaAuths) return false;
373-
const isAuths = isString(value)
374-
? metaAuths.includes(value)
375-
: isIncludeAllChildren(value, metaAuths);
376-
return isAuths ? true : false;
377-
}
370+
// function hasAuth(value: string | Array<string>): boolean {
371+
// if (!value) return false;
372+
// /** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */
373+
// const metaAuths = getAuths();
374+
// if (!metaAuths) return false;
375+
// const isAuths = isString(value)
376+
// ? metaAuths.includes(value)
377+
// : isIncludeAllChildren(value, metaAuths);
378+
// return isAuths ? true : false;
379+
// }
378380

379381
/** 是否有按钮级别的权限 */
380-
function hasGlobalAuth(value: string | Array<string>): boolean {
382+
function hasAuth(value: string): boolean {
381383
if (!value) return false;
382384
/** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */
383-
const metaAuths = usePermissionStoreHook().metaAuths;
384-
if (!metaAuths) return false;
385-
return isString(value)
386-
? metaAuths.includes(value)
387-
: isIncludeAllChildren(value, metaAuths);
385+
const permissionAuths = usePermissionStoreHook().permissionAuths;
386+
if (!permissionAuths) return false;
387+
return permissionAuths[value];
388388
}
389389

390390
/** 获取所有菜单中的第一个菜单(顶级菜单)*/
@@ -420,7 +420,6 @@ export {
420420
getTopMenu,
421421
addPathMatch,
422422
isOneOfArray,
423-
hasGlobalAuth,
424423
getHistoryMode,
425424
addAsyncRoutes,
426425
getParentPaths,

‎src/store/modules/permission.ts‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@ import {
1212
} from "../utils";
1313
import { useMultiTagsStoreHook } from "./multiTags";
1414

15-
function getGlobalAuths(arr: any[]) {
16-
let res = [];
17-
18-
function deep(arr: any[]) {
19-
arr.forEach(item => {
20-
let auths = item?.meta?.auths;
21-
if (auths?.length > 0) {
22-
res = res.concat(auths);
23-
}
24-
if (item.children) {
25-
deep(item.children);
26-
}
27-
});
28-
}
29-
30-
deep(arr);
31-
return res;
32-
}
33-
3415
export const usePermissionStore = defineStore({
3516
id: "pure-permission",
3617
state: () => ({
@@ -43,9 +24,15 @@ export const usePermissionStore = defineStore({
4324
// 缓存页面keepAlive
4425
cachePageList: [],
4526
// 全局的授权
46-
metaAuths: []
27+
permissionAuths: []
4728
}),
4829
actions: {
30+
handleWholeAuths(auths: any[]) {
31+
this.permissionAuths = {};
32+
auths.forEach(auth => {
33+
this.permissionAuths[auth] = true;
34+
});
35+
},
4936
/** 组装整体路由生成的菜单 */
5037
handleWholeMenus(routes: any[]) {
5138
this.wholeMenus = filterNoPermissionTree(
@@ -54,7 +41,6 @@ export const usePermissionStore = defineStore({
5441
this.flatteningRoutes = formatFlatteningRoutes(
5542
this.constantMenus.concat(routes)
5643
);
57-
this.metaAuths = getGlobalAuths(this.constantMenus.concat(routes));
5844
},
5945
cacheOperate({ mode, name }: cacheType) {
6046
const delIndex = this.cachePageList.findIndex(v => v === name);

‎src/views/account/components/AccountManagement.vue‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { computed } from "vue";
33
import { deviceDetection } from "@pureadmin/utils";
44
import { useAccountManage } from "../utils/hook";
5-
import { hasGlobalAuth } from "@/router/utils";
5+
import { hasAuth } from "@/router/utils";
66
77
defineOptions({
88
name: "AccountManagement"
@@ -14,23 +14,23 @@ const list = computed(() => [
1414
{
1515
name: "password",
1616
title: t("account.password"),
17-
button: hasGlobalAuth("reset:UserInfo") && t("buttons.update")
17+
button: hasAuth("reset:UserInfo") && t("buttons.update")
1818
},
1919
{
2020
name: "phone",
2121
title: t("userinfo.phone"),
2222
illustrate: userinfoStore.email
2323
? `${t("account.bind")}:${userinfoStore.phone}`
2424
: t("account.unbound"),
25-
button: hasGlobalAuth("bind:UserInfo") && t("buttons.update")
25+
button: hasAuth("bind:UserInfo") && t("buttons.update")
2626
},
2727
{
2828
name: "email",
2929
title: t("userinfo.email"),
3030
illustrate: userinfoStore.email
3131
? `${t("account.bind")}:${userinfoStore.email}`
3232
: t("account.unbound"),
33-
button: hasGlobalAuth("bind:UserInfo") && t("buttons.update")
33+
button: hasAuth("bind:UserInfo") && t("buttons.update")
3434
}
3535
]);
3636

‎src/views/account/utils/hook.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
delay,
66
deviceDetection
77
} from "@pureadmin/utils";
8-
import { hasGlobalAuth } from "@/router/utils";
8+
import { hasAuth } from "@/router/utils";
99
import { message } from "@/utils/message";
1010
import { computed, h, onMounted, reactive, type Ref, ref, toRaw } from "vue";
1111
import { userInfoApi } from "@/api/user/userinfo";
@@ -39,10 +39,10 @@ export function useApiAuth() {
3939
});
4040

4141
const auth = reactive({
42-
upload: hasGlobalAuth("upload:UserInfo"),
43-
update: hasGlobalAuth("update:UserInfo"),
44-
bind: hasGlobalAuth("bind:UserInfo"),
45-
reset: hasGlobalAuth("reset:UserInfo")
42+
upload: hasAuth("upload:UserInfo"),
43+
update: hasAuth("update:UserInfo"),
44+
bind: hasAuth("bind:UserInfo"),
45+
reset: hasAuth("reset:UserInfo")
4646
});
4747
return {
4848
api,
@@ -191,7 +191,7 @@ export function useUserLoginLog() {
191191
});
192192

193193
const auth = reactive({
194-
list: hasGlobalAuth("list:userLoginLog")
194+
list: hasAuth("list:userLoginLog")
195195
});
196196

197197
const { tagStyle } = usePublicHooks();

‎src/views/system/components/searchDept.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts" setup>
22
import { h, reactive } from "vue";
3-
import { hasGlobalAuth } from "@/router/utils";
3+
import { hasAuth } from "@/router/utils";
44
import { searchDeptApi } from "@/api/system/search";
55
import RePlusSearch from "@/components/RePlusSearch";
66
import { ElImage } from "element-plus";
@@ -32,7 +32,7 @@ const baseColumnsFormat = ({ listColumns }) => {
3232

3333
<template>
3434
<RePlusSearch
35-
v-if="hasGlobalAuth('list:systemSearchDept')"
35+
v-if="hasAuth('list:systemSearchDept')"
3636
v-model="selectValue"
3737
locale-name="systemDept"
3838
:baseColumnsFormat="baseColumnsFormat"

‎src/views/system/components/searchMenu.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts" setup>
22
import { h, reactive } from "vue";
3-
import { hasGlobalAuth } from "@/router/utils";
3+
import { hasAuth } from "@/router/utils";
44
import { searchMenuApi } from "@/api/system/search";
55
import RePlusSearch from "@/components/RePlusSearch";
66
import { transformI18n } from "@/plugins/i18n";
@@ -32,7 +32,7 @@ const baseColumnsFormat = ({ listColumns }) => {
3232

3333
<template>
3434
<RePlusSearch
35-
v-if="hasGlobalAuth('list:systemSearchMenu')"
35+
v-if="hasAuth('list:systemSearchMenu')"
3636
v-model="selectValue"
3737
locale-name="systemMenu"
3838
:baseColumnsFormat="baseColumnsFormat"

‎src/views/system/components/searchRole.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts" setup>
22
import { reactive } from "vue";
3-
import { hasGlobalAuth } from "@/router/utils";
3+
import { hasAuth } from "@/router/utils";
44
import { searchRoleApi } from "@/api/system/search";
55
import RePlusSearch from "@/components/RePlusSearch";
66
@@ -17,7 +17,7 @@ const api = reactive(searchRoleApi);
1717

1818
<template>
1919
<RePlusSearch
20-
v-if="hasGlobalAuth('list:systemSearchRole')"
20+
v-if="hasAuth('list:systemSearchRole')"
2121
v-model="selectValue"
2222
locale-name="systemRole"
2323
:api="api"

0 commit comments

Comments
 (0)