JDictSelectUtil.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * 字典 util
  3. * author: scott
  4. * date: 20190109
  5. */
  6. import { ajaxGetDictItems, getDictItemsByCode } from './index';
  7. /**
  8. * 获取字典数组
  9. * 【目前仅表单设计器页面使用该方法】
  10. * @param dictCode 字典Code
  11. * @param isTransformResponse 是否转换返回结果
  12. * @return List<Map>
  13. */
  14. export async function initDictOptions(dictCode, isTransformResponse = true) {
  15. if (!dictCode) {
  16. return '字典Code不能为空!';
  17. }
  18. //优先从缓存中读取字典配置
  19. if (getDictItemsByCode(dictCode)) {
  20. let res = {};
  21. res.result = getDictItemsByCode(dictCode);
  22. res.success = true;
  23. if (isTransformResponse) {
  24. return res.result;
  25. } else {
  26. return res;
  27. }
  28. }
  29. //获取字典数组
  30. return await ajaxGetDictItems(dictCode, {}, { isTransformResponse });
  31. }
  32. /**
  33. * 字典值替换文本通用方法
  34. * @param dictOptions 字典数组
  35. * @param text 字典值
  36. * @return String
  37. */
  38. export function filterDictText(dictOptions, text) {
  39. // --update-begin----author:sunjianlei---date:20200323------for: 字典翻译 text 允许逗号分隔 ---
  40. if (text != null && Array.isArray(dictOptions)) {
  41. let result = [];
  42. // 允许多个逗号分隔,允许传数组对象
  43. let splitText;
  44. if (Array.isArray(text)) {
  45. splitText = text;
  46. } else {
  47. splitText = text.toString().trim().split(',');
  48. }
  49. for (let txt of splitText) {
  50. let dictText = txt;
  51. for (let dictItem of dictOptions) {
  52. if (txt.toString() === dictItem.value.toString()) {
  53. dictText = dictItem.text || dictItem.title || dictItem.label;
  54. break;
  55. }
  56. }
  57. result.push(dictText);
  58. }
  59. return result.join(',');
  60. }
  61. return text;
  62. // --update-end----author:sunjianlei---date:20200323------for: 字典翻译 text 允许逗号分隔 ---
  63. }
  64. /**
  65. * 字典值替换文本通用方法(多选)
  66. * @param dictOptions 字典数组
  67. * @param text 字典值
  68. * @return String
  69. */
  70. export function filterMultiDictText(dictOptions, text) {
  71. //js “!text” 认为0为空,所以做提前处理
  72. if (text === 0 || text === '0') {
  73. if (dictOptions) {
  74. for (let dictItem of dictOptions) {
  75. if (text == dictItem.value) {
  76. return dictItem.text;
  77. }
  78. }
  79. }
  80. }
  81. if (!text || text == 'undefined' || text == 'null' || !dictOptions || dictOptions.length == 0) {
  82. return '';
  83. }
  84. let re = '';
  85. text = text.toString();
  86. let arr = text.split(',');
  87. dictOptions.forEach(function (option) {
  88. if (option) {
  89. for (let i = 0; i < arr.length; i++) {
  90. if (arr[i] === option.value) {
  91. re += option.text + ',';
  92. break;
  93. }
  94. }
  95. }
  96. });
  97. if (re == '') {
  98. return text;
  99. }
  100. return re.substring(0, re.length - 1);
  101. }
  102. /**
  103. * 翻译字段值对应的文本
  104. * @param children
  105. * @returns string
  106. */
  107. export function filterDictTextByCache(dictCode, key) {
  108. if (key == null || key.length == 0) {
  109. return;
  110. }
  111. if (!dictCode) {
  112. return '字典Code不能为空!';
  113. }
  114. //优先从缓存中读取字典配置
  115. if (getDictItemsByCode(dictCode)) {
  116. let item = getDictItemsByCode(dictCode).filter((t) => t['value'] == key);
  117. if (item && item.length > 0) {
  118. return item[0]['text'];
  119. }
  120. }
  121. }
  122. /** 通过code获取字典数组 */
  123. export async function getDictItems(dictCode, params) {
  124. //优先从缓存中读取字典配置
  125. if (getDictItemsByCode(dictCode)) {
  126. let desformDictItems = getDictItemsByCode(dictCode).map((item) => ({
  127. ...item,
  128. label: item.text,
  129. }));
  130. return desformDictItems;
  131. }
  132. //缓存中没有,就请求后台
  133. return await ajaxGetDictItems(dictCode, params)
  134. .then(({ success, result }) => {
  135. if (success) {
  136. let res = result.map((item) => ({ ...item, label: item.text }));
  137. console.log('------- 从DB中获取到了字典-------dictCode : ', dictCode, res);
  138. return Promise.resolve(res);
  139. } else {
  140. console.error('getDictItems error: : ', res);
  141. return Promise.resolve([]);
  142. }
  143. })
  144. .catch((res) => {
  145. console.error('getDictItems error: ', res);
  146. return Promise.resolve([]);
  147. });
  148. }