From ddca0e977a3c9fd2f955b990760c13107b69e0bc Mon Sep 17 00:00:00 2001 From: D <3066417822@qq.com> Date: Tue, 12 Dec 2023 13:14:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E5=85=B8=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/system/dict/data.js | 52 ++++++++++++++++++++++++++++++ src/api/system/dict/type.js | 60 ++++++++++++++++++++++++++++++++++ src/main.js | 16 ++++++++++ src/store/index.ts | 4 ++- src/store/modules/dict.ts | 64 +++++++++++++++++++++++++++++++++++++ src/types/store.ts | 2 ++ src/utils/dict.ts | 33 +++++++++++++++++++ 7 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 src/api/system/dict/data.js create mode 100644 src/api/system/dict/type.js create mode 100644 src/store/modules/dict.ts create mode 100644 src/utils/dict.ts diff --git a/src/api/system/dict/data.js b/src/api/system/dict/data.js new file mode 100644 index 0000000..6c9eb79 --- /dev/null +++ b/src/api/system/dict/data.js @@ -0,0 +1,52 @@ +import request from '@/utils/request' + +// 查询字典数据列表 +export function listData(query) { + return request({ + url: '/system/dict/data/list', + method: 'get', + params: query + }) +} + +// 查询字典数据详细 +export function getData(dictCode) { + return request({ + url: '/system/dict/data/' + dictCode, + method: 'get' + }) +} + +// 根据字典类型查询字典数据信息 +export function getDicts(dictType) { + return request({ + url: '/system/dict/data/type/' + dictType, + method: 'get' + }) +} + +// 新增字典数据 +export function addData(data) { + return request({ + url: '/system/dict/data', + method: 'post', + data: data + }) +} + +// 修改字典数据 +export function updateData(data) { + return request({ + url: '/system/dict/data', + method: 'put', + data: data + }) +} + +// 删除字典数据 +export function delData(dictCode) { + return request({ + url: '/system/dict/data/' + dictCode, + method: 'delete' + }) +} diff --git a/src/api/system/dict/type.js b/src/api/system/dict/type.js new file mode 100644 index 0000000..a0254ba --- /dev/null +++ b/src/api/system/dict/type.js @@ -0,0 +1,60 @@ +import request from '@/utils/request' + +// 查询字典类型列表 +export function listType(query) { + return request({ + url: '/system/dict/type/list', + method: 'get', + params: query + }) +} + +// 查询字典类型详细 +export function getType(dictId) { + return request({ + url: '/system/dict/type/' + dictId, + method: 'get' + }) +} + +// 新增字典类型 +export function addType(data) { + return request({ + url: '/system/dict/type', + method: 'post', + data: data + }) +} + +// 修改字典类型 +export function updateType(data) { + return request({ + url: '/system/dict/type', + method: 'put', + data: data + }) +} + +// 删除字典类型 +export function delType(dictId) { + return request({ + url: '/system/dict/type/' + dictId, + method: 'delete' + }) +} + +// 刷新字典缓存 +export function refreshCache() { + return request({ + url: '/system/dict/type/refreshCache', + method: 'delete' + }) +} + +// 获取字典选择框列表 +export function optionselect() { + return request({ + url: '/system/dict/type/optionselect', + method: 'get' + }) +} diff --git a/src/main.js b/src/main.js index f704b95..3ea98a1 100644 --- a/src/main.js +++ b/src/main.js @@ -4,10 +4,26 @@ import uviewPlus from 'uview-plus' import { createSSRApp } from 'vue' + +import { useDict } from '@/utils/dict' +import { parseTime, resetForm, addDateRange, handleTree, selectDictLabel, selectDictLabels } from '@/utils/ruoyi' + + + export function createApp() { const app = createSSRApp(App) app.use(uviewPlus) app.use(plugins) + + // 全局方法挂载 + app.config.globalProperties.useDict = useDict + app.config.globalProperties.parseTime = parseTime + app.config.globalProperties.resetForm = resetForm + app.config.globalProperties.handleTree = handleTree + app.config.globalProperties.addDateRange = addDateRange + app.config.globalProperties.selectDictLabel = selectDictLabel + app.config.globalProperties.selectDictLabels = selectDictLabels + return { app } diff --git a/src/store/index.ts b/src/store/index.ts index 012045c..e1b34dc 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,9 +1,11 @@ import user from '@/store/modules/user' +import dict from '@/store/modules/dict' import getters from './getters' import { createStore } from "vuex"; -const store = createStore({ +const store = createStore({ modules: { user, + dict }, getters }); diff --git a/src/store/modules/dict.ts b/src/store/modules/dict.ts new file mode 100644 index 0000000..4728efe --- /dev/null +++ b/src/store/modules/dict.ts @@ -0,0 +1,64 @@ +import config from "@/config"; +import storage from "@/utils/storage"; +import constant from "@/utils/constant"; +import { login, logout, getInfo } from "@/api/login"; +import { getToken, setToken, removeToken } from "@/utils/auth"; +import { dictStateType, dictType } from "@/types/store"; +import { Module } from "vuex"; + +const baseUrl = config.baseUrl; + +const dict: Module = { + state: { + dict: new Array(), + }, + actions: { + // 获取字典 + getDict({ state }, _key) { + if (_key == null && _key == "") { + return null; + } + try { + for (let i = 0; i < state.dict.length; i++) { + if (state.dict[i].key == _key) { + return state.dict[i].value; + } + } + } catch (e) { + return null; + } + }, + // 设置字典 + setDict({ state }, dict: dictType) { + if (dict.key !== null && dict.key !== "") { + state.dict.push({ + key: dict.key, + value: dict.value, + }); + } + }, + // 删除字典 + removeDict({ state }, _key) { + var bln = false; + try { + for (let i = 0; i < state.dict.length; i++) { + if (state.dict[i].key == _key) { + state.dict.splice(i, 1); + return true; + } + } + } catch (e) { + bln = false; + } + return bln; + }, + // 清空字典 + cleanDict({ state }) { + state.dict = new Array(); + }, + // 初始字典 + initDict() {}, + }, +}; + +export default dict; diff --git a/src/types/store.ts b/src/types/store.ts index 58ac80a..a219fad 100644 --- a/src/types/store.ts +++ b/src/types/store.ts @@ -14,4 +14,6 @@ export interface UserForm { uuid: string } +export type dictType = { key: string; value: string }; +export type dictStateType = { dict: Array }; diff --git a/src/utils/dict.ts b/src/utils/dict.ts new file mode 100644 index 0000000..1975f7d --- /dev/null +++ b/src/utils/dict.ts @@ -0,0 +1,33 @@ +import store from "@/store"; +import { getDicts } from "@/api/system/dict/data"; +import { Ref, ref, toRefs } from "vue"; + +/** + * 获取字典数据 + */ +export function useDict(...args: any[]) { + const res: Ref = ref({}); + return (() => { + args.forEach(async (dictType: string, index) => { + res.value[dictType] = []; + const dicts = await store.dispatch("getDict", dictType); + if (dicts) { + res.value[dictType] = dicts; + } else { + getDicts(dictType).then((resp) => { + res.value[dictType] = resp.data.map((p: any) => ({ + label: p.dictLabel, + value: p.dictValue, + elTagType: p.listClass, + elTagClass: p.cssClass, + })); + store.dispatch("setDict", { + key: dictType, + value: res.value[dictType], + }); + }); + } + }); + return toRefs(res.value); + })(); +}