useDictionaries.js 1.3 KB

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