import type { SysConfigInfo } from '@vben/types';
|
|
import { merge } from '@jnpf/utils';
|
|
import { defineStore } from 'pinia';
|
|
import { getDictionaryAll } from '#/api/systemData/dictionary';
|
|
interface DicChildItem {
|
isTree?: number;
|
id: string;
|
enCode: string;
|
fullName: string;
|
}
|
interface DicItem extends DicChildItem {
|
dictionaryList: DicChildItem[];
|
}
|
interface BaseState {
|
// 系统配置
|
sysConfigInfo: Partial<SysConfigInfo>;
|
dictionaryList: DicItem[];
|
}
|
|
/**
|
* 系统配置相关
|
*/
|
export const useBaseStore = defineStore('base', {
|
getters: {
|
getDicList(): DicItem[] {
|
return this.dictionaryList;
|
},
|
getSysConfig(): Partial<SysConfigInfo> {
|
return this.sysConfigInfo;
|
},
|
},
|
actions: {
|
setSysConfig(config: Partial<SysConfigInfo>) {
|
this.sysConfigInfo = merge(config as any, this.sysConfigInfo || {});
|
},
|
setDictionaryList(list: DicItem[] = []) {
|
this.dictionaryList = list;
|
},
|
async getDictionaryAll(): Promise<DicItem[]> {
|
try {
|
if (this.dictionaryList.length > 0) {
|
return this.dictionaryList;
|
} else {
|
const res = await getDictionaryAll();
|
if (!res) return [];
|
this.dictionaryList = res.data.list;
|
return res.data.list;
|
}
|
} catch {
|
return [];
|
}
|
},
|
async getDictionaryData(encode: string, id: string = ''): Promise<DicChildItem | DicChildItem[]> {
|
try {
|
let data: Partial<DicItem> = {};
|
let json: DicChildItem | DicChildItem[] = [];
|
let list: DicItem[] = [];
|
list = this.dictionaryList.length === 0 ? await this.getDictionaryAll() : this.dictionaryList;
|
if (encode) {
|
const arr = list.filter((o) => o.enCode === encode);
|
if (arr.length === 0) return [];
|
data = arr[0] as DicItem;
|
if (id) {
|
let rowData: DicChildItem[] = [];
|
if (data.isTree) {
|
function findData(list) {
|
for (const e of list) {
|
if (e.id === id) {
|
rowData[0] = e;
|
break;
|
}
|
if (e.children && e.children.length > 0) {
|
findData(e.children);
|
}
|
}
|
}
|
findData(data.dictionaryList);
|
} else {
|
rowData = (data.dictionaryList as DicChildItem[]).filter((o) => o.id === id);
|
}
|
json =
|
rowData.length > 0
|
? (rowData[0] as DicItem)
|
: {
|
id: '',
|
fullName: '',
|
enCode: '',
|
};
|
} else {
|
json = data.dictionaryList as DicChildItem[];
|
}
|
return json;
|
}
|
return json;
|
} catch {
|
return [];
|
}
|
},
|
async getDicDataSelector(value: string, key: string = 'id'): Promise<DicChildItem[]> {
|
try {
|
let data: Partial<DicItem> = {};
|
let json: DicChildItem[] = [];
|
let list: DicItem[] = [];
|
list = this.dictionaryList.length === 0 ? await this.getDictionaryAll() : this.dictionaryList;
|
if (!value) return [];
|
const arr = list.filter((o) => {
|
return o[key] === value;
|
});
|
if (arr.length === 0) return [];
|
data = arr[0] as DicItem;
|
json = data.dictionaryList as DicChildItem[];
|
return json;
|
} catch {
|
return [];
|
}
|
},
|
$reset() {
|
this.dictionaryList = [];
|
},
|
},
|
persist: {
|
// 持久化
|
pick: ['dictionaryList', 'sysConfigInfo'],
|
},
|
state: (): BaseState => ({
|
dictionaryList: [],
|
sysConfigInfo: {},
|
}),
|
});
|