import { defineStore } from 'pinia'; import { getDictionaryDataAll, getGroupSelector, getRoleSelector, } from '@/api/common' export const useBaseStore = defineStore({ id: ' app-base', state: () => ({ dictionaryList: [], groupList: [], roleList: [], relationData: {}, }), getters: { getDictionaryList() { return this.dictionaryList }, getRelationData() { return this.relationData }, }, actions: { setDictionaryList(dictionaryList) { this.dictionaryList = dictionaryList || [] }, setGroupList(groupList) { this.groupList = groupList }, setRoleList(roleList) { this.roleList = roleList }, updateRelationData(val) { this.relationData = val }, getDictionaryDataAll() { return new Promise((resolve, reject) => { if (this.dictionaryList.length) { resolve(this.dictionaryList) } else { getDictionaryDataAll().then(res => { this.setDictionaryList(res.data.list) resolve(res.data.list) }).catch(error => { reject(error) }) } }) }, getDictionaryData(info) { return new Promise(async resolve => { let list = [], data = [], json = [] if (!this.dictionaryList.length) { list = await this.getDictionaryDataAll() } else { list = this.dictionaryList } if (info.sort) { data = list.filter(o => o.enCode === info.sort)[0] if (!info.id) { json = data?.dictionaryList || [] } else { let rowData = []; if (!data.isTree) { rowData = data.dictionaryList.fliter(o => o.id == info.id) } else { const findData = list => { for (let i = 0; i < list.length; i++) { const e = list[i]; if (e.id == info.id) { rowData[0] = e break } if (e.children && e.children.length) { findData(e.children) } } } findData(data.dictionaryList) } if (rowData.length) { json = rowData[0] } else { json = { id: "", fullName: "" }; } } } resolve(json) }) }, getDicDataSelector(value, key = 'id') { return new Promise(async resolve => { let list = [], data = {}, json = []; if (!this.dictionaryList.length) { list = await this.getDictionaryDataAll() } else { list = this.dictionaryList } if (!value) return resolve([]) let arr = list.filter(o => o[key] === value); if (!arr.length) return resolve([]) data = arr[0]; json = data.dictionaryList; resolve(json) }) }, getGroupList() { return new Promise((resolve, reject) => { if (!this.groupList.length) { getGroupSelector().then(res => { this.setGroupList(res.data) resolve(res.data) }).catch(error => { reject(error) }) } else { resolve(this.groupList) } }) }, getRoleList() { return new Promise((resolve, reject) => { if (!this.roleList.length) { getRoleSelector().then(res => { this.setRoleList(res.data.list) resolve(res.data.list) }).catch(error => { reject(error) }) } else { resolve(this.roleList) } }) }, }, });