feat: 添加权限状态管理
This commit is contained in:
1
src/composables/permission/index.ts
Normal file
1
src/composables/permission/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './use-permission';
|
||||
15
src/composables/permission/use-permission.ts
Normal file
15
src/composables/permission/use-permission.ts
Normal file
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -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';
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './alarm';
|
||||
export * from './device';
|
||||
export * from './permission';
|
||||
export * from './polling';
|
||||
export * from './setting';
|
||||
export * from './station';
|
||||
|
||||
73
src/stores/permission.ts
Normal file
73
src/stores/permission.ts
Normal file
@@ -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<Station['code'], PermissionType[]>;
|
||||
|
||||
export const usePermissionStore = defineStore(
|
||||
NDM_PERMISSION_STORE_ID,
|
||||
() => {
|
||||
const permRecords = ref<NdmPermissionResultVO[]>([]);
|
||||
|
||||
const permissions = computed<Permissions>(() => {
|
||||
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<Record<PermissionType, Station[]>> = {};
|
||||
// 按原始的车站顺序进行遍历,保持显示顺序不变
|
||||
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,
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user