From 671b429b3c000eccf05910452505562f06defa73 Mon Sep 17 00:00:00 2001
From: schangxiang@126.com <schangxiang@126.com>
Date: 周二, 06 5月 2025 07:54:56 +0800
Subject: [PATCH] 222
---
HIAWms/web/src/libs/Permission/Permission.ts | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 206 insertions(+), 0 deletions(-)
diff --git a/HIAWms/web/src/libs/Permission/Permission.ts b/HIAWms/web/src/libs/Permission/Permission.ts
new file mode 100644
index 0000000..9417df6
--- /dev/null
+++ b/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,
+ }
+}
--
Gitblit v1.9.3