useDictionaries.js 2.9 KB

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