helper.wxs
3.13 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
/* global getRegExp */
var regExpWidth = getRegExp('{width}', 'g');
var regExpHeight = getRegExp('{height}', 'g');
var regExpMode = getRegExp('{mode}', 'g');
var regExpQg = getRegExp('/q/d+', 'g');
var regExpQ = getRegExp('/q/d+');
var regExpQuality = getRegExp('/quality/d+');
var regExpQualityg = getRegExp('/quality/d+', 'g');
var regExpImageView = getRegExp('imageView');
var regExpImageMogr = getRegExp('imageMogr');
var defaultQuality = 75;
function image(imgUrl, w, h, mode, q) {
var urls,
query,
url;
var params = {
w: w,
h: h,
mode: mode || 2,
q: q || defaultQuality
};
if (imgUrl && (typeof imgUrl === 'string')) {
urls = imgUrl.split('?');
query = urls[1] || '';
url = urls[0];
if (url.indexOf('http:') === 0) {
url = url.replace('http:', 'https:');
}
if (!query || query === 'imageslim') {
url += params.q === defaultQuality ? '?imageslim' : '?imageView2/0/interlace/1/q/' + params.q;
imgUrl = url;
} else {
imgUrl = imgUrl.replace(regExpWidth, params.w)
.replace(regExpHeight, params.h)
.replace(regExpMode, (params.mode));
if (regExpImageView.test(query)) { // imageView2 || imageView
if (!regExpQ.test(query)) {
imgUrl += '/q/' + params.q;
} else {
imgUrl = imgUrl.replace(regExpQg, '/q/' + params.q);
}
} else if (regExpImageMogr.test(query)) {
if (!regExpQuality.test(query)) {
imgUrl += '/quality/' + params.q;
} else {
imgUrl = imgUrl.replace(regExpQualityg, '/quality/' + params.q);
}
}
}
return imgUrl;
} else {
return '';
}
}
function round(num, precision) {
num = +num;
if (num !== 0 && (!num || isNaN(num))) {
return '';
}
precision = precision > 0 ? precision.toFixed() : 2;
return num.toFixed(precision);
}
// 如果图片没有imageView2,则默认添加imageView2
function imgView(imgSrc, w, h, mode, q) {
var imgUrl = imgSrc || '';
var indexOf = imgUrl.indexOf('?');
if (!imgUrl) {
return '';
}
if (imgUrl.indexOf('http://') === imgUrl.indexOf('https://')) {
return imgUrl;
}
if (indexOf === -1) {
imgUrl += '?imageView2/{mode}/w/{width}/h/{height}';
return image(imgUrl, w, h, mode, q);
}
if (indexOf > -1) {
return image(imgUrl, w, h, mode, q);
}
return imgUrl;
}
function dateFormat(times, format) {
// ios 日期格式为2017/05/06 12:10:30
var date = times ? getDate(times.replace(getRegExp('-', 'g'), '/')) : getDate();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
format = format || 'YYYY/MM/DD HH:mm:ss';
[year, month, day, hour, minute, second] = [year, month, day, hour, minute, second].map(function(n) {
n = n.toString();
return n[1] ? n : '0' + n;
});
return format.replace('YYYY', year).replace('MM', month).replace('DD', day)
.replace('HH', hour).replace('mm', minute).replace('ss', second);
}
module.exports = {
dateFormat: dateFormat,
image: image,
imgView: imgView,
round: round
};