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
// src/directives/model-permission.js
 
export default {
  inserted(el, binding) {
    // 从sessionStorage获取设备型号
    let publicConfig = sessionStorage.getItem('publicConfig')
    let { model } = publicConfig ? JSON.parse(publicConfig) : {}
    const { value } = binding
    
    if (!value) return // 没有配置权限,默认显示
    
    if (Array.isArray(value)) {
      // 允许的型号列表
      if (!value.includes(model)) {
        el.parentNode?.removeChild(el)
      }
    } else if (typeof value === 'string') {
      // 单个允许的型号
      if (model !== value) {
        el.parentNode?.removeChild(el)
      }
    } else if (typeof value === 'object') {
      // 复杂配置:{ allow: [], deny: [] }
      const { allow, deny } = value
      
      if (allow && !allow.includes(model)) {
        el.parentNode?.removeChild(el)
      }
      if (deny && deny.includes(model)) {
        el.parentNode?.removeChild(el)
      }
    }
  },
  
  // update(el, binding) {
  //   const { value, oldValue } = binding
    
  //   if (value !== oldValue) {
  //     const elClone = el.cloneNode(true)
  //     el.parentNode?.replaceChild(elClone, el)
  //     this.inserted(elClone, binding)
  //   }
  // }
}