|
@@ -0,0 +1,113 @@
|
|
|
|
|
+// src/store/modules/dict.js
|
|
|
|
|
+import { defineStore } from 'pinia'
|
|
|
|
|
+import { ref } from 'vue'
|
|
|
|
|
+
|
|
|
|
|
+import { getDicts } from '@/api/system/dict/data'
|
|
|
|
|
+
|
|
|
|
|
+export const useDictStore = defineStore('dict', () => {
|
|
|
|
|
+ // 字典数据缓存 { dictType: { data: [...], expireTime: timestamp } }
|
|
|
|
|
+ const dictMap = ref({})
|
|
|
|
|
+
|
|
|
|
|
+ // 缓存过期时间:10分钟
|
|
|
|
|
+ const CACHE_EXPIRE_TIME = 10 * 60 * 1000
|
|
|
|
|
+
|
|
|
|
|
+ // 检查并获取字典数据(自动处理缓存过期)
|
|
|
|
|
+ const getDict = async (dictType) => {
|
|
|
|
|
+ const cache = dictMap.value[dictType]
|
|
|
|
|
+ const now = Date.now()
|
|
|
|
|
+
|
|
|
|
|
+ // 检查缓存是否存在且未过期
|
|
|
|
|
+ if (cache && now < cache.expireTime) {
|
|
|
|
|
+ return cache.data
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 缓存不存在或已过期,清除旧缓存
|
|
|
|
|
+ if (cache) {
|
|
|
|
|
+ delete dictMap.value[dictType]
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 调用接口获取字典数据
|
|
|
|
|
+ const res = await getDicts(dictType)
|
|
|
|
|
+ console.log('Store获取字典数据:', dictType, res) // 添加调试信息
|
|
|
|
|
+
|
|
|
|
|
+ // 由于request.js的拦截器,res直接就是数组
|
|
|
|
|
+ let arr = []
|
|
|
|
|
+ if (res && Array.isArray(res)) {
|
|
|
|
|
+ arr = res.map(p => ({
|
|
|
|
|
+ label: p.dictLabel, // 显示标签
|
|
|
|
|
+ value: p.dictValue, // 实际值
|
|
|
|
|
+ elTagType: p.listClass, // 标签类型(用于样式)
|
|
|
|
|
+ elTagClass: p.cssClass // CSS类名(用于样式)
|
|
|
|
|
+ }))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('字典数据格式错误:', res)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 存入新缓存,设置过期时间
|
|
|
|
|
+ dictMap.value[dictType] = {
|
|
|
|
|
+ data: arr,
|
|
|
|
|
+ expireTime: now + CACHE_EXPIRE_TIME
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return arr
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 同步获取字典数据(从缓存中获取,如果缓存不存在则返回空数组)
|
|
|
|
|
+ const getDictSync = (dictType) => {
|
|
|
|
|
+ const cache = dictMap.value[dictType]
|
|
|
|
|
+ return cache ? cache.data : []
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 设置字典数据到缓存
|
|
|
|
|
+ const setDict = (dictType, data) => {
|
|
|
|
|
+ dictMap.value[dictType] = {
|
|
|
|
|
+ data: data,
|
|
|
|
|
+ expireTime: Date.now() + CACHE_EXPIRE_TIME
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 手动清除指定字典缓存
|
|
|
|
|
+ const clearDictCache = (dictType) => {
|
|
|
|
|
+ if (dictType) {
|
|
|
|
|
+ delete dictMap.value[dictType]
|
|
|
|
|
+ } else {
|
|
|
|
|
+ dictMap.value = {}
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ dictMap,
|
|
|
|
|
+ getDict,
|
|
|
|
|
+ getDictSync,
|
|
|
|
|
+ setDict,
|
|
|
|
|
+ clearDictCache
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+ // , {
|
|
|
|
|
+ // persist: {
|
|
|
|
|
+ // // storage: {
|
|
|
|
|
+ // // getItem(key) {
|
|
|
|
|
+ // // return uni.getStorageSync(key);
|
|
|
|
|
+ // // },
|
|
|
|
|
+ // // setItem(key, value) {
|
|
|
|
|
+ // // uni.setStorageSync(key, value);
|
|
|
|
|
+ // // }
|
|
|
|
|
+ // // }
|
|
|
|
|
+
|
|
|
|
|
+ // storage: {
|
|
|
|
|
+ // getItem(key) {
|
|
|
|
|
+ // return JSON.parse(sessionStorage.getItem(key)) // 使用 sessionStorage
|
|
|
|
|
+ // },
|
|
|
|
|
+ // setItem(key, value) {
|
|
|
|
|
+ // sessionStorage.setItem(key, JSON.stringify(value))
|
|
|
|
|
+ // },
|
|
|
|
|
+ // removeItem(key) {
|
|
|
|
|
+ // sessionStorage.removeItem(key)
|
|
|
|
|
+ // }
|
|
|
|
|
+ // }
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+ // }
|
|
|
|
|
+)
|