zs
2025-06-04 5a149d626ae8bc3fa4bddbb53f8caf40f51f6da6
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
  reactive,
  ref,
  onUnmounted,
  VNode,
  DirectiveBinding,
  Directive,
  computed,
} from 'vue'
import { ElMessage } from 'element-plus'
import sdk from 'sdk'
import { Permission, UserInfo, Props } from './Permission.d'
import { useGlobalState } from '../Store/Store'
 
import { get } from 'lodash'
import { editionMap } from '../enum'
import { _t } from '../Language/Language'
 
/**
 * 开发环境
 */
const isDev = process.env.NODE_ENV === 'development'
/**
 * 组件子权限
 */
let subsPermissions: Permission[] = []
/**
 * 临时缓存权限
 */
const permissionCodes = ref<string[]>([])
 
const featureMap = ref<Record<string, any>>({})
 
/**
 * 校验权限,发出警告
 * @param code
 * @returns
 */
export const isPermission = (
  code: (typeof subsPermissions)[number]['id'],
  isHint = true
) => {
  if (!permissionCodes.value.includes(code)) {
    isHint && ElMessage.warning(_t('用户没有该权限!'))
    return false
  }
  return true
}
 
export const vPermission: { [key: string]: Directive } = {
  created(
    el: HTMLElement,
    binding: DirectiveBinding,
    vNode: VNode,
    prevVNode: VNode
  ) {
    el.addEventListener(
      'click',
      (event: Event) => {
        if (!isPermission(binding.value)) {
          event.stopPropagation()
          return false
        }
      },
      true
    )
  },
}
 
/**
 * 设置权限
 * @param permissionMap
 */
const setPermissions = (permissionMap: Record<string, string>) => {
  Object.entries(permissionMap).forEach(([key, value]) => {
    subsPermissions.push({
      id: key,
      name: value,
    })
  })
}
 
export const vEditionShow: { [key: string]: Directive } = {
  updated(el: HTMLElement, binding: DirectiveBinding<any>) {
    if (el) {
      const state: Record<string, any> = useGlobalState()
      const l = Object.keys(state.featureMap.state.value)?.length
      const featureKeys = binding.value
        .split(',')
        .filter((v) => v)
        .map((v: string) => {
          return editionMap[v]
        })
      if (!isEdition(featureKeys) && l) {
        el.remove()
      }
    }
  },
}
 
/**
 * 判断是否有该功能
 * @param feature
 * @returns
 */
export const isEdition = (features: string[], condition = true) => {
  const state: Record<string, any> = useGlobalState()
  const l = Object.keys(state.featureMap.state.value)?.length
  if (l) {
    features = features.filter((v) => v)
    if (features.length === 0) return true
    let newFeatures = features
    if (features[0].length < 3) {
      newFeatures = features.map((v) => {
        return editionMap[v]
      })
    }
 
    if (!Object.keys(featureMap.value).length) {
      featureMap.value = state.featureMap.state.value
    }
    const check = newFeatures.every((key: string) => {
      return featureMap?.value?.[key]?.value
    })
    return check && condition
  }
}
 
/**
 * 初始化权限
 * @param props
 * @param permissionMap
 * @example usePermission(props, {
 *    'user-add': '新增用户',
 *  })
 */
export const usePermission = (
  props: Props | any,
  permissionMap: Record<string, string>
) => {
  subsPermissions = []
  permissionCodes.value = []
  setPermissions(permissionMap)
  const node = computed(() => props.node || {})
  const children = get(
    window.app.current.project?.current.page,
    'body.children',
    []
  )
 
  const page = isDev
    ? {
        permissions: [],
      }
    : window.app.current.project?.current.page
  const permission = {
    id: node.value?.id,
    name: node.value?.name,
    subs: subsPermissions,
  }
 
  page.permissions = page.permissions || []
  const childrenIds = children.map((item: any) => item.id)
  page.permissions = page.permissions.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 userInfo: UserInfo = isDev
    ? { permissions: { all: true, widgets: [] } }
    : sdk.userInfo
 
  const { all, widgets } = userInfo.permissions
  permissionCodes.value = all
    ? permission.subs.map((item: Permission) => item.id)
    : widgets
 
  onUnmounted(() => {
    if (node.value) {
      const index = page.permissions.findIndex(
        (f: any) => f.id === node.value?.id
      )
      if (index !== -1) {
        page.permissions.splice(index, 1)
      }
    }
  })
 
  return {
    isPermission,
  }
}