zs
2025-04-29 02787d77b6c2444e88cbffc3cfa1d762faedb278
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
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'
 
/**
 * 开发环境
 */
const isDev = process.env.NODE_ENV === 'development'
/**
 * 组件子权限
 */
let subsPermissions: Permission[] = []
/**
 * 临时缓存权限
 */
const permissionCodes = ref<string[]>([])
 
/**
 * 校验权限,发出警告
 * @param code
 * @returns
 */
const isPermission = (
  code: (typeof subsPermissions)[number]['id'],
  isHint = true
) => {
  if (!permissionCodes.value.includes(code)) {
    isHint && ElMessage.warning('用户没有该权限!')
    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,
    })
  })
}
 
/**
 * 初始化权限
 * @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 page = isDev
    ? {
        permissions: [],
      }
    : // @ts-ignore
      window.app.current.project?.current.page
  const permission = {
    id: node.value?.id,
    name: node.value?.name,
    subs: subsPermissions,
  }
 
  page.permissions = page.permissions || []
 
  if (
    page.permissions.every(
      (item: typeof permission) => item.id !== permission.id
    )
  ) {
    page.permissions.push(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,
  }
}