dictUtils.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import $http from './request.js'
  2. const dictListKey = 'dictList'
  3. export function getDictLabel (type, value, defaultLabel) {
  4. if ((!value && value !== 0) || (!type && type !== 0)) {
  5. if (defaultLabel !== undefined) {
  6. return defaultLabel
  7. } else {
  8. return '--'
  9. }
  10. }
  11. let dictList = uni.getStorageSync(dictListKey)
  12. let dicts = dictList[type]
  13. if (dicts) {
  14. for (let i = 0; i < dicts.length; i++) {
  15. if (dicts[i].value && dicts[i].value.toString() === value.toString()) {
  16. return dicts[i].label
  17. }
  18. }
  19. }
  20. if (defaultLabel !== undefined) {
  21. return defaultLabel
  22. } else {
  23. return '--'
  24. }
  25. }
  26. export function getDictValue (type, label, defaultValue) {
  27. if ((!label && label !== 0) || (!type && type !== 0)) {
  28. if (defaultValue !== undefined) {
  29. return defaultValue
  30. } else {
  31. return '--'
  32. }
  33. }
  34. let dictList = uni.getStorageSync(dictListKey)
  35. let dicts = dictList[type]
  36. if (dicts) {
  37. for (let i = 0; i < dicts.length; i++) {
  38. if (dicts[i].label && dicts[i].label.toString() === label.toString()) {
  39. return dicts[i].value
  40. }
  41. }
  42. }
  43. if (defaultValue !== undefined) {
  44. return defaultValue
  45. } else {
  46. return '--'
  47. }
  48. }
  49. export function getDictList (type) {
  50. let dictList = uni.getStorageSync(dictListKey)
  51. if (!type && type !== 0) { // 不传参 返回全部字典
  52. return dictList
  53. }
  54. let dicts = dictList[type]
  55. return dicts || []
  56. }
  57. export function setDictList(dictList){
  58. uni.setStorageSync(dictListKey,dictList);
  59. }
  60. export default {getDictLabel, getDictValue, getDictList, setDictList}