websoft-uniapp仓库模板
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.
 
 
 
 
 
 

58 lines
1.4 KiB

/**
* 登录用户 store
*/
import { defineStore } from 'pinia';
import type { User } from '@/uni_modules/ws-common/api/system/user/model';
import { getUserInfo } from '@/uni_modules/ws-common/api/layout';
export interface UserState {
info: User | null;
menus: any[] | 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 {};
}
// 用户信息
this.info = result;
// 用户权限
this.authorities =
result.authorities
?.filter((d) => !!d.authority)
?.map((d) => d.authority) ?? [];
// 用户角色
this.roles = result.roles?.map((d) => d.roleCode) ?? [];
// 用户菜单, 过滤掉按钮类型并转为 children 形式
this.menus = [];
return result;
},
/**
* 更新用户信息
*/
setInfo(value: User) {
this.info = value;
},
}
});