import { defineStore } from 'pinia'; import { RESULT_CODE_SUCCESS, RESULT_MSG_SUCCESS, } from '@/constants/result-constants'; import { listAllNeInfo } from '@/api/ne/neInfo'; import { parseDataToOptions } from '@/utils/parse-tree-utils'; /**网元信息类型 */ type Ne = { /**网元列表 */ list: Record[]; /**级联options树结构 */ neCascaderOptions: Record[]; /**选择器单级父类型 */ neSelectOtions: Record[]; }; const useNeStore = defineStore('ne', { state: (): Ne => ({ list: [], neCascaderOptions: [], neSelectOtions: [], }), getters: { /** * 网元列表 * @param state 内部属性不用传入 * @returns 级联options */ getNeList(state) { return state.list; }, /** * 获取级联options树结构 * @param state 内部属性不用传入 * @returns 级联options */ getNeCascaderOptions(state) { return state.neCascaderOptions; }, /** * 选择器单级父类型 * @param state 内部属性不用传入 * @returns 选择options */ getNeSelectOtions(state) { return state.neSelectOtions; }, }, actions: { // 刷新网元列表 async fnNelistRefresh() { this.list = []; return await this.fnNelist(); }, // 获取网元列表 async fnNelist() { // 有数据不请求 if (this.list.length > 0) { return { code: RESULT_CODE_SUCCESS, msg: RESULT_MSG_SUCCESS['en_US'], data: this.list, }; } const res = await listAllNeInfo({ bandStatus: false, bandHost: false, }); if (res.code === RESULT_CODE_SUCCESS) { // 原始列表 this.list = JSON.parse(JSON.stringify(res.data)); // 转级联数据 const options = parseDataToOptions( res.data, 'neType', 'neName', 'neUid' ); this.neCascaderOptions = options; // 转选择器单级父类型 this.neSelectOtions = options.map(item => { return { label: item.label, value: item.value, }; }); } return res; }, /** * 含有网元 * @param metaNeType ['udm', 'ims', 'udm+ims', 'SGWC'] 支持大小写 * @returns boolean */ fnHasNe(metaNeType: string[]) { if (this.list.length > 0) { const neTypes = this.neSelectOtions.map(item => item.value); let match = false; // 匹配 for (const netype of metaNeType) { if (netype.indexOf('+') > -1) { metaNeType = netype.split('+'); match = true; break; } } if (match) { // 同时匹配 return metaNeType.every(item => neTypes.includes(item.toUpperCase())); } // 有一种 return metaNeType.some(item => neTypes.includes(item.toUpperCase())); } return false; }, }, }); export default useNeStore;