import { Module, MutationTree, ActionTree, GetterTree } from 'vuex' import { SysDictionaryDto as Dictionary, StringOptionResultModel } from '@/api/swagger' import { RootState } from '@/store' import { api } from '@/api' const initState: IDictionaryState = {} interface Basic { [key: string]: Dictionary[] } export type IDictionaryState = Basic const mutations: MutationTree = { update( state: IDictionaryState, { code, dictionary: data }: { code: keyof IDictionaryState; dictionary: Dictionary[] }, ) { state[code] = data }, } const actions: ActionTree = { async fetch( { commit }, params: { codes: (keyof IDictionaryState)[] isLoadAll?: boolean }, ): Promise { // const res = await getDictionaryByCodes(codes, isLoadAll) const { codes, isLoadAll } = params const res = await api.get('/api/Dictionary/OptionsListByCodes', { codes: codes.join(','), isLoadAll: isLoadAll as boolean, }) if (Array.isArray(res.data)) { const dictObj = res.data.reduce((a, b) => { if (b.data) { a[b.data as string] = a[b.data as string] || [] a[b.data as string].push(b) } return a }, {}) Object.keys(dictObj).forEach((code: string) => { commit('update', { code, dictionary: dictObj[code] }) }) } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return res.data! }, } const getters: GetterTree = {} for (const key in initState) { getters[key] = (state) => state[key] } const dictionary: Module = { namespaced: true, state: initState, mutations, getters, actions, } export default dictionary