import { defineStore } from 'pinia';
|
|
import { getGroupSelector } from '#/api/permission/group';
|
import { getRoleSelector } from '#/api/permission/role';
|
|
interface BaseState {
|
groupList: any[];
|
roleList: any[];
|
}
|
|
/**
|
* 组织权限相关
|
*/
|
export const useOrganizeStore = defineStore('organize', {
|
getters: {},
|
actions: {
|
async getGroupList(): Promise<any[]> {
|
try {
|
if (this.groupList.length > 0) return this.groupList;
|
const res = await getGroupSelector();
|
if (!res) return [];
|
this.groupList = (res.data || []).map((o) => ({ ...o, type: 'group' }));
|
return this.groupList;
|
} catch {
|
return [];
|
}
|
},
|
async getAllRoleList(): Promise<any[]> {
|
try {
|
if (this.roleList.length > 0) return this.roleList;
|
const res = await getRoleSelector();
|
if (!res) return [];
|
this.roleList = (res.data.list || []).map((o) => ({ ...o, roleType: o.type, type: 'role' }));
|
return this.roleList;
|
} catch {
|
return [];
|
}
|
},
|
async getRoleList(data: any = {}): Promise<any[]> {
|
const { type = 'user', isSystem = 0 } = data;
|
// type : user-用户角色 organize-组织角色 position-岗位角色
|
// isSystem : 0-过滤系统角色 1-只有系统角色 2-全部角色
|
try {
|
const list = this.roleList.length ? this.roleList : await this.getAllRoleList();
|
let result = list.filter((o) => o.roleType === type);
|
if (!isSystem) result = result.filter((o) => o.globalMark !== 1);
|
if (isSystem === 1) result = result.filter((o) => o.globalMark === 1);
|
return result;
|
} catch {
|
return [];
|
}
|
},
|
$reset() {
|
this.groupList = [];
|
this.roleList = [];
|
},
|
},
|
state: (): BaseState => ({
|
groupList: [],
|
roleList: [],
|
}),
|
});
|