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