Browse Source

优化 getTableButton 方法

huangbin 4 years ago
parent
commit
8044cc69f5
1 changed files with 37 additions and 22 deletions
  1. 37 22
      src/common/setup/table/button.ts

+ 37 - 22
src/common/setup/table/button.ts

@@ -1,12 +1,11 @@
 import { BtnClassName, BtnListType } from '@/common/components/btnList/interface';
 import { EnumRouterName } from '@/common/constants/enumRouterName';
-import { ModalName } from '@/common/constants/modalName';
 import { OperationTabMenu, OperationTabMenuAuth } from '@/services/go/commonService/interface';
 import { sessionStorageUtil } from "@/utils/storage";
 import { inject, ref, Ref, toRaw, unref } from 'vue';
+import { useRoute } from 'vue-router';
 import { openModal } from "../modal";
 import { ButtonListKey } from './interface';
-import { useRoute } from 'vue-router';
 
 /**
  * 获取class 名
@@ -265,28 +264,44 @@ export function getButtonList(menuType: EnumRouterName, hasDetail: boolean) {
 /**
  * 根据code权限获取按钮列表
  */
-export function getTableButton(codes: string[]): BtnListType[] {
+export function getTableButton(codes: string[] = []): BtnListType[] {
     const route = useRoute();
     const auth = route.meta.auth as OperationTabMenuAuth[];
 
-    // 根据code权限获取按钮列表
-    return codes.reduce((result, code) => {
-        if (code === 'detail') {
-            result.push({
-                lable: '详情',
-                code: 'detail',
-                className: getClassName(''),
-            });
-        } else {
-            const item = auth.find((val) => val.code === code);
-            if (item) {
-                result.push({
-                    lable: item.label,
-                    code: item.code,
-                    className: getClassName(item.code),
-                });
+    // 标准化 按钮 数据
+    const useBtn = (lable: string, code: string, className: string) => {
+        return { lable, code, className: getClassName(className) }
+    }
+
+    if (codes.length === 0) {
+        // 不传入 code 参数,默认返回全部按钮数据
+        return auth.map(el => {
+            return useBtn(el.label, el.code, el.code)
+        })
+    } else {
+        // 转 map 已空间换时间
+        const authMap = new Map<string, OperationTabMenuAuth>()
+        auth.forEach(el => {
+            const { code } = el
+            if (authMap.has(code)) {
+                console.warn('按钮权限列表配置重复的code,请开发人员查看!!!')
+            } else {
+                authMap.set(code, el)
             }
-        }
-        return result;
-    }, [] as BtnListType[]);
+        })
+        // 根据 传入的 code 返回 code对应的配置数据
+        return codes.reduce((result, code) => {
+            // 详情 特殊处理,因为详情界面未配置权限,需要手动处理
+            if (code === 'detail') {
+                result.push(useBtn('详情', 'detail', ''))
+            } else {
+                const item = authMap.get(code)
+                if (item) {
+                    const { code, label } = item
+                    result.push(useBtn(label, code, code))
+                }
+            }
+            return result
+        }, [] as BtnListType[])
+    }
 }