车间能级提升-智能设备管理系统
朱桂飞
2025-01-09 3e8f7f239bedae0b4f04a1ac6bd443ba6298f73c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { DictData } from '#/api/system/dict/dict-data-model';
 
import { dictDataInfo } from '#/api/system/dict/dict-data';
import { type Option, useDictStore } from '#/store/dict';
 
// todo 重复代码的封装
export function getDict(dictName: string): DictData[] {
  const { dictRequestCache, getDict, setDictInfo } = useDictStore();
  // 这里拿到
  const dictList = getDict(dictName);
  // 检查请求状态缓存
  if (dictList.length === 0 && !dictRequestCache.has(dictName)) {
    dictRequestCache.set(
      dictName,
      dictDataInfo(dictName)
        .then((resp) => {
          // 缓存到store 这样就不用重复获取了
          // 内部处理了push的逻辑 这里不用push
          setDictInfo(dictName, resp);
        })
        .finally(() => {
          // 移除请求状态缓存
          /**
           * 这里主要判断字典item为空的情况(无奈兼容 不给字典item本来就是错误用法)
           * 会导致if一直进入逻辑导致接口无限刷新
           * 在这里dictList为空时 不删除缓存
           */
          if (dictList.length > 0) {
            dictRequestCache.delete(dictName);
          }
        }),
    );
  }
  return dictList;
}
 
export function getDictOptions(dictName: string): Option[] {
  const { dictRequestCache, getDictOptions, setDictInfo } = useDictStore();
  const dictOptionList = getDictOptions(dictName);
  // 检查请求状态缓存
  if (dictOptionList.length === 0 && !dictRequestCache.has(dictName)) {
    dictRequestCache.set(
      dictName,
      dictDataInfo(dictName)
        .then((resp) => {
          // 缓存到store 这样就不用重复获取了
          // 内部处理了push的逻辑 这里不用push
          setDictInfo(dictName, resp);
        })
        .finally(() => {
          // 移除请求状态缓存
          /**
           * 这里主要判断字典item为空的情况(无奈兼容 不给字典item本来就是错误用法)
           * 会导致if一直进入逻辑导致接口五线刷新
           * 在这里dictList为空时 不删除缓存
           */
          if (dictOptionList.length > 0) {
            dictRequestCache.delete(dictName);
          }
        }),
    );
  }
  return dictOptionList;
}