useDictionaries.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {
  2. getDictData,
  3. getIndustryListData,
  4. getSkillList,
  5. getIndustryTreeData,
  6. getAreaListData,
  7. getAreaMapData,
  8. getPositionTreeData,
  9. getAreaTreeData,
  10. getPositionData
  11. } from '@/api/common'
  12. // const setDict = (type, val, cacheTime = 7200) => {
  13. const setDict = (type, val) => {
  14. if (type === 'areaTreeData') {
  15. const obj = val.data.find(e => e.name === '中国')
  16. val = obj?.children ? obj.children : []
  17. }
  18. // 一小时过期
  19. const currentTime = new Date()
  20. currentTime.setTime(currentTime.getTime() + 3600 * 1000)
  21. uni.setStorageSync(type, JSON.stringify({
  22. data: val,
  23. // expire: Date.now() + cacheTime * 1000
  24. expire: currentTime.getTime()
  25. }))
  26. }
  27. export const getDict = (type, params, apiType = 'dict') => {
  28. if (!type) {
  29. return []
  30. }
  31. return new Promise((resolve) => {
  32. const item = uni.getStorageSync(type)
  33. const catchData = item ? JSON.parse(item) : null
  34. if (catchData && catchData.expire && (Date.now() <= catchData.expire) && catchData.data && catchData.data?.data?.length) {
  35. return resolve({ data: catchData.data })
  36. }
  37. // 传参按照规范参数传
  38. const query = params ? params : { type }
  39. const apiFn = {
  40. dict: getDictData,
  41. positionTreeData: getPositionTreeData, // 职位tree
  42. areaTreeData: getAreaTreeData, // 区域tree
  43. industryTreeData: getIndustryTreeData, // 行业tree
  44. industryList: getIndustryListData,
  45. skillList: getSkillList,
  46. areaList: getAreaListData,
  47. areaMap: getAreaMapData,
  48. positionData: getPositionData
  49. }
  50. apiFn[apiType](query).then(data => {
  51. // setDict(type, data, Date.now())
  52. setDict(type, data)
  53. resolve({ data })
  54. })
  55. })
  56. }