dictUtils.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import DictService from '@/api/sys/DictService'
  2. export function getDictLabel (type, value, defaultLabel) {
  3. if ((!value && value !== 0) || (!type && type !== 0)) {
  4. if (defaultLabel !== undefined) {
  5. return defaultLabel
  6. } else {
  7. return '--'
  8. }
  9. }
  10. let dictList = JSON.parse(localStorage.getItem('dictList') || '[]')
  11. let dicts = dictList[type]
  12. if (dicts) {
  13. for (let i = 0; i < dicts.length; i++) {
  14. if (dicts[i].value && dicts[i].value.toString() === value.toString()) {
  15. return dicts[i].label
  16. }
  17. }
  18. }
  19. if (defaultLabel !== undefined) {
  20. return defaultLabel
  21. } else {
  22. return '--'
  23. }
  24. }
  25. export function getDictValue (type, label, defaultValue) {
  26. if ((!label && label !== 0) || (!type && type !== 0)) {
  27. if (defaultValue !== undefined) {
  28. return defaultValue
  29. } else {
  30. return '--'
  31. }
  32. }
  33. let dictList = JSON.parse(localStorage.getItem('dictList') || '[]')
  34. let dicts = dictList[type]
  35. if (dicts) {
  36. for (let i = 0; i < dicts.length; i++) {
  37. if (dicts[i].label && dicts[i].label.toString() === label.toString()) {
  38. return dicts[i].value
  39. }
  40. }
  41. }
  42. if (defaultValue !== undefined) {
  43. return defaultValue
  44. } else {
  45. return '--'
  46. }
  47. }
  48. export function getDictList (type) {
  49. if (!type && type !== 0) {
  50. return []
  51. }
  52. let dictList = JSON.parse(localStorage.getItem('dictList') || '[]')
  53. let dicts = dictList[type]
  54. return dicts || []
  55. }
  56. export function refreshDictList () {
  57. new DictService().getDictMap().then(({data}) => {
  58. localStorage.setItem('dictList', JSON.stringify(data || '[]'))
  59. })
  60. }
  61. export default {getDictLabel, getDictValue, getDictList, refreshDictList}