index.wxs
2.66 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
/**设置千分位 */
var setPrice = function (n = 0.0) {
if ((typeof n == 'string' || typeof n == 'number') && !isNaN(n)) {
n = parseFloat(n).toFixed(2)
var r = getRegExp('(d{1,3})(?=(d{3})+(?:.))', 'g')
var p = n.replace(r, ',')
return p
// var r = getRegExp('/./', 'g')
// var p = n.replace(r, 'test')
// return n
} else {
console.log(n, '不是一个数字', !isNaN(n))
}
}
var threeNumForTwo = {
money_three_for_two_thousands: function (num) {
//num = parseInt(num);
// return num
var num = num + ''
var d = ''
if (num.slice(0, 1) == '-') {
d = num.slice(0, 1)
num = num.slice(1)
}
var len = num.length
var index = num.indexOf('.')
if (index == -1) {
num = num + '.00'
} else if (index + 2 == len) {
num = num + '0'
}
var index = num.indexOf('.') // 字符出现的位置
var num2 = num.slice(-3)
num = num.slice(0, index)
var result = ''
while (num.length > 3) {
result = ',' + num.slice(-3) + result
num = num.slice(0, num.length - 3)
}
if (num) {
result = num + result
}
// return result
return d + (result + num2)
}
}
var filterItemName = function (item,revenueData) {
console.log(item,revenueData);
var unit = item.name == 'upgradeRateData' ? '%' : ''
if (revenueData && revenueData[item.name]) {
return (revenueData[item.name].value || 0) + unit
} else{
return 0 + unit
}
}
var revenueToClass = function (item,revenueData,name) {
console.log(item,revenueData,name);
// var unit = item.name == 'upgradeRateData' ? '%' : ''
if (revenueData && revenueData[item.name]&&revenueData[item.name][name]) {
const val = revenueData[item.name][name]
return val > 0 ? 'up' : 'down'
}
}
var dateFormatSlash = function (date) {
// 验证日期格式
if (!date) return;
console.log(date);
// var regex = /^\d{4}-\d{2}-\d{2}$/; 不支持正则表达式
// if (typeof date !== 'string' || !regex.test(date)) {
// console.log('Invalid date format. Expected format: "YYYY-MM-DD"');
// return
// }
// 解构赋值提取年、月和日
var parts = date.split('-');
var year = parts[0];
var month = parts[1];
var day = parts[2] || '';
// 构建日期字符串
if (day) {
return year + '/' + month + '/' + day; // 带有日的格式:YY/MM/DD
} else {
return year + '/' + month; // 只有年月的格式:YY/MM
}
}
/*
* 导出
*/
module.exports = {
setPrice: setPrice,
filterItemName:filterItemName,
revenueToClass:revenueToClass,
dateFormatSlash:dateFormatSlash,
money_three_for_two_thousands: threeNumForTwo.money_three_for_two_thousands //暴露接口调用
}