forked from gxwebsoft/websoft-cms
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.6 KiB
128 lines
3.6 KiB
/**
|
|
* 登录用户 store
|
|
*/
|
|
import { defineStore } from 'pinia';
|
|
import { formatMenus, toTreeData, formatTreeData } from 'ele-admin-pro/es';
|
|
import type { MenuItemType } from 'ele-admin-pro/es';
|
|
import type { User } from '@/api/system/user/model';
|
|
import { TOKEN_STORE_NAME, USER_MENUS } from '@/config/setting';
|
|
import { getUserInfo } from '@/api/layout';
|
|
import { initialization } from '@/api/system/tenant';
|
|
// import { isExternalLink } from 'ele-admin-pro';
|
|
// import { message } from 'ant-design-vue';
|
|
const EXTRA_MENUS: any = [];
|
|
|
|
export interface UserState {
|
|
info: User | null;
|
|
menus: MenuItemType[] | null | undefined;
|
|
authorities: (string | undefined)[];
|
|
roles: (string | undefined)[];
|
|
}
|
|
|
|
export const useUserStore = defineStore({
|
|
id: 'user',
|
|
state: (): UserState => ({
|
|
// 当前登录用户的信息
|
|
info: null,
|
|
// 当前登录用户的菜单
|
|
menus: null,
|
|
// 当前登录用户的权限
|
|
authorities: [],
|
|
// 当前登录用户的角色
|
|
roles: []
|
|
}),
|
|
getters: {},
|
|
actions: {
|
|
/**
|
|
* 请求用户信息、权限、角色、菜单
|
|
*/
|
|
async fetchUserInfo() {
|
|
const result = await getUserInfo().catch(() => {});
|
|
if (!result) {
|
|
return {};
|
|
}
|
|
// 租户初始化
|
|
if (result.authorities?.length == 0) {
|
|
result.roles?.map((d) => {
|
|
if (d.roleCode === 'superAdmin') {
|
|
// const hide = message.loading('系统正在初始化...', 500);
|
|
initialization(d.roleId).then(() => {
|
|
// hide();
|
|
location.replace('/');
|
|
return;
|
|
});
|
|
}
|
|
});
|
|
}
|
|
// 用户信息
|
|
this.info = result;
|
|
// 缓存租户信息
|
|
localStorage.setItem('TenantId', `${this.info.tenantId}`);
|
|
// 缓存企业信息
|
|
if (this.info.companyInfo) {
|
|
localStorage.setItem(
|
|
'CompanyLogo',
|
|
`${this.info.companyInfo?.companyLogo}`
|
|
);
|
|
localStorage.setItem('PlanId', `${this.info.companyInfo?.planId}`);
|
|
localStorage.setItem(
|
|
'ModulesUrl',
|
|
`${this.info.companyInfo.modulesUrl}`
|
|
);
|
|
}
|
|
// 用户权限
|
|
this.authorities =
|
|
result.authorities
|
|
?.filter((d) => !!d.authority)
|
|
?.map((d) => d.authority) ?? [];
|
|
// 用户角色
|
|
this.roles = result.roles?.map((d) => d.roleCode) ?? [];
|
|
// 获取token
|
|
const token = localStorage.getItem(TOKEN_STORE_NAME);
|
|
// 用户菜单, 过滤掉按钮类型并转为 children 形式
|
|
const { menus, homePath } = formatMenus(
|
|
USER_MENUS ??
|
|
toTreeData({
|
|
data: result.authorities
|
|
?.filter((d) => d.menuType !== 1)
|
|
.map((d) => {
|
|
// 改造子模块的访问路径
|
|
if (d.modulesUrl) {
|
|
d.component = `${d.modulesUrl}${d.path}?token=${token}`;
|
|
}
|
|
return d;
|
|
}),
|
|
idField: 'menuId',
|
|
parentIdField: 'parentId'
|
|
}).concat(EXTRA_MENUS)
|
|
);
|
|
this.menus = menus;
|
|
// console.log('主控菜单', menus);
|
|
return { menus, homePath };
|
|
},
|
|
/**
|
|
* 更新用户信息
|
|
*/
|
|
setInfo(value: User) {
|
|
this.info = 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;
|
|
});
|
|
}
|
|
}
|
|
});
|