DictOptions.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { mergeRecursive } from '@/utils/zk'
  2. import dictConverter from './DictConverter'
  3. export const options = {
  4. metas: {
  5. '*': {
  6. /**
  7. * 字典请求,方法签名为function(dictMeta: DictMeta): Promise
  8. */
  9. request: (dictMeta) => {
  10. console.log(`load dict ${dictMeta.type}`)
  11. return Promise.resolve([])
  12. },
  13. /**
  14. * 字典响应数据转换器,方法签名为function(response: Object, dictMeta: DictMeta): DictData
  15. */
  16. responseConverter,
  17. labelField: 'label',
  18. valueField: 'value',
  19. },
  20. },
  21. /**
  22. * 默认标签字段
  23. */
  24. DEFAULT_LABEL_FIELDS: ['label', 'name', 'title'],
  25. /**
  26. * 默认值字段
  27. */
  28. DEFAULT_VALUE_FIELDS: ['value', 'id', 'uid', 'key'],
  29. }
  30. /**
  31. * 映射字典
  32. * @param {Object} response 字典数据
  33. * @param {DictMeta} dictMeta 字典元数据
  34. * @returns {DictData}
  35. */
  36. function responseConverter(response, dictMeta) {
  37. const dicts = response.resysDictionary.sysDictionaryDetails instanceof Array ? response.resysDictionary.sysDictionaryDetails : response
  38. if (dicts === undefined) {
  39. console.warn(`no dict data of "${dictMeta.type}" found in the response`)
  40. return []
  41. }
  42. return dicts.map(d => dictConverter(d, dictMeta))
  43. }
  44. export function mergeOptions(src) {
  45. mergeRecursive(options, src)
  46. }
  47. export default options