import { defineStore } from 'pinia' import { tagsTenant } from '@/api/modules/knowledge/tags' import { ElMessage } from 'element-plus' import { datasetGroupsGetAllByTenantId } from '@/api/modules/knowledge/group' import { pluginConfigs, pluginGetModelTypeList } from '@/api/modules/model' import { dictionary, dictionaryDefine } from './dictionary-define' import { sysDictFindByDictType } from '@/api/modules/global/dictionary' // @ts-ignore import FileExcel from '@/assets/images/file-excel.png' // @ts-ignore import FileTxt from '@/assets/images/file-txt.png' // @ts-ignore import FileUnknown from '@/assets/images/file-unknown.png' // @ts-ignore import FileWord from '@/assets/images/file-word.png' const listToMap = ({ list, labelKey = 'label', valueKey = 'value', isObj = false, }) => { const map = new Map() list.forEach((v) => { map.set(v[valueKey], isObj ? v : v[labelKey]) }) return map } export const useDictionaryStore = defineStore('dictionary', { state: () => ({ ...dictionary, knowledgeTags: { waiting: false, list: [], map: new Map(), objMap: new Map(), }, knowledgeGroups: { waiting: false, list: [], map: new Map(), objMap: new Map(), }, modelProvides: { waiting: false, list: [], map: new Map(), objMap: new Map(), }, modelTypes: { waiting: false, list: [], map: new Map(), objMap: new Map(), }, shareConditions: [ { label: '不开放', value: 'NO_SHARE' }, { label: '全部开放', value: 'OPEN_SHARE' }, { label: '申请开放', value: 'SHARE_ON_CONDITION' }, ], modelStatus: [ { label: '停用', value: 0 }, { label: '启用', value: 1 }, ], documentStatus: [ { label: '启用', value: 0 }, { label: '停用', value: 1 }, ], appTypes: [ { label: '简洁', value: 0 }, { label: '高级', value: 1 }, ], appStatus: [ { label: '草稿', value: 0 }, { label: '发布', value: 1 }, ], tenantStatus: [ { label: '启用', value: true }, { label: '停用', value: false }, ], }), getters: { // shareConditionsMap() { // return listToMap((this as any).shareConditions) // }, }, actions: { getStaticDict(dict, value) { const map = listToMap((this as any)[dict]) return map.get(value) || value }, initDict(type) { return new Promise((resolve, reject) => { if (this[dictionaryDefine[type][0]].length === 0) { sysDictFindByDictType(type) .then(({ data }: any) => { const map = new Map() const objMap = new Map() if (data.length > 0) { data.forEach((v: any) => { v.label = v.dictName v.value = v.dictValue map.set(v.value, v.label) objMap.set(v.value, v) }) } this[dictionaryDefine[type][0]] = data this[dictionaryDefine[type][1]] = map this[dictionaryDefine[type]?.[2]] = objMap resolve(data) }) .catch(() => {}) } else { resolve(this[dictionaryDefine[type][0]]) } }) }, initKnowledgeTags(tenantId) { if (!this.knowledgeTags.waiting) { this.knowledgeTags.waiting = true tagsTenant(tenantId) .then(({ data }: any) => { const arr: any = data.map((v) => { v.label = v.name v.value = v.id v.total = v.targetSize return v }) this.knowledgeTags.list = arr this.knowledgeTags.map = listToMap({ list: arr }) this.knowledgeTags.objMap = listToMap({ list: arr, isObj: true }) }) .catch(() => {}) .finally(() => { this.knowledgeTags.waiting = false }) } }, initKnowledgeGroups(tenantId) { if (!this.knowledgeGroups.waiting) { this.knowledgeGroups.waiting = true datasetGroupsGetAllByTenantId(tenantId) .then(({ data }: any) => { const arr: any = data.map((v) => { v.label = v.name v.value = v.id return v }) this.knowledgeGroups.list = arr this.knowledgeGroups.map = listToMap({ list: arr }) this.knowledgeGroups.objMap = listToMap({ list: arr, isObj: true }) }) .catch(() => {}) .finally(() => { this.knowledgeGroups.waiting = false }) } }, initModelTypes() { if (!this.modelTypes.waiting) { this.modelTypes.waiting = true pluginGetModelTypeList() .then(({ data }: any) => { const arr: any = data.map((v) => { v.label = v.label v.value = v.code return v }) this.modelTypes.list = arr this.modelTypes.map = listToMap({ list: arr }) this.modelTypes.objMap = listToMap({ list: arr, isObj: true }) }) .catch(() => {}) .finally(() => { this.modelTypes.waiting = false }) } }, initModelProvides() { if (!this.modelProvides.waiting) { this.modelProvides.waiting = true pluginConfigs() .then(({ data }: any) => { const arr: any = [] Object.values(data).forEach((v: any) => { arr.push( ...v.map((v) => { v.label = v.providerName v.value = v.id return v }), ) }) this.modelProvides.list = arr this.modelProvides.map = listToMap({ list: arr }) this.modelProvides.objMap = listToMap({ list: arr, isObj: true }) }) .catch(() => {}) .finally(() => { this.modelProvides.waiting = false }) } }, getFileIcon(name) { if (name.toLowerCase().includes('.txt')) { return FileTxt } else if ( name.toLowerCase().includes('.xls') || name.toLowerCase().includes('.xlsx') ) { return FileExcel } else if ( name.toLowerCase().includes('.doc') || name.toLowerCase().includes('.docx') ) { return FileWord } return FileUnknown }, }, })