222
schangxiang@126.com
2025-05-06 38b161e4d52362081bfe78fb5b51fbf384db7ce2
HIAWms/web/src/libs/Permission/Permission.ts
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,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,
  }
}