value.js
2.3 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
"use strict";
const common_vendor = require("../../../../common/vendor.js");
const value = {
computed: {
// 经处理后需要显示的值
value() {
const {
text,
mode,
format,
href
} = this;
if (mode === "price") {
if (!/^\d+(\.\d+)?$/.test(text)) {
common_vendor.index.$u.error("金额模式下,text参数需要为金额格式");
}
if (common_vendor.index.$u.test.func(format)) {
return format(text);
}
return common_vendor.index.$u.priceFormat(text, 2);
}
if (mode === "date") {
!common_vendor.index.$u.test.date(text) && common_vendor.index.$u.error("日期模式下,text参数需要为日期或时间戳格式");
if (common_vendor.index.$u.test.func(format)) {
return format(text);
}
if (format) {
return common_vendor.index.$u.timeFormat(text, format);
}
return common_vendor.index.$u.timeFormat(text, "yyyy-mm-dd");
}
if (mode === "phone") {
if (common_vendor.index.$u.test.func(format)) {
return format(text);
}
if (format === "encrypt") {
return `${text.substr(0, 3)}****${text.substr(7)}`;
}
return text;
}
if (mode === "name") {
!(typeof text === "string") && common_vendor.index.$u.error("姓名模式下,text参数需要为字符串格式");
if (common_vendor.index.$u.test.func(format)) {
return format(text);
}
if (format === "encrypt") {
return this.formatName(text);
}
return text;
}
if (mode === "link") {
!common_vendor.index.$u.test.url(href) && common_vendor.index.$u.error("超链接模式下,href参数需要为URL格式");
return text;
}
return text;
}
},
methods: {
// 默认的姓名脱敏规则
formatName(name) {
let value2 = "";
if (name.length === 2) {
value2 = name.substr(0, 1) + "*";
} else if (name.length > 2) {
let char = "";
for (let i = 0, len = name.length - 2; i < len; i++) {
char += "*";
}
value2 = name.substr(0, 1) + char + name.substr(-1, 1);
} else {
value2 = name;
}
return value2;
}
}
};
exports.value = value;