作者 RuoYi

空值不进行回显数据字典

  1 +
  2 +
1 /** 3 /**
2 * 通用js方法封装处理 4 * 通用js方法封装处理
3 * Copyright (c) 2019 ruoyi 5 * Copyright (c) 2019 ruoyi
@@ -5,130 +7,133 @@ @@ -5,130 +7,133 @@
5 7
6 // 日期格式化 8 // 日期格式化
7 export function parseTime(time, pattern) { 9 export function parseTime(time, pattern) {
8 - if (arguments.length === 0 || !time) {  
9 - return null  
10 - }  
11 - const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'  
12 - let date  
13 - if (typeof time === 'object') {  
14 - date = time  
15 - } else {  
16 - if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {  
17 - time = parseInt(time)  
18 - } else if (typeof time === 'string') {  
19 - time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm),'');  
20 - }  
21 - if ((typeof time === 'number') && (time.toString().length === 10)) {  
22 - time = time * 1000  
23 - }  
24 - date = new Date(time)  
25 - }  
26 - const formatObj = {  
27 - y: date.getFullYear(),  
28 - m: date.getMonth() + 1,  
29 - d: date.getDate(),  
30 - h: date.getHours(),  
31 - i: date.getMinutes(),  
32 - s: date.getSeconds(),  
33 - a: date.getDay()  
34 - }  
35 - const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {  
36 - let value = formatObj[key]  
37 - // Note: getDay() returns 0 on Sunday  
38 - if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }  
39 - if (result.length > 0 && value < 10) {  
40 - value = '0' + value  
41 - }  
42 - return value || 0  
43 - })  
44 - return time_str 10 + if (arguments.length === 0 || !time) {
  11 + return null
  12 + }
  13 + const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  14 + let date
  15 + if (typeof time === 'object') {
  16 + date = time
  17 + } else {
  18 + if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  19 + time = parseInt(time)
  20 + } else if (typeof time === 'string') {
  21 + time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
  22 + }
  23 + if ((typeof time === 'number') && (time.toString().length === 10)) {
  24 + time = time * 1000
  25 + }
  26 + date = new Date(time)
  27 + }
  28 + const formatObj = {
  29 + y: date.getFullYear(),
  30 + m: date.getMonth() + 1,
  31 + d: date.getDate(),
  32 + h: date.getHours(),
  33 + i: date.getMinutes(),
  34 + s: date.getSeconds(),
  35 + a: date.getDay()
  36 + }
  37 + const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  38 + let value = formatObj[key]
  39 + // Note: getDay() returns 0 on Sunday
  40 + if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  41 + if (result.length > 0 && value < 10) {
  42 + value = '0' + value
  43 + }
  44 + return value || 0
  45 + })
  46 + return time_str
45 } 47 }
46 48
47 // 表单重置 49 // 表单重置
48 export function resetForm(refName) { 50 export function resetForm(refName) {
49 - if (this.$refs[refName]) {  
50 - this.$refs[refName].resetFields();  
51 - } 51 + if (this.$refs[refName]) {
  52 + this.$refs[refName].resetFields();
  53 + }
52 } 54 }
53 55
54 // 添加日期范围 56 // 添加日期范围
55 export function addDateRange(params, dateRange, propName) { 57 export function addDateRange(params, dateRange, propName) {
56 - let search = params;  
57 - search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};  
58 - dateRange = Array.isArray(dateRange) ? dateRange : [];  
59 - if (typeof (propName) === 'undefined') {  
60 - search.params['beginTime'] = dateRange[0];  
61 - search.params['endTime'] = dateRange[1];  
62 - } else {  
63 - search.params['begin' + propName] = dateRange[0];  
64 - search.params['end' + propName] = dateRange[1];  
65 - }  
66 - return search; 58 + let search = params;
  59 + search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  60 + dateRange = Array.isArray(dateRange) ? dateRange : [];
  61 + if (typeof (propName) === 'undefined') {
  62 + search.params['beginTime'] = dateRange[0];
  63 + search.params['endTime'] = dateRange[1];
  64 + } else {
  65 + search.params['begin' + propName] = dateRange[0];
  66 + search.params['end' + propName] = dateRange[1];
  67 + }
  68 + return search;
67 } 69 }
68 70
69 -// 回显数据字典 71 +// 回显数据字典
70 export function selectDictLabel(datas, value) { 72 export function selectDictLabel(datas, value) {
71 - var actions = [];  
72 - Object.keys(datas).some((key) => {  
73 - if (datas[key].value == ('' + value)) {  
74 - actions.push(datas[key].label);  
75 - return true;  
76 - }  
77 - })  
78 - return actions.join(''); 73 + var actions = [];
  74 + Object.keys(datas).some((key) => {
  75 + if (datas[key].value == ('' + value)) {
  76 + actions.push(datas[key].label);
  77 + return true;
  78 + }
  79 + })
  80 + return actions.join('');
79 } 81 }
80 82
81 // 回显数据字典(字符串数组) 83 // 回显数据字典(字符串数组)
82 export function selectDictLabels(datas, value, separator) { 84 export function selectDictLabels(datas, value, separator) {
83 - var actions = [];  
84 - var currentSeparator = undefined === separator ? "," : separator;  
85 - var temp = value.split(currentSeparator);  
86 - Object.keys(value.split(currentSeparator)).some((val) => {  
87 - Object.keys(datas).some((key) => {  
88 - if (datas[key].value == ('' + temp[val])) {  
89 - actions.push(datas[key].label + currentSeparator);  
90 - }  
91 - })  
92 - })  
93 - return actions.join('').substring(0, actions.join('').length - 1); 85 + if(value === undefined) {
  86 + return "";
  87 + }
  88 + var actions = [];
  89 + var currentSeparator = undefined === separator ? "," : separator;
  90 + var temp = value.split(currentSeparator);
  91 + Object.keys(value.split(currentSeparator)).some((val) => {
  92 + Object.keys(datas).some((key) => {
  93 + if (datas[key].value == ('' + temp[val])) {
  94 + actions.push(datas[key].label + currentSeparator);
  95 + }
  96 + })
  97 + })
  98 + return actions.join('').substring(0, actions.join('').length - 1);
94 } 99 }
95 100
96 // 字符串格式化(%s ) 101 // 字符串格式化(%s )
97 export function sprintf(str) { 102 export function sprintf(str) {
98 - var args = arguments, flag = true, i = 1;  
99 - str = str.replace(/%s/g, function () {  
100 - var arg = args[i++];  
101 - if (typeof arg === 'undefined') {  
102 - flag = false;  
103 - return '';  
104 - }  
105 - return arg;  
106 - });  
107 - return flag ? str : ''; 103 + var args = arguments, flag = true, i = 1;
  104 + str = str.replace(/%s/g, function () {
  105 + var arg = args[i++];
  106 + if (typeof arg === 'undefined') {
  107 + flag = false;
  108 + return '';
  109 + }
  110 + return arg;
  111 + });
  112 + return flag ? str : '';
108 } 113 }
109 114
110 // 转换字符串,undefined,null等转化为"" 115 // 转换字符串,undefined,null等转化为""
111 export function parseStrEmpty(str) { 116 export function parseStrEmpty(str) {
112 - if (!str || str == "undefined" || str == "null") {  
113 - return "";  
114 - }  
115 - return str; 117 + if (!str || str == "undefined" || str == "null") {
  118 + return "";
  119 + }
  120 + return str;
116 } 121 }
117 122
118 // 数据合并 123 // 数据合并
119 export function mergeRecursive(source, target) { 124 export function mergeRecursive(source, target) {
120 - for (var p in target) {  
121 - try {  
122 - if (target[p].constructor == Object) {  
123 - source[p] = mergeRecursive(source[p], target[p]);  
124 - } else {  
125 - source[p] = target[p];  
126 - }  
127 - } catch(e) {  
128 - source[p] = target[p];  
129 - } 125 + for (var p in target) {
  126 + try {
  127 + if (target[p].constructor == Object) {
  128 + source[p] = mergeRecursive(source[p], target[p]);
  129 + } else {
  130 + source[p] = target[p];
  131 + }
  132 + } catch (e) {
  133 + source[p] = target[p];
130 } 134 }
131 - return source; 135 + }
  136 + return source;
132 }; 137 };
133 138
134 /** 139 /**
@@ -139,47 +144,47 @@ export function mergeRecursive(source, target) { @@ -139,47 +144,47 @@ export function mergeRecursive(source, target) {
139 * @param {*} children 孩子节点字段 默认 'children' 144 * @param {*} children 孩子节点字段 默认 'children'
140 */ 145 */
141 export function handleTree(data, id, parentId, children) { 146 export function handleTree(data, id, parentId, children) {
142 - let config = {  
143 - id: id || 'id',  
144 - parentId: parentId || 'parentId',  
145 - childrenList: children || 'children'  
146 - };  
147 -  
148 - var childrenListMap = {};  
149 - var nodeIds = {};  
150 - var tree = [];  
151 -  
152 - for (let d of data) {  
153 - let parentId = d[config.parentId];  
154 - if (childrenListMap[parentId] == null) {  
155 - childrenListMap[parentId] = [];  
156 - }  
157 - nodeIds[d[config.id]] = d;  
158 - childrenListMap[parentId].push(d);  
159 - }  
160 -  
161 - for (let d of data) {  
162 - let parentId = d[config.parentId];  
163 - if (nodeIds[parentId] == null) {  
164 - tree.push(d);  
165 - }  
166 - }  
167 -  
168 - for (let t of tree) {  
169 - adaptToChildrenList(t);  
170 - }  
171 -  
172 - function adaptToChildrenList(o) {  
173 - if (childrenListMap[o[config.id]] !== null) {  
174 - o[config.childrenList] = childrenListMap[o[config.id]];  
175 - }  
176 - if (o[config.childrenList]) {  
177 - for (let c of o[config.childrenList]) {  
178 - adaptToChildrenList(c);  
179 - }  
180 - }  
181 - }  
182 - return tree; 147 + let config = {
  148 + id: id || 'id',
  149 + parentId: parentId || 'parentId',
  150 + childrenList: children || 'children'
  151 + };
  152 +
  153 + var childrenListMap = {};
  154 + var nodeIds = {};
  155 + var tree = [];
  156 +
  157 + for (let d of data) {
  158 + let parentId = d[config.parentId];
  159 + if (childrenListMap[parentId] == null) {
  160 + childrenListMap[parentId] = [];
  161 + }
  162 + nodeIds[d[config.id]] = d;
  163 + childrenListMap[parentId].push(d);
  164 + }
  165 +
  166 + for (let d of data) {
  167 + let parentId = d[config.parentId];
  168 + if (nodeIds[parentId] == null) {
  169 + tree.push(d);
  170 + }
  171 + }
  172 +
  173 + for (let t of tree) {
  174 + adaptToChildrenList(t);
  175 + }
  176 +
  177 + function adaptToChildrenList(o) {
  178 + if (childrenListMap[o[config.id]] !== null) {
  179 + o[config.childrenList] = childrenListMap[o[config.id]];
  180 + }
  181 + if (o[config.childrenList]) {
  182 + for (let c of o[config.childrenList]) {
  183 + adaptToChildrenList(c);
  184 + }
  185 + }
  186 + }
  187 + return tree;
183 } 188 }
184 189
185 /** 190 /**
@@ -187,34 +192,34 @@ export function handleTree(data, id, parentId, children) { @@ -187,34 +192,34 @@ export function handleTree(data, id, parentId, children) {
187 * @param {*} params 参数 192 * @param {*} params 参数
188 */ 193 */
189 export function tansParams(params) { 194 export function tansParams(params) {
190 - let result = ''  
191 - for (const propName of Object.keys(params)) {  
192 - const value = params[propName];  
193 - var part = encodeURIComponent(propName) + "=";  
194 - if (value !== null && typeof (value) !== "undefined") {  
195 - if (typeof value === 'object') {  
196 - for (const key of Object.keys(value)) {  
197 - if (value[key] !== null && typeof (value[key]) !== 'undefined') {  
198 - let params = propName + '[' + key + ']';  
199 - var subPart = encodeURIComponent(params) + "=";  
200 - result += subPart + encodeURIComponent(value[key]) + "&";  
201 - }  
202 - }  
203 - } else {  
204 - result += part + encodeURIComponent(value) + "&";  
205 - }  
206 - }  
207 - }  
208 - return result 195 + let result = ''
  196 + for (const propName of Object.keys(params)) {
  197 + const value = params[propName];
  198 + var part = encodeURIComponent(propName) + "=";
  199 + if (value !== null && typeof (value) !== "undefined") {
  200 + if (typeof value === 'object') {
  201 + for (const key of Object.keys(value)) {
  202 + if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  203 + let params = propName + '[' + key + ']';
  204 + var subPart = encodeURIComponent(params) + "=";
  205 + result += subPart + encodeURIComponent(value[key]) + "&";
  206 + }
  207 + }
  208 + } else {
  209 + result += part + encodeURIComponent(value) + "&";
  210 + }
  211 + }
  212 + }
  213 + return result
209 } 214 }
210 215
211 // 验证是否为blob格式 216 // 验证是否为blob格式
212 export async function blobValidate(data) { 217 export async function blobValidate(data) {
213 - try {  
214 - const text = await data.text();  
215 - JSON.parse(text);  
216 - return false;  
217 - } catch (error) {  
218 - return true;  
219 - }  
220 -} 218 + try {
  219 + const text = await data.text();
  220 + JSON.parse(text);
  221 + return false;
  222 + } catch (error) {
  223 + return true;
  224 + }
  225 +}