Commit f9957e6b9e0782a64b0c8fa194f3e16d63ec5fd6

Authored by ww
1 parent 1eeb7d47

perf: 优化组态权限控制

... ... @@ -4289,7 +4289,8 @@ App.prototype.loadFile = function (id, sameWindow, file, success, force) {
4289 4289 // TODO Thingskit 从服务器请求 load file
4290 4290 async function getRemoteData() {
4291 4291 const { doGetConfigurationContent } = useContentData()
4292   -
  4292 + const { getAuthInfo } = useAuth?.() || {}
  4293 +
4293 4294 const platformInfo = getPlatformInfo()
4294 4295 document.getElementById('first-text-animation-text').querySelector('text').innerHTML = platformInfo?.name || window.PROJECT_ENV?.shortName
4295 4296 document.getElementById('first-text-animation').style.display = 'block'
... ... @@ -4312,6 +4313,21 @@ App.prototype.loadFile = function (id, sameWindow, file, success, force) {
4312 4313 * @property {ConfigurationContentListItemType[]} configurationContentList
4313 4314 */
4314 4315
  4316 + /**
  4317 + * @typedef PermissionInfo
  4318 + * @property {string[]} permissions
  4319 + * @property {string} hasPreview
  4320 + * @property {string} platform
  4321 + * @property {ConfigurationContentListItemType[]} configurationContentList
  4322 + */
  4323 +
  4324 + /**
  4325 + * @type {PermissionInfo}
  4326 + */
  4327 + const permissionInfo = await getAuthInfo?.() || {}
  4328 +
  4329 + this.permissionInfo = permissionInfo
  4330 +
4315 4331 /**
4316 4332 * @type {ConfigurationContentType}
4317 4333 */
... ... @@ -5137,6 +5153,9 @@ App.prototype.addPreviewButton = function () {
5137 5153 switchLightboxMode?.()
5138 5154 }))
5139 5155
  5156 + if (this.permissionInfo && !this.permissionInfo.hasPreviewAuth) this.previewButton.style.display = 'none'
  5157 +
  5158 +
5140 5159 this.buttonContainer.appendChild(this.previewButton)
5141 5160 }
5142 5161 }
... ... @@ -5375,6 +5394,8 @@ App.prototype.addSaveButton = function () {
5375 5394 .funct();
5376 5395 }))
5377 5396
  5397 + if (this.permissionInfo && !this.permissionInfo.hasDesignAuth) this.saveButton.style.display = 'none'
  5398 +
5378 5399 this.buttonContainer.appendChild(this.saveButton)
5379 5400 }
5380 5401 }
... ...
... ... @@ -9,6 +9,7 @@ enum Api {
9 9 REFRESH_TOKEN = '/auth/token',
10 10 GET_DICT = '/dict_item/find',
11 11 UPLOAD_FILE = '/oss/upload',
  12 + GET_PERMISSIONS = '/role/me/permissions',
12 13 }
13 14 export const getPlatformInfo = () => {
14 15 return defHttp.get<PlatformInfo>({
... ... @@ -44,3 +45,9 @@ export function doUploadFile(file: FormData) {
44 45 },
45 46 })
46 47 }
  48 +
  49 +export function getCurrentRolePermission() {
  50 + return defHttp.get<string[]>({
  51 + url: Api.GET_PERMISSIONS,
  52 + })
  53 +}
... ...
... ... @@ -4,6 +4,9 @@ import { useParseParams } from '@/core/LoadData'
4 4 import type { MxCell } from '@/fitCore/types'
5 5 import type { BasicNodeBindType, NodeDataActJsonType, NodeDataDataSourceJsonType, NodeDataEventJsonType, NodeDataType, SaveNodeDataParamsType } from '@/api/node/model'
6 6 import { CellAttributeKeyEnum } from '@/enums/cellAttributeEnum'
  7 +import { useContentDataStoreWithOut } from '@/store/modules/contentData'
  8 +import { useMessage } from '@/hooks/web/useMessage'
  9 +import { MessageEnum } from '@/enums/messageEnum'
7 10
8 11 interface UseNodeDataParamsType {
9 12 cell: MxCell
... ... @@ -29,8 +32,11 @@ function removeCellSourceAttribute(cell: MxCell) {
29 32 }
30 33
31 34 export function useNodeData({ cell, immediate = true }: UseNodeDataParamsType) {
  35 + const { createMessage } = useMessage()
  36 +
32 37 const nodeData = ref<NodeDataType>()
33 38
  39 + const contentDataStore = useContentDataStoreWithOut()
34 40 // const contentId = window.DrawApp.configurationContentId!
35 41 const { configurationId } = useParseParams()
36 42
... ... @@ -65,6 +71,11 @@ export function useNodeData({ cell, immediate = true }: UseNodeDataParamsType) {
65 71 }
66 72
67 73 const saveNodeAllData = async (data: Partial<SaveNodeDataParamsType>) => {
  74 + if (!contentDataStore.hasDesignAuth) {
  75 + createMessage.error(MessageEnum.NO_AUTH)
  76 + throw new Error(MessageEnum.NO_AUTH)
  77 + }
  78 +
68 79 const result = await doSaveNodeAllData(Object.assign(data, basicNodeBindData(ActionType.SAVE)))
69 80 removeCellSourceAttribute(cell)
70 81 nodeData.value = result || getDefaultNodeData()
... ... @@ -72,6 +83,11 @@ export function useNodeData({ cell, immediate = true }: UseNodeDataParamsType) {
72 83 }
73 84
74 85 const saveNodeDataSourceData = async (data: NodeDataDataSourceJsonType) => {
  86 + if (!contentDataStore.hasDesignAuth) {
  87 + createMessage.error(MessageEnum.NO_AUTH)
  88 + throw new Error(MessageEnum.NO_AUTH)
  89 + }
  90 +
75 91 const result = await doSaveNodeDataSource({ ...basicNodeBindData(ActionType.SAVE), data })
76 92 removeCellSourceAttribute(cell)
77 93 if (unref(nodeData)?.dataSourceJson)
... ... @@ -81,12 +97,22 @@ export function useNodeData({ cell, immediate = true }: UseNodeDataParamsType) {
81 97 }
82 98
83 99 const saveNodeEventData = async (data: NodeDataEventJsonType) => {
  100 + if (!contentDataStore.hasDesignAuth) {
  101 + createMessage.error(MessageEnum.NO_AUTH)
  102 + throw new Error(MessageEnum.NO_AUTH)
  103 + }
  104 +
84 105 const result = await doSaveNodeEvent({ ...basicNodeBindData(ActionType.SAVE), data })
85 106 removeCellSourceAttribute(cell)
86 107 return result
87 108 }
88 109
89 110 const saveNodeActData = async (data: NodeDataActJsonType) => {
  111 + if (!contentDataStore.hasDesignAuth) {
  112 + createMessage.error(MessageEnum.NO_AUTH)
  113 + throw new Error(MessageEnum.NO_AUTH)
  114 + }
  115 +
90 116 const result = await doSaveNodeAct({ ...basicNodeBindData(ActionType.SAVE), data })
91 117 removeCellSourceAttribute(cell)
92 118 return result
... ...
... ... @@ -2,3 +2,4 @@ export { usePlatform, getPlatformInfo, setPlatformInfo } from './module/usePlatf
2 2 export { useContentData } from './module/useContentData'
3 3 export { useParseParams } from './module/useParseParams'
4 4 export { useLightboxModeService } from './module/useLightboxModeService'
  5 +export { useAuth } from './module/useAuth'
... ...
  1 +import { getCurrentRolePermission } from '@/api/sys'
  2 +import { useContentDataStoreWithOut } from '@/store/modules/contentData'
  3 +import { isShareMode } from '@/utils/env'
  4 +
  5 +export function useAuth() {
  6 + const contentDataStore = useContentDataStoreWithOut()
  7 + const getAuthInfo = async () => {
  8 + const result = { permissions: [] as string[], hasPreview: true, hasDesign: true }
  9 + if (isShareMode()) return result
  10 +
  11 + const permissions = await getCurrentRolePermission()
  12 + result.permissions = permissions
  13 + const ret = contentDataStore.setPermissions(permissions)
  14 +
  15 + return {
  16 + permissions,
  17 + ...ret,
  18 + }
  19 + }
  20 +
  21 + return {
  22 + getAuthInfo,
  23 + }
  24 +}
  25 +
  26 +window.useAuth = useAuth
... ...
... ... @@ -6,11 +6,14 @@ import { ShareLoginModal } from '@/core/Share'
6 6 import { PageModeEnum } from '@/enums/modeEnum'
7 7 import { useContentDataStoreWithOut } from '@/store/modules/contentData'
8 8 import { useUserStoreWithOut } from '@/store/modules/user'
  9 +import { useMessage } from '@/hooks/web/useMessage'
  10 +import { MessageEnum } from '@/enums/messageEnum'
9 11
10 12 export function useContentData() {
11 13 const contentDataStore = useContentDataStoreWithOut()
12 14 const { mode, publicId, configurationId } = useParseParams()
13 15 const userStore = useUserStoreWithOut()
  16 + const { createMessage } = useMessage()
14 17
15 18 const shareModeBootstrap = async () => {
16 19 if (!publicId) return
... ... @@ -46,6 +49,10 @@ export function useContentData() {
46 49 }
47 50
48 51 const doSaveConfigurationContent = async (params: ConfigurationContentType) => {
  52 + if (!contentDataStore.hasDesignAuth) {
  53 + createMessage.error(MessageEnum.NO_AUTH)
  54 + return
  55 + }
49 56 return saveData(params)
50 57 }
51 58
... ...
  1 +export enum ConfigurationAuthEnum {
  2 + DESIGN = 'api:yt:configuration:center:get_configuration_info:design',
  3 + PREVIEW = 'api:yt:configuration:center:get_configuration_info:preview',
  4 +}
  5 +
  6 +export enum ConfigurationTemplateAuthEnum {
  7 + DESIGN = 'api:yt:configuration:template:center:get_configuration_info:design',
  8 + PREVIEW = 'api:yt:configuration:template:center:get_configuration_info:preview',
  9 +}
... ...
  1 +export enum MessageEnum {
  2 + NO_AUTH = '没有权限!',
  3 +}
... ...
... ... @@ -4,6 +4,8 @@ import { store } from '..'
4 4 import type { NodeDataType } from '@/api/node/model'
5 5 import type { ProductAndDevice } from '@/api/content/model'
6 6 import type { CreateComponentType } from '@/core/Library/types'
  7 +import { isShareMode } from '@/utils/env'
  8 +import { ConfigurationAuthEnum, ConfigurationTemplateAuthEnum } from '@/enums/authEnum'
7 9
8 10 interface DeviceList {
9 11 deviceId: string
... ... @@ -19,6 +21,8 @@ interface ContentDataStoreType {
19 21 configurationContentList?: any
20 22 configurationContentId: Nullable<string>
21 23 diveceDetailMap: Record<string, DeviceList>
  24 + hasDesignAuth?: boolean
  25 + hasPerviewAuth?: boolean
22 26 }
23 27
24 28 export const useContentDataStore = defineStore('app-content-data', {
... ... @@ -30,6 +34,8 @@ export const useContentDataStore = defineStore('app-content-data', {
30 34 configurationContentList: [],
31 35 configurationContentId: null,
32 36 diveceDetailMap: {},
  37 + hasDesignAuth: true,
  38 + hasPerviewAuth: true,
33 39 }),
34 40 actions: {
35 41
... ... @@ -74,6 +80,16 @@ export const useContentDataStore = defineStore('app-content-data', {
74 80 if (!id) return null
75 81 return this.contentData.find(item => item.id === id) || null
76 82 },
  83 +
  84 + setPermissions(string: string[]) {
  85 + if (isShareMode()) return
  86 + this.hasDesignAuth = string.includes((this.isTemplate ? ConfigurationTemplateAuthEnum : ConfigurationAuthEnum).DESIGN)
  87 + this.hasPerviewAuth = string.includes((this.isTemplate ? ConfigurationTemplateAuthEnum : ConfigurationAuthEnum).PREVIEW)
  88 + return {
  89 + hasDesignAuth: toRaw(this.hasDesignAuth),
  90 + hasPreviewAuth: toRaw(this.hasPerviewAuth),
  91 + }
  92 + },
77 93 },
78 94
79 95 getters: {
... ...
... ... @@ -60,6 +60,8 @@ declare global {
60 60 usePlatform: Fn
61 61
62 62 useLightboxModeService: Fn
  63 +
  64 + useAuth: Fn
63 65 }
64 66 }
65 67
... ...