1
0

JDictSelectUtil.js 4.0 KB

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