adtec-core-package 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adtec-core-package",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
@@ -0,0 +1,81 @@
1
+ /**
2
+ * 创建人 胡啸东
3
+ * 说明: 查询字典Hooks
4
+ * 创建时间: 2024/11/28 下午10:27
5
+ * 修改时间: 2024/11/28 下午10:27
6
+ */
7
+ import { onBeforeUnmount, ref } from 'vue'
8
+ import type { ISysDictDataCacheVo } from '../interface/ISysDictDataCacheVo'
9
+ import { ElMessage } from 'element-plus'
10
+ import SysDictCacheApi from '../api/SysDictCacheApi.ts'
11
+
12
+ // 定义包含map的整体类型
13
+ export interface dictMapType {
14
+ [key: string]: ISysDictDataCacheVo[]
15
+ }
16
+
17
+ export default function useDictHooks(dictTypes: string[]) {
18
+ const dictMap = ref<dictMapType>({})
19
+ const dictDataMap = ref<Map<string, Map<string, ISysDictDataCacheVo>>>(new Map())
20
+ const dictDefaultValueMap = ref<Map<string, ISysDictDataCacheVo>>(new Map())
21
+ const getDict = async (dictTypes: string[]) => {
22
+ try {
23
+ const data = await SysDictCacheApi.batchGetSysDictDataCacheVo(dictTypes)
24
+ dictMap.value = { ...dictMap.value, ...data }
25
+ //获取默认值
26
+ Object.keys(data).forEach((item: string) => {
27
+ findDefaultValue(data[item], item)
28
+ const dataMap = new Map<string, ISysDictDataCacheVo>()
29
+ packageDictDataMap(dataMap, data[item])
30
+ dictDataMap.value.set(item, dataMap)
31
+ })
32
+ } catch (error: any) {
33
+ ElMessage.error(error.msg || error.message)
34
+ }
35
+ }
36
+
37
+ const packageDictDataMap = async (
38
+ map: Map<string, ISysDictDataCacheVo>,
39
+ list?: ISysDictDataCacheVo[],
40
+ ) => {
41
+ if (list && list.length) {
42
+ list.forEach((item: ISysDictDataCacheVo) => {
43
+ map.set(item.value, { ...item, ...{ children: undefined } })
44
+ packageDictDataMap(map, item.children)
45
+ })
46
+ }
47
+ }
48
+ const findDefaultValue = async (list: ISysDictDataCacheVo[], type: string) => {
49
+ const find = getDefaultValue(list)
50
+ find && dictDefaultValueMap.value.set(type, find)
51
+ }
52
+ const getDefaultValue = (list?: ISysDictDataCacheVo[]): ISysDictDataCacheVo | undefined => {
53
+ if (list && list.length) {
54
+ const find = list.find((item: ISysDictDataCacheVo) => item.isDefault === '1')
55
+ if (find) return find
56
+ for (const iSysDictDataCacheVo of list) {
57
+ const res = getDefaultValue(iSysDictDataCacheVo.children)
58
+ if (res) return res
59
+ }
60
+ }
61
+ }
62
+
63
+ const getDictName = (value?: string, dictType?: string) => {
64
+ return dictDataMap.value.get(dictType!)?.get(value!)?.label || value
65
+ }
66
+ const getDictData = (value: string, dictType: string) => {
67
+ return dictDataMap.value.get(dictType)?.get(value)
68
+ }
69
+ getDict(dictTypes).then(() => {})
70
+ onBeforeUnmount(() => {
71
+ dictDefaultValueMap.value.clear()
72
+ dictDataMap.value.clear()
73
+ })
74
+ return {
75
+ dictMap,
76
+ dictDefaultValueMap,
77
+ getDict,
78
+ getDictName,
79
+ getDictData,
80
+ }
81
+ }