useDictionaries.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.data = obj?.children ? obj.children.map(e =>{
  17. // 市辖区直接显示区
  18. const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
  19. if (municipality && e.children[0].children?.length) e.children = e.children[0].children
  20. return e
  21. }) : []
  22. }
  23. if (type === 'areaTreeDataExtend') { // 前排加上不限
  24. const obj = val.data.find(e => e.name === '中国')
  25. if (obj?.children?.length) {
  26. // const province =
  27. obj.children.forEach(e=> {
  28. // 市辖区直接显示区
  29. const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
  30. if (municipality && e.children[0].children?.length) e.children = e.children[0].children
  31. // 不限
  32. extendFun(e)
  33. })
  34. val.data = obj.children
  35. } else val.data = []
  36. function extendFun (e) {
  37. const idType = Number.isInteger(e.id) ? 'int' : 'str'
  38. if(e.children?.length) {
  39. e.children.unshift({ name: '不限', id: e.id+'unlimited', idType })
  40. e.children.forEach(i => extendFun(i))
  41. }
  42. }
  43. }
  44. // 一小时过期
  45. const currentTime = new Date()
  46. currentTime.setTime(currentTime.getTime() + 3600 * 1000)
  47. uni.setStorageSync(type, JSON.stringify({
  48. data: val,
  49. // expire: Date.now() + cacheTime * 1000
  50. expire: currentTime.getTime()
  51. }))
  52. }
  53. export const getDict = (type, params, apiType = 'dict') => {
  54. if (!type) {
  55. return []
  56. }
  57. return new Promise((resolve) => {
  58. const item = uni.getStorageSync(type)
  59. const catchData = item ? JSON.parse(item) : null
  60. if (catchData && catchData.expire && (Date.now() <= catchData.expire) && catchData.data && catchData.data?.data?.length) {
  61. return resolve({ data: catchData.data })
  62. }
  63. // 传参按照规范参数传
  64. const query = params ? params : { type }
  65. const apiFn = {
  66. dict: getDictData,
  67. positionTreeData: getPositionTreeData, // 职位tree
  68. areaTreeData: getAreaTreeData, // 区域tree
  69. areaTreeDataExtend: getAreaTreeData, // 区域tree(二级以后含不限)
  70. industryTreeData: getIndustryTreeData, // 行业tree
  71. industryList: getIndustryListData,
  72. skillList: getSkillList,
  73. areaList: getAreaListData,
  74. areaMap: getAreaMapData,
  75. positionData: getPositionData
  76. }
  77. apiFn[apiType](query).then(data => {
  78. // setDict(type, data, Date.now())
  79. setDict(type, data)
  80. resolve({ data })
  81. })
  82. })
  83. }