useDictionaries.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { getDictData, getIndustryListData, getAreaListData, getPositionTreeData, getAreaTreeData } from '@/api/common/index'
  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. 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: getPositionTreeData, // 职位tree
  24. areaTreeData: getAreaTreeData, // 区域tree
  25. industryList: getIndustryListData,
  26. areaList: getAreaListData
  27. }
  28. apiFn[apiType](query).then(data => {
  29. setDict(type, data, Date.now())
  30. resolve({ data })
  31. })
  32. })
  33. }