saas-context.ts
2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { acceptHMRUpdate, defineStore } from 'pinia';
import { MOCK_SAAS_TENANTS } from '#/views/food-labeling/shared/mock-platform-data';
/** 当前登录身份(前端 Mock;联调后由后端 JWT / 用户信息注入) */
export type SaasActorType = 'platform' | 'tenant_admin' | 'tenant_user';
interface SaasContextState {
actorType: SaasActorType;
tenantId: string | null;
tenantName: string | null;
/** 当前租户已开通的菜单 key(公司管理员及其下属授权的上限) */
tenantMenuKeys: string[];
}
/**
* SAAS 权限上下文:平台管理员 vs 公司(租户)管理员
*/
export const useSaasContextStore = defineStore('food-saas-context', {
actions: {
/** 模拟以某公司管理员身份进入系统 */
impersonateTenantAdmin(tenantId: string) {
const tenant = MOCK_SAAS_TENANTS.find((t) => t.id === tenantId);
if (!tenant) {
return;
}
this.actorType = 'tenant_admin';
this.tenantId = tenant.id;
this.tenantName = tenant.companyName;
this.tenantMenuKeys = [...tenant.menuPermissionKeys];
},
resetToPlatformAdmin() {
this.actorType = 'platform';
this.tenantId = null;
this.tenantName = null;
this.tenantMenuKeys = [];
},
setTenantMenuKeys(keys: string[]) {
this.tenantMenuKeys = keys;
const tenant = MOCK_SAAS_TENANTS.find((t) => t.id === this.tenantId);
if (tenant) {
tenant.menuPermissionKeys = [...keys];
}
},
},
getters: {
isPlatformAdmin: (state) => state.actorType === 'platform',
isTenantAdmin: (state) => state.actorType === 'tenant_admin',
/** 角色/用户配置菜单权限时的可选上限 */
allowedMenuKeys(state): string[] {
if (state.actorType === 'platform') {
return [];
}
return state.tenantMenuKeys;
},
},
state: (): SaasContextState => ({
actorType: 'platform',
tenantId: null,
tenantName: null,
tenantMenuKeys: [],
}),
});
const hot = import.meta.hot;
if (hot) {
hot.accept(acceptHMRUpdate(useSaasContextStore, hot));
}