diff --git a/src/composables/permission/index.ts b/src/composables/permission/index.ts new file mode 100644 index 0000000..6c7b0b8 --- /dev/null +++ b/src/composables/permission/index.ts @@ -0,0 +1 @@ +export * from './use-permission'; diff --git a/src/composables/permission/use-permission.ts b/src/composables/permission/use-permission.ts new file mode 100644 index 0000000..d1b13c0 --- /dev/null +++ b/src/composables/permission/use-permission.ts @@ -0,0 +1,15 @@ +import type { Station } from '@/apis'; +import type { PermissionType } from '@/enums'; +import { usePermissionStore } from '@/stores'; + +export const usePermission = () => { + const permissionStore = usePermissionStore(); + + const hasPermission = (stationCode: Station['code'], permissionType: PermissionType) => { + return !!permissionStore.permissions[stationCode]?.includes(permissionType); + }; + + return { + hasPermission, + }; +}; diff --git a/src/constants/store.ts b/src/constants/store.ts index d78ab37..c651ad9 100644 --- a/src/constants/store.ts +++ b/src/constants/store.ts @@ -1,5 +1,6 @@ export const NDM_ALARM_STORE_ID = 'ndm-alarm-store'; export const NDM_DEVICE_STORE_ID = 'ndm-device-store'; +export const NDM_PERMISSION_STORE_ID = 'ndm-permission-store'; export const NDM_POLLIING_STORE_ID = 'ndm-polling-store'; export const NDM_SETTING_STORE_ID = 'ndm-setting-store'; export const NDM_STATION_STORE_ID = 'ndm-station-store'; diff --git a/src/stores/index.ts b/src/stores/index.ts index 080721b..3a2d590 100644 --- a/src/stores/index.ts +++ b/src/stores/index.ts @@ -1,5 +1,6 @@ export * from './alarm'; export * from './device'; +export * from './permission'; export * from './polling'; export * from './setting'; export * from './station'; diff --git a/src/stores/permission.ts b/src/stores/permission.ts new file mode 100644 index 0000000..114ceda --- /dev/null +++ b/src/stores/permission.ts @@ -0,0 +1,73 @@ +import type { NdmPermissionResultVO, Station } from '@/apis'; +import { NDM_PERMISSION_STORE_ID } from '@/constants'; +import { PERMISSION_TYPE_NAMES, type PermissionType } from '@/enums'; +import { useStationStore } from '@/stores'; +import { defineStore } from 'pinia'; +import { computed, ref } from 'vue'; +import { objectEntries } from '@vueuse/core'; + +type Permissions = Record; + +export const usePermissionStore = defineStore( + NDM_PERMISSION_STORE_ID, + () => { + const permRecords = ref([]); + + const permissions = computed(() => { + const stationStore = useStationStore(); + + const result: Permissions = {}; + + // 如果该用户没有任何权限记录,则开放所有权限,否则根据记录配置权限 + if (permRecords.value.length === 0) { + stationStore.stations.forEach((station) => { + result[station.code] = [...objectEntries(PERMISSION_TYPE_NAMES).map(([permType]) => permType)]; + }); + } else { + stationStore.stations.forEach((station) => { + result[station.code] = []; + const stationPermRecords = permRecords.value.filter((record) => record.stationCode === station.code); + if (stationPermRecords.length === 0) return; + stationPermRecords.forEach(({ type: permType }) => { + if (!permType) return; + result[station.code]?.push(permType); + }); + }); + } + + return result; + }); + + // 按权限对车站进行分类 + const stations = computed(() => { + const stationStore = useStationStore(); + const result: Partial> = {}; + // 按原始的车站顺序进行遍历,保持显示顺序不变 + stationStore.stations.forEach((station) => { + const permissionTypes = permissions.value[station.code]; + if (!permissionTypes) return; + permissionTypes.forEach((permissionType) => { + if (!result[permissionType]) result[permissionType] = []; + result[permissionType].push(station); + }); + }); + return result; + }); + + const setPermRecords = (records: NdmPermissionResultVO[]) => { + permRecords.value = records; + }; + + return { + permRecords, + + permissions, + stations, + + setPermRecords, + }; + }, + { + persist: true, + }, +);