22
schangxiang@126.com
2025-05-20 5b189017d143be6366f56ffcdd3c3699a381e034
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
import { reactive, ref, onUnmounted } from 'vue'
import sdk from 'sdk'
import { ElMessage } from 'element-plus'
import { _t } from '../app'
import { get } from 'lodash'
 
const subs = [
  {
    id: 'DefectList',
    name: '缺陷清单',
  },
  {
    id: 'ToDoUnqualified',
    name: '待办不良品',
  },
  {
    id: 'JudgmentRecord',
    name: '产品判定记录',
  },
  {
    id: 'ToDoUnqualified-judgment',
    name: '待办不良品-判定',
  },
  {
    id: 'ToDoUnqualified-configuration',
    name: '待办不良品-配置',
  },
  {
    id: 'JudgmentRecord-export',
    name: '产品判定记录-导出',
  },
]
const subsType = subs.map((e) => e.id)
export const permissionCodes = ref<any[]>([])
export const initPermission = (props: any) => {
  // // 前端页面权限设置
  const node = reactive(props.node)
 
  let permissions =
    process.env.NODE_ENV === 'development'
      ? []
      : window.app.current.project?.current.page?.permissions
  const permission = {
    id: node?.id,
    name: node?.name,
    subs,
  }
  const children = get(
    window.app.current.project?.current.page,
    'body.children',
    []
  )
  // permissions.push(permission)
  const childrenIds = children.map((item: any) => item.id)
  permissions = permissions.filter((item: any) => childrenIds.includes(item.id))
 
  if (
    permissions.every((item: typeof permission) => item.id !== permission.id)
  ) {
    permissions.push(permission)
  }
 
  if (childrenIds.includes(permission.id)) {
    const currentPermission = permissions.find(
      (item: typeof permission) => item.id === permission.id
    )
    currentPermission && Object.assign(currentPermission, permission)
  }
  const userInfo = sdk.userInfo
 
  // mock data
  if (process.env.NODE_ENV === 'development') {
    userInfo.permissions = {
      all: true,
      widgets: [],
    }
  }
 
  if (userInfo.permissions.all) {
    permissionCodes.value = permission.subs.map((item) => item.id)
  } else {
    permissionCodes.value = userInfo.permissions.widgets
  }
 
  onUnmounted(() => {
    const index = permissions.findIndex((f: any) => f.id === node?.id)
    if (index !== -1) {
      permissions.splice(index, 1)
    }
  })
}
 
export const isHasPermission = (code: (typeof subsType)[number]) => {
  if (!permissionCodes.value.includes(code)) {
    ElMessage.warning(_t('用户没有该权限!'))
    return false
  }
  return true
}