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.
hoto-auth-vue3/src/stores/baseData.js

152 lines
4.0 KiB

// | ------------------------------------------------------------
// | @版本: version 0.1
// | @创建人: 【Nie-hotok】
// | @E-mail: x71291@outlook.com
// | @所在项目: hoto-auth-vue3
// | @文件描述: baseData.js -
// | @创建时间: 2024-07-02 22:38
// | @更新时间: 2024-07-02 22:38
// | @修改记录:
// | -*-*-*- (时间--修改人--修改说明) -*-*-*-
// | =
// | ------------------------------------------------------------
import { defineStore } from 'pinia';
import { computed, reactive } from 'vue';
import { CoreDict, CoreEnv, CoreService } from '@/api/index.js';
export const useBaseDataStore = defineStore('baseData', () => {
const state = reactive({
// ! 服务列表
serviceList: [],
// ! 字典列表
dictList: [],
// ! 字典树
dictTree: [],
// ! 字典类型
dictTypeList: [
{
label: '文字',
value: '1',
},
{
label: '图片',
value: '2',
},
],
// ! 环境变量列表
envList: [],
// ! 环境变量树
envTree: [],
// ! 防抖
// 防抖获取服务列表
canGetServiceList: true,
// 防抖获取字典列表
canGetDictList: true,
// 防抖获取env列表
canGetEnvList: true,
});
// 获取服务列表
async function getServiceList(serviceInfo = undefined) {
if (!state.canGetServiceList) return;
state.canGetServiceList = false;
setTimeout(() => {
state.canGetServiceList = true;
}, 2000);
const resd = await CoreService.getService({
...serviceInfo,
isList: true,
});
state.serviceList = resd;
}
// 获取字典列表
const getDictList = async (dictInfo = undefined) => {
if (!state.canGetDictList) return;
state.canGetDictList = false;
setTimeout(() => {
state.canGetDictList = true;
}, 2000);
const resd = await CoreDict.getDict({
...dictInfo,
isList: true,
});
state.dictList = resd;
const [list] = getDictTree(resd);
state.dictTree = list;
};
// ! 获取Env列表
const getEnvList = async (envInfo = undefined) => {
if (!state.canGetEnvList) return;
state.canGetEnvList = false;
setTimeout(() => {
state.canGetEnvList = true;
}, 2000);
const resd = await CoreEnv.getEnv({
...envInfo,
isList: true,
})
state.envList = resd;
const [list] = getEnvTree(resd);
state.envTree = list;
};
return {
state,
getServiceList,
getDictList,
getEnvList
};
});
// ! 格式化字典树
function getDictTree(list, pid = 0) {
const newList = [];
let noList = [];
for (let i in list) {
const obj = list[i];
if (obj.pid == pid) {
newList.push({
meta: { ...obj },
label: obj.dictName,
value: obj.dictId,
id: obj.dictId,
});
} else {
noList.push(obj);
}
}
for (let obj of newList) {
const [children, ls] = getDictTree(noList, obj.id);
obj.children = children;
noList = ls;
}
return [newList, noList];
}
// ! 格式化Env树
function getEnvTree(list, pid = 0) {
const newList = [];
let noList = [];
for (let i in list) {
const obj = list[i];
if (obj.pid == pid) {
newList.push({
meta: { ...obj },
label: obj.envName,
value: obj.envId,
id: obj.envId,
});
} else {
noList.push(obj);
}
}
for (let obj of newList) {
const [children, ls] = getEnvTree(noList, obj.id);
obj.children = children;
noList = ls;
}
return [newList, noList];
}