06263dceafb3969b4e4a01e983b3a43661ea40cb.svn-base 3.8 KB

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