导入字典
This commit is contained in:
parent
502cf3507f
commit
240f9fa966
9
App.vue
9
App.vue
|
|
@ -1,4 +1,8 @@
|
|||
<script>
|
||||
// #ifdef VUE3
|
||||
import { initDictData } from '@/utils/dict'
|
||||
// #endif
|
||||
|
||||
export default {
|
||||
onLaunch: function() {
|
||||
console.log('App Launch')
|
||||
|
|
@ -12,6 +16,11 @@
|
|||
uni.setStorageSync('token', testToken)
|
||||
console.log('已设置测试 token:', testToken)
|
||||
}
|
||||
|
||||
// 初始化字典数据
|
||||
initDictData().catch(err => {
|
||||
console.error('初始化字典数据失败:', err)
|
||||
})
|
||||
// #endif
|
||||
|
||||
// 检测初始网络状态
|
||||
|
|
|
|||
|
|
@ -26,3 +26,23 @@ export const getRegionTree = () => {
|
|||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取数据字典列表
|
||||
* @param {Object} params 请求参数(可选)
|
||||
* @param {string} params.dictType 字典类型(可选,用于筛选特定类型的字典)
|
||||
* @returns {Promise} 返回字典数据列表
|
||||
*/
|
||||
export const getDictDataList = (params = {}) => {
|
||||
const queryParams = []
|
||||
if (params.dictType) {
|
||||
queryParams.push(`dictType=${encodeURIComponent(params.dictType)}`)
|
||||
}
|
||||
const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''
|
||||
|
||||
return uni.$uv.http.get(`/system/dict/data/list${queryString}`, {
|
||||
custom: {
|
||||
auth: true
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
218
store/dict.js
Normal file
218
store/dict.js
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
import { defineStore } from 'pinia'
|
||||
import { getDictDataList } from '@/api/common'
|
||||
|
||||
/**
|
||||
* 字典数据 Store
|
||||
* 用于管理数据字典,提供字典数据的存储和映射功能
|
||||
*/
|
||||
export const useDictStore = defineStore('dict', {
|
||||
state: () => {
|
||||
return {
|
||||
// 所有字典数据列表
|
||||
dictList: [],
|
||||
// 按 dictType 分组的字典数据,格式:{ dictType: [dictItems] }
|
||||
dictMap: {},
|
||||
// 字典数据是否已加载
|
||||
isLoaded: false,
|
||||
// 加载时间戳,用于判断是否需要刷新
|
||||
loadTime: null
|
||||
}
|
||||
},
|
||||
|
||||
getters: {
|
||||
/**
|
||||
* 根据字典类型获取字典列表
|
||||
* @param {string} dictType 字典类型
|
||||
* @returns {Array} 字典项列表
|
||||
*/
|
||||
getDictByType: (state) => (dictType) => {
|
||||
return state.dictMap[dictType] || []
|
||||
},
|
||||
|
||||
/**
|
||||
* 根据字典类型和值获取字典标签
|
||||
* @param {string} dictType 字典类型
|
||||
* @param {string|number} dictValue 字典值
|
||||
* @returns {string} 字典标签,如果未找到返回原值
|
||||
*/
|
||||
getDictLabel: (state) => (dictType, dictValue) => {
|
||||
const dictItems = state.dictMap[dictType] || []
|
||||
const item = dictItems.find(item => String(item.dictValue) === String(dictValue))
|
||||
return item ? item.dictLabel : dictValue
|
||||
},
|
||||
|
||||
/**
|
||||
* 根据字典类型和值获取完整的字典项
|
||||
* @param {string} dictType 字典类型
|
||||
* @param {string|number} dictValue 字典值
|
||||
* @returns {Object|null} 字典项对象,如果未找到返回 null
|
||||
*/
|
||||
getDictItem: (state) => (dictType, dictValue) => {
|
||||
const dictItems = state.dictMap[dictType] || []
|
||||
return dictItems.find(item => String(item.dictValue) === String(dictValue)) || null
|
||||
},
|
||||
|
||||
/**
|
||||
* 批量获取字典标签
|
||||
* @param {string} dictType 字典类型
|
||||
* @param {Array<string|number>} dictValues 字典值数组
|
||||
* @returns {Array<string>} 字典标签数组
|
||||
*/
|
||||
getDictLabels: (state) => (dictType, dictValues) => {
|
||||
if (!Array.isArray(dictValues)) {
|
||||
return []
|
||||
}
|
||||
const dictItems = state.dictMap[dictType] || []
|
||||
return dictValues.map(value => {
|
||||
const item = dictItems.find(item => String(item.dictValue) === String(value))
|
||||
return item ? item.dictLabel : value
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
/**
|
||||
* 加载字典数据
|
||||
* @param {boolean} forceRefresh 是否强制刷新(默认 false)
|
||||
* @returns {Promise} 返回加载结果
|
||||
*/
|
||||
async loadDictData(forceRefresh = false) {
|
||||
// 如果已加载且不强制刷新,直接返回
|
||||
if (this.isLoaded && !forceRefresh) {
|
||||
return Promise.resolve(this.dictList)
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await getDictDataList()
|
||||
// 根据响应数据结构处理
|
||||
// 如果响应直接是数组,使用 response
|
||||
// 如果响应有 rows 字段,使用 response.rows
|
||||
const dictList = Array.isArray(response) ? response : (response.rows || [])
|
||||
|
||||
// 过滤掉 status 不为 '0' 的字典项(停用的字典)
|
||||
const activeDictList = dictList.filter(item => item.status === '0' || item.status === 0)
|
||||
|
||||
this.dictList = activeDictList
|
||||
this.isLoaded = true
|
||||
this.loadTime = Date.now()
|
||||
|
||||
// 按 dictType 分组
|
||||
this.dictMap = {}
|
||||
activeDictList.forEach(item => {
|
||||
if (!this.dictMap[item.dictType]) {
|
||||
this.dictMap[item.dictType] = []
|
||||
}
|
||||
this.dictMap[item.dictType].push(item)
|
||||
})
|
||||
|
||||
// 按 dictSort 排序
|
||||
Object.keys(this.dictMap).forEach(dictType => {
|
||||
this.dictMap[dictType].sort((a, b) => {
|
||||
const sortA = parseInt(a.dictSort) || 0
|
||||
const sortB = parseInt(b.dictSort) || 0
|
||||
return sortA - sortB
|
||||
})
|
||||
})
|
||||
|
||||
// 保存到本地存储
|
||||
try {
|
||||
uni.setStorageSync('dictData', {
|
||||
dictList: this.dictList,
|
||||
dictMap: this.dictMap,
|
||||
loadTime: this.loadTime
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn('保存字典数据到本地存储失败:', error)
|
||||
}
|
||||
|
||||
return this.dictList
|
||||
} catch (error) {
|
||||
console.error('加载字典数据失败:', error)
|
||||
// 如果加载失败,尝试从本地存储恢复
|
||||
try {
|
||||
const cached = uni.getStorageSync('dictData')
|
||||
if (cached && cached.dictList) {
|
||||
this.dictList = cached.dictList
|
||||
this.dictMap = cached.dictMap
|
||||
this.loadTime = cached.loadTime
|
||||
this.isLoaded = true
|
||||
console.log('从本地存储恢复字典数据')
|
||||
return this.dictList
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('从本地存储恢复字典数据失败:', e)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 加载特定类型的字典数据
|
||||
* @param {string} dictType 字典类型
|
||||
* @returns {Promise} 返回该类型的字典列表
|
||||
*/
|
||||
async loadDictByType(dictType) {
|
||||
try {
|
||||
const response = await getDictDataList({ dictType })
|
||||
const dictList = Array.isArray(response) ? response : (response.rows || [])
|
||||
const activeDictList = dictList.filter(item => item.status === '0' || item.status === 0)
|
||||
|
||||
// 更新该类型的字典数据
|
||||
if (!this.dictMap[dictType]) {
|
||||
this.dictMap[dictType] = []
|
||||
}
|
||||
this.dictMap[dictType] = activeDictList.sort((a, b) => {
|
||||
const sortA = parseInt(a.dictSort) || 0
|
||||
const sortB = parseInt(b.dictSort) || 0
|
||||
return sortA - sortB
|
||||
})
|
||||
|
||||
// 更新主列表
|
||||
const existingIndex = this.dictList.findIndex(item => item.dictType === dictType)
|
||||
if (existingIndex >= 0) {
|
||||
// 移除旧的该类型数据
|
||||
this.dictList = this.dictList.filter(item => item.dictType !== dictType)
|
||||
}
|
||||
this.dictList.push(...activeDictList)
|
||||
|
||||
return this.dictMap[dictType]
|
||||
} catch (error) {
|
||||
console.error(`加载字典类型 ${dictType} 失败:`, error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 初始化字典数据(从本地存储恢复)
|
||||
*/
|
||||
initFromCache() {
|
||||
try {
|
||||
const cached = uni.getStorageSync('dictData')
|
||||
if (cached && cached.dictList) {
|
||||
this.dictList = cached.dictList
|
||||
this.dictMap = cached.dictMap
|
||||
this.loadTime = cached.loadTime
|
||||
this.isLoaded = true
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('从本地存储初始化字典数据失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 清除字典数据
|
||||
*/
|
||||
clearDictData() {
|
||||
this.dictList = []
|
||||
this.dictMap = {}
|
||||
this.isLoaded = false
|
||||
this.loadTime = null
|
||||
try {
|
||||
uni.removeStorageSync('dictData')
|
||||
} catch (error) {
|
||||
console.warn('清除本地存储的字典数据失败:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
63
utils/dict.js
Normal file
63
utils/dict.js
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import { useDictStore } from '@/store/dict'
|
||||
|
||||
/**
|
||||
* 字典工具函数
|
||||
* 提供便捷的字典映射方法
|
||||
*/
|
||||
|
||||
/**
|
||||
* 根据字典类型和值获取字典标签
|
||||
* @param {string} dictType 字典类型
|
||||
* @param {string|number} dictValue 字典值
|
||||
* @returns {string} 字典标签
|
||||
*/
|
||||
export const getDictLabel = (dictType, dictValue) => {
|
||||
const dictStore = useDictStore()
|
||||
return dictStore.getDictLabel(dictType, dictValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字典类型获取字典列表
|
||||
* @param {string} dictType 字典类型
|
||||
* @returns {Array} 字典项列表
|
||||
*/
|
||||
export const getDictList = (dictType) => {
|
||||
const dictStore = useDictStore()
|
||||
return dictStore.getDictByType(dictType)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字典类型和值获取完整的字典项
|
||||
* @param {string} dictType 字典类型
|
||||
* @param {string|number} dictValue 字典值
|
||||
* @returns {Object|null} 字典项对象
|
||||
*/
|
||||
export const getDictItem = (dictType, dictValue) => {
|
||||
const dictStore = useDictStore()
|
||||
return dictStore.getDictItem(dictType, dictValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量获取字典标签
|
||||
* @param {string} dictType 字典类型
|
||||
* @param {Array<string|number>} dictValues 字典值数组
|
||||
* @returns {Array<string>} 字典标签数组
|
||||
*/
|
||||
export const getDictLabels = (dictType, dictValues) => {
|
||||
const dictStore = useDictStore()
|
||||
return dictStore.getDictLabels(dictType, dictValues)
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化字典数据
|
||||
* @param {boolean} forceRefresh 是否强制刷新
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export const initDictData = async (forceRefresh = false) => {
|
||||
const dictStore = useDictStore()
|
||||
// 先从缓存初始化
|
||||
dictStore.initFromCache()
|
||||
// 然后加载最新数据
|
||||
return dictStore.loadDictData(forceRefresh)
|
||||
}
|
||||
|
||||
|
|
@ -2,4 +2,5 @@ export * from './qiniu'
|
|||
export * from './request'
|
||||
export * from './taskConfig'
|
||||
export * from './customerMappings'
|
||||
export * from './textSolve'
|
||||
export * from './textSolve'
|
||||
export * from './dict'
|
||||
Loading…
Reference in New Issue
Block a user