Blame view

antis-ncc-admin/src/components/workbench/widget-catalog.js 2.61 KB
e1dcb3a0   “wangming”   1
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
  /**
   * 前端内置工作台组件元数据(后端 lq_workbench_widget 尚未入库时的补充)。
   * 权限:按当前用户菜单是否包含员工档案审核页控制可见性。
   */
  export const LOCAL_WIDGET_CATALOG = [
    {
      code: 'employee-profile-pending',
      name: '员工档案审核',
      icon: 'el-icon-postcard',
      category: '待办审批',
      description: '扫码入职员工档案待审核列表',
      defaultW: 6,
      defaultH: 5,
      minW: 4,
      minH: 4,
      sortCode: 145,
      menuCheck: {
        paths: ['/permission/employee-profile-submission'],
        encodes: ['employeeProfileSubmission'],
        urlParts: ['permission/employee-profile-submission']
      }
    }
  ]
  
  function flattenMenus(menuList) {
    const flat = []
    const walk = (list) => {
      ;(list || []).forEach(m => {
        if (m.urlAddress) flat.push(m)
        if (m.children && m.children.length) walk(m.children)
      })
    }
    walk(menuList)
    return flat
  }
  
  /** 判断当前用户是否拥有指定菜单(与快捷入口组件口径一致)。 */
  export function hasMenuAccess(menuList, check) {
    if (!check) return true
    const flat = flattenMenus(menuList)
    const paths = flat.map(m => (m.urlAddress || '').replace(/^\//, '').toLowerCase())
    const encodes = flat
      .map(m => (m.enCode || m.encode || '').toLowerCase())
      .filter(Boolean)
  
    if (check.urlParts) {
      for (const part of check.urlParts) {
        const key = part.toLowerCase()
        if (paths.some(p => p === key || p.includes(key))) return true
      }
    }
    if (check.encodes) {
      for (const enc of check.encodes) {
        const key = enc.toLowerCase()
        if (encodes.some(e => e.includes(key))) return true
      }
    }
    if (check.paths) {
      for (const path of check.paths) {
        const key = path.replace(/^\//, '').toLowerCase()
        if (paths.some(p => p === key || p.includes(key))) return true
      }
    }
    return false
  }
  
  /** 将本地 catalog 中用户有权限且 API 未返回的组件合并进可添加列表。 */
  export function mergeAvailableWidgets(apiWidgets, menuList) {
    const apiList = Array.isArray(apiWidgets) ? apiWidgets : []
    const codes = new Set(apiList.map(w => w.code))
    const localItems = LOCAL_WIDGET_CATALOG
      .filter(w => !codes.has(w.code))
      .filter(w => hasMenuAccess(menuList, w.menuCheck))
      .map(w => ({
        code: w.code,
        name: w.name,
        icon: w.icon,
        category: w.category,
        description: w.description,
        defaultW: w.defaultW,
        defaultH: w.defaultH,
        minW: w.minW,
        minH: w.minH,
        sortCode: w.sortCode
      }))
  
    return [...apiList, ...localItems].sort((a, b) => (a.sortCode || 0) - (b.sortCode || 0))
  }