2
schangxiang@126.com
2025-05-12 6177ed5cb88df34f2a67d9d0610e3e0dc7030e70
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
import { reactive, ref, onUnmounted, provide, inject } from 'vue'
// @ts-ignore
import sdk from 'sdk'
import { ElMessage } from 'element-plus'
import { get, uniqBy } from 'lodash'
import { _t } from '@/libs/Language/Language'
const KEY = 'ORDER_MANAGEMENT_PERMISSION'
const isDev = process.env.NODE_ENV === 'development'
 
export function usePermission(props: any) {
  // 前端页面权限设置
  const node = reactive(props.node || {})
 
  const subs = [
    {
      id: 'Order-tabs-management',
      name: '工单管理Tab',
    },
    {
      id: 'Order-tabs-records',
      name: '工单记录Tab',
    },
    {
      id: 'OrderManagement-actions-add',
      name: '添加',
    },
    {
      id: 'OrderManagement-actions-update',
      name: '编辑',
    },
    {
      id: 'OrderManagement-actions-delete',
      name: '删除',
    },
    {
      id: 'OrderManagement-actions-export',
      name: '导出',
    },
    {
      id: 'OrderManagement-actions-import',
      name: '导入',
    },
    {
      id: 'OrderManagement-actions-download',
      name: '下载模板',
    },
    {
      id: 'OrderManagement-actions-deliver',
      name: '下发',
    },
    {
      id: 'OrderManagement-actions-pause',
      name: '暂停',
    },
    {
      id: 'OrderManagement-actions-finish',
      name: '结束',
    },
    {
      id: 'OrderManagement-actions-sort',
      name: '排序',
    },
    {
      id: 'OrderRecords-actions-export',
      name: '导出',
    },
  ]
 
  const subsType = subs.map((e) => e.id)
 
  const allTabs = ['Order-tabs-management', 'Order-tabs-records']
 
  const permissionCodes = ref<any[]>([])
 
  const showTabs: any = ref([])
 
  // @ts-ignore
  const page = isDev
    ? {
        permissions: [],
      }
    : window.app.current.project?.current.page
  const permission = {
    id: node?.id,
    name: node?.name,
    subs,
  }
  const children = get(
    window.app.current.project?.current.page,
    'body.children',
    []
  )
  const childrenIds = children.map((item: any) => item.id)
  page.permissions = uniqBy(page.permissions, 'id').filter((item: any) =>
    childrenIds.includes(item.id)
  )
 
  if (
    page.permissions.every(
      (item: typeof permission) => item.id !== permission.id
    )
  ) {
    page.permissions.push(permission)
  }
 
  if (childrenIds.includes(permission.id)) {
    const currentPermission = page.permissions.find(
      (item: typeof permission) => item.id === permission.id
    )
    currentPermission && Object.assign(currentPermission, permission)
  }
 
  const initPermission = () => {
    const userInfo = sdk.userInfo
 
    // mock data
    if (isDev) {
      userInfo.permissions = {
        all: false,
        // @ts-ignore
        widgets: subs.map((item) => item.id),
      }
    }
 
    if (userInfo.permissions.all) {
      permissionCodes.value = permission.subs.map((item) => item.id)
      showTabs.value = allTabs
    } else {
      permissionCodes.value = userInfo.permissions.widgets
      permissionCodes.value.forEach((item: any) => {
        if (allTabs.includes(item)) {
          showTabs.value.push(item)
        }
      })
    }
  }
 
  const curTab: any = ref('OrderManagement')
  const changeTab = (tab: string) => {
    curTab.value = tab
  }
 
  const isHasPermission = (code: (typeof subsType)[number]) => {
    if (!permissionCodes.value.includes(code)) {
      ElMessage.warning(_t('用户没有该权限!'))
      return false
    }
    return true
  }
 
  onUnmounted(() => {
    if (node) {
      const index = page.permissions.findIndex((f: any) => f.id === node?.id)
      if (index !== -1) {
        page.permissions.splice(index, 1)
      }
    }
  })
 
  return {
    permissionCodes,
    showTabs,
    initPermission,
    isHasPermission,
    curTab,
    changeTab,
  }
}
 
// @ts-ignore
export function createProvider(props: any): ReturnType<typeof useStore> {
  let value = usePermission(props)
  provide(KEY, value)
  return value
}
 
export function createInjector(): ReturnType<typeof usePermission> {
  return inject(KEY) as ReturnType<typeof usePermission>
}