useDictionaries.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { dictApi } from '@/api/menduner/common/useDictionaries'
  2. // 定义对应的api
  3. // const DICT_CITY_API = {
  4. // menduner_exp_type: getDictData
  5. // }
  6. const setDict = (type, val, cacheTime = 7200) => {
  7. localStorage.setItem(type, JSON.stringify({
  8. data: val,
  9. expire: Date.now() + cacheTime * 1000
  10. }))
  11. }
  12. export const getDict = (type, params, apiType = 'dict') => {
  13. if (!type) {
  14. // console.error('type不存在', type, params, apiType)
  15. return []
  16. }
  17. return new Promise((resolve) => {
  18. const item = localStorage.getItem(type)
  19. const catchData = item ? JSON.parse(item) : null
  20. if (catchData && catchData.expire && (Date.now() <= catchData.expire)) {
  21. return resolve({ data: catchData.data })
  22. }
  23. // 传参按照规范参数传
  24. const query = params ? params : { type }
  25. const apiFn = {
  26. // dict: getDictData,
  27. positionTreeData: dictApi.getPositionTreeData, // 职位tree
  28. areaTreeData: dictApi.getAreaTreeData, // 区域tree
  29. // industryTreeData: getIndustryTreeData, // 行业tree
  30. // industryList: getIndustryListData,
  31. // skillList: getSkillList,
  32. areaList: dictApi.getAreaListData,
  33. // areaMap: getAreaMapData,
  34. positionData: dictApi.getPositionData
  35. }
  36. if (!apiFn[apiType]) return
  37. apiFn[apiType](query).then(data => {
  38. setDict(type, data, Date.now())
  39. resolve({ data })
  40. })
  41. })
  42. }