初始化
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 登录用户 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;
|
||||
},
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user