util.js
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// import moment from 'moment'
import dayjs from "dayjs";
// import Vue from 'vue'
import { Loading, Message, MessageBox } from "element-ui";
let loadingInstance = "";
export const Util = {
/**
* 简单alert
* title 标题
* message 内容
* className 自定义class
* showCancelButton 是否展示取消按钮
* confirmButtonText 确认按钮文案
* cancelButtonText 取消按钮文案
* confirmButtonColor 确认按钮颜色
* cancelButtonColor 取消按钮颜色
*/
msg(options) {
MessageBox({
title: options.title || "",
type: options.type || "error",
message: options.message || "",
className: options.className || "",
showCancelButton: options.showCancelButton || false,
confirmButtonText: options.confirmButtonText || "确认",
cancelButtonText: options.cancelButtonText || "取消",
confirmButtonColor: options.confirmButtonColor || "#1989fa",
cancelButtonColor: options.cancelButtonColor || "#000",
dangerouslyUseHTMLString: true,
callback(action, done) {
if (action == "confirm") {
if (options.success) {
options.success(done);
}
} else {
console.log(done);
// done()
}
},
});
/* Vue.$vux.alert.show({
title: options.title||'',
content: options.message||'',
onShow() {
options.onShow?(options.onShow)():'';
},
onHide() {
options.onHide?(options.onHide)():'';
}
}) */
},
/**
* 提示框
*/
Info(options) {
Message({
message: options.message || "提示",
type: options.type || "error",
duration: options.time || 2000,
onClose: options.onClose || "",
});
},
/**
* 轻提示
*/
Tips(options) {
Message({
message: options.message || "提示",
duration: options.duration || 3000,
type: options.type || "text",
icon: options.icon || "",
});
},
/**
* loading
*/
loading(message) {
loadingInstance = Loading.service({
text: message || "加载中...",
});
},
/**
* 关闭loading
*/
closeLoading() {
loadingInstance.close();
},
/**
* 校验手机号码格式
*/
checkPhone(mobile) {
const myreg
= /^(13\d|15\d|16\d|17\d|18\d|19\d|14\d)\d{8}$/;
return myreg.test(mobile);
},
/**
* 校验固定电话
*/
validationTel(tel) {
const reg = /^0\d{2,3}-\d{7,8}$/;
return reg.test(tel);
},
/**
* 是否特殊字符
*/
isSpecialLetter(str) {
const reg = new RegExp(
"[`~!@#$^&*=|{}':;,[\\]<>?!¥…—【】‘;:”“。,、? ]",
);
return reg.test(str);
},
/**
* 只能输入英文字母和数字,不能输入中文
*/
validationInput(str) {
const reg = /[^\w\.\/]/gi // eslint-disable-line
return reg.test(str);
},
/**
* 获取当前时间
*/
getCurrentDate() {
const time = new Date();
const y = time.getFullYear();
const m = time.getMonth() + 1;
const d = time.getDate();
return {
date: y + "/" + m + "/" + d,
};
},
/**
* 获取浏览器参数
*/
getUrlParams(name) {
const vars = [];
let hash;
const oParams = {};
const hashes = window.location.href
.slice(window.location.href.indexOf("?") + 1)
.split("&");
for (let i = 0; i < hashes.length; i++) {
hash = hashes[i].split("=");
vars.push(hash[0]);
oParams[hash[0]] = hash[1];
}
console.log(vars);
if (name) {
return oParams[name];
} else {
return oParams;
}
},
/**
* 根据当前时间,计算往后30天的日期
*/
getAfterDate(day) {
const date1 = new Date();
const date2 = new Date(date1);
let time = 30;
if (day) {
time = day;
}
date2.setDate(date1.getDate() + time);
const _date
= date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
return {
date: _date,
year: date2.getFullYear(),
month: date2.getMonth() + 1,
day: date2.getDate(),
};
},
/**
* 日期格式转换
*/
dateFormate(str, type) {
let txt = "YYYY-MM-DD";
if (type) {
txt = type;
}
const m = dayjs(str).format(txt);
return m == "Invalid Date" ? "-" : m;
},
/**
* 字段截取
*/
cutStr(str, num) {
if (!str) {
return "";
}
let m = 10;
let n = str;
if (num) {
m = num;
}
if (str.length > m) {
n = str.slice(0, m) + "...";
}
return n;
},
// /**
// * 深克隆 所有原型链上的对象和属性
// * 例: const bar=deepClone(foo)
// * console.log(bar===foo) false
// * @param {*} obj
// */
deepClone(obj) {
const clone = require("rfdc")({ proto: false, circles: false });
return clone(obj);
},
// deepClone: function (obj) {
// // Handle the 3 simple types, and null or undefined or function
// if (obj == null || typeof obj !== "object") return obj
// // Handle Date
// if (obj instanceof Date) {
// const copy = new Date()
// copy.setTime(obj.getTime())
// return copy
// }
// // Handle Array or Object
// if ((obj instanceof Array) | (obj instanceof Object)) {
// const copy = obj instanceof Array ? [] : {}
// for (var attr in obj) {
// // if (Object.prototype.hasOwnProperty.call(attr)) {
// if (obj.hasOwnProperty(attr)) {
// copy[attr] = Util.deepClone(obj[attr])
// // console.log("copy[attr] ", copy[attr])
// }
// }
// return copy
// }
// throw new Error("Unable to clone obj! Its type isn't supported.")
// },
/**
* url下载文件
*
*/
downLoadFile(url) {
// console.log(row)
// return
const Temp = document.createElement("a");
// Temp.href = process.env.VUE_APP_BASE_URL + '/article/export.do?articleId=' + row.id
url
= url
+ `${url.includes("?") ? "&" : "?"}`
+ "timestamp="
+ new Date().getTime();
Temp.href = url;
// console.log(url)
// console.log(Temp.href)
document.body.append(Temp);
Temp.click();
document.body.removeChild(Temp);
},
/**
* @param {object} json
* @param {object} type: 默认不传 ==>全部小写;传1 ==>全部大写;传2 ==>首字母大写
* 将json的key值进行大小写转换
*/
jsonKeysToCase(json, type) {
if (typeof json == "object") {
let tempJson = JSON.parse(JSON.stringify(json));
toCase(tempJson);
return tempJson;
} else {
return json;
}
function toCase(json) {
if (typeof json == "object") {
if (Array.isArray(json)) {
json.forEach((item) => {
toCase(item);
});
} else {
for (let key in json) {
let item = json[key];
if (typeof item == "object") {
toCase(item);
}
delete json[key];
switch (type) {
case 1:
// key值全部大写
json[key.toLocaleUpperCase()] = item;
break;
case 2:
// key值首字母大写,其余小写
json[
key.substring(0, 1).toLocaleUpperCase()
+ key.substring(1).toLocaleLowerCase()
] = item;
break;
default:
// 默认key值全部小写
json[key.toLocaleLowerCase()] = item;
break;
}
}
}
}
}
},
// 判断字段为空时显示
fieldShow(field, show) {
return field || show;
},
// 整数补零显示两位小数
returnFloat(num) {
num = num.toString();
if (num.includes(".")) {
let newVal = num.split(".");
if (newVal[1].length == 1) {
newVal = newVal.join(".");
newVal += "0";
num = newVal;
}
} else {
num += ".00";
}
return num;
},
getExplorer() {
let ua = navigator.userAgent.toLocaleLowerCase();
let browserType = null;
if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
browserType = "IE";
} else if (ua.match(/firefox/) != null) {
browserType = "火狐";
} else if (ua.match(/ubrowser/) != null) {
browserType = "UC";
} else if (ua.match(/opera/) != null) {
browserType = "欧朋";
} else if (ua.match(/bidubrowser/) != null) {
browserType = "百度";
} else if (ua.match(/metasr/) != null) {
browserType = "搜狗";
} else if (
ua.match(/tencenttraveler/) != null
|| ua.match(/qqbrowse/) != null
) {
browserType = "QQ";
} else if (ua.match(/maxthon/) != null) {
browserType = "遨游";
} else {
browserType = "谷歌";
}
return browserType;
},
validateNumber(value) {
// 尝试将输入转换为浮点数
const number = Number.parseFloat(value);
// 检查是否成功转换为数字,并且是否大于0
if (isNaN(number) || number <= 0) {
return false;
}
// 将数字转换为字符串,并检查小数点后的长度
const numberString = number.toString();
const decimalIndex = numberString.indexOf(".");
// 如果没有小数点,则视为有效(例如,1或100)
if (decimalIndex === -1) {
return true;
}
// 获取小数点后的部分
const decimalPart = numberString.slice(decimalIndex + 1);
// 检查小数点后的长度是否不超过2
return decimalPart.length <= 2;
},
getStatusItem(baseData, optKey, row, rowKey) {
if (!baseData || !baseData[optKey] || (!row && row !== 0))
return null;
let value = rowKey ? row[rowKey] : row;
let item = baseData[optKey].find(item => item.confId == value);
return (item && item.confValue) || "-";
},
getStatusColor(status, optKey) {
if (status === null || status === undefined)
return "#C0C4CC";
const map = (this.baseData && this.baseData[optKey]) || {};
return map[status] || "#C0C4CC";
},
getOptionsList(that, formName, hasAll = true, refreshSigle) {
if (refreshSigle) {
let item = that[formName].find(k => k.name == refreshSigle);
let index = that[formName].findIndex(k => k.name == refreshSigle);
that.$set(
that[formName][index],
"optionList",
deepClone(that.baseData[item.optionsName]),
);
if (
hasAll
&& that.baseData[item.optionsName].length
&& that.baseData[item.optionsName][0][
that[formName][index].optionLabel
] !== "全部"
) {
const temp = {};
temp[that[formName][index].optionId] = "";
temp[that[formName][index].optionLabel] = "全部";
that[formName][index].optionList.unshift(temp);
}
} else {
that[formName].forEach((item, index) => {
if (item.type === "select" && that.baseData[item.optionsName]?.length) {
that.$set(
that[formName][index],
"optionList",
deepClone(that.baseData[item.optionsName]),
);
if (
hasAll
&& that.baseData[item.optionsName].length
&& that.baseData[item.optionsName][0][
that[formName][index].optionLabel
] !== "全部"
) {
const temp = {};
temp[that[formName][index].optionId] = "";
temp[that[formName][index].optionLabel] = "全部";
that[formName][index].optionList.unshift(temp);
}
}
});
}
},
};
export const showModal = Util.msg;
export const showToast = Util.Info;
export const showLoading = Util.loading;
export const hideLoading = Util.closeLoading;
export const deepClone = Util.deepClone;
export const downLoadFile = Util.downLoadFile;
export const returnFloat = Util.returnFloat;
export const getExplorer = Util.getExplorer;
export const getStatusItem = Util.getStatusItem;
export const getStatusColor = Util.getStatusColor;
export const getOptionsList = Util.getOptionsList;