Initial commit
This commit is contained in:
85
src/store/modules/tenant.ts
Normal file
85
src/store/modules/tenant.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 租户信息 store
|
||||
*/
|
||||
import { defineStore } from 'pinia';
|
||||
import { formatMenus, toTreeData, formatTreeData } from 'ele-admin-pro';
|
||||
import type { MenuItem } from 'ele-admin-pro';
|
||||
import { USER_MENUS } from '@/config/setting';
|
||||
import { getTenantInfo } from '@/api/layout';
|
||||
import { Tenant } from '@/api/system/tennat/model';
|
||||
import { Company } from '@/api/system/company/model';
|
||||
// const EXTRA_MENUS: any = [];
|
||||
|
||||
export interface UserState {
|
||||
tenant: Tenant | null;
|
||||
company: Company | null;
|
||||
menus: MenuItem[] | null | undefined;
|
||||
}
|
||||
|
||||
export const useTenantStore = defineStore({
|
||||
id: 'tenant',
|
||||
state: (): UserState => ({
|
||||
// 租户信息
|
||||
tenant: null,
|
||||
// 企业信息
|
||||
company: null,
|
||||
// 当前登录用户的菜单
|
||||
menus: null
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
/**
|
||||
* 请求用户信息、权限、角色、菜单
|
||||
*/
|
||||
async fetchTenantInfo() {
|
||||
const company = await getTenantInfo().catch(() => void 0);
|
||||
if (!company) {
|
||||
return {};
|
||||
}
|
||||
// 租户信息
|
||||
this.company = company;
|
||||
// 企业信息
|
||||
if (company) {
|
||||
this.company = company;
|
||||
localStorage.setItem('TenantId', String(company.tenantId));
|
||||
localStorage.setItem('TenantName', String(company.companyName));
|
||||
localStorage.setItem('CompanyId', String(company.companyId));
|
||||
}
|
||||
// 用户菜单, 过滤掉按钮类型并转为 children 形式
|
||||
const { menus, homePath } = formatMenus(
|
||||
USER_MENUS ??
|
||||
toTreeData({
|
||||
data: USER_MENUS,
|
||||
idField: 'menuId',
|
||||
parentIdField: 'parentId'
|
||||
})
|
||||
);
|
||||
this.menus = menus;
|
||||
return { menus, homePath };
|
||||
},
|
||||
/**
|
||||
* 更新租户信息
|
||||
*/
|
||||
setInfo(value: Tenant) {
|
||||
this.tenant = value;
|
||||
},
|
||||
/**
|
||||
* 更新菜单的 badge
|
||||
*/
|
||||
setMenuBadge(path: string, value?: number | string, color?: string) {
|
||||
this.menus = formatTreeData(this.menus, (m) => {
|
||||
if (path === m.path) {
|
||||
return {
|
||||
...m,
|
||||
meta: {
|
||||
...m.meta,
|
||||
badge: value,
|
||||
badgeColor: color
|
||||
}
|
||||
};
|
||||
}
|
||||
return m;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user