| | |
| | | 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 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 |
| | |
| | | }) |
| | | } |
| | | |
| | | 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 |
| | |
| | | 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, |
| | |
| | | } |
| | | |
| | | 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( |
| | |
| | | ) { |
| | | 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 |