zs
2025-05-06 55bf797dcc730b37bc691ebab2b51ff9db8ed245
HIAWms/web/src/libs/Permission/Permission.ts
@@ -10,6 +10,11 @@
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'
/**
 * 开发环境
@@ -24,17 +29,19 @@
 */
const permissionCodes = ref<string[]>([])
const featureMap = ref<Record<string, any>>({})
/**
 * 校验权限,发出警告
 * @param code
 * @returns
 */
const isPermission = (
export const isPermission = (
  code: (typeof subsPermissions)[number]['id'],
  isHint = true
) => {
  if (!permissionCodes.value.includes(code)) {
    isHint && ElMessage.warning('用户没有该权限!')
    isHint && ElMessage.warning(_t('用户没有该权限!'))
    return false
  }
  return true
@@ -73,6 +80,52 @@
  })
}
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
@@ -89,13 +142,17 @@
  permissionCodes.value = []
  setPermissions(permissionMap)
  const node = computed(() => props.node || {})
  const children = get(
    window.app.current.project?.current.page,
    'body.children',
    []
  )
  const page = isDev
    ? {
        permissions: [],
      }
    : // @ts-ignore
      window.app.current.project?.current.page
    : window.app.current.project?.current.page
  const permission = {
    id: node.value?.id,
    name: node.value?.name,
@@ -103,6 +160,10 @@
  }
  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(
@@ -111,6 +172,14 @@
  ) {
    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