util.js
3.18 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
'use strict';
/* eslint-disable */
function formatNumber(n) {
n = n.toString();
return n[1] ? n : '0' + n;
}
function formatTime(date) {
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}
function getDeviceInfo(wx) {
let res = wx.getSystemInfoSync(),
windowWidth;
windowWidth = res.windowWidth;
return { windowWidth };
}
function shouldDiscardTap(currentTimeStamp, lastTimeStamp) {
if (lastTimeStamp !== 0 && currentTimeStamp - lastTimeStamp < 250) {
return true;
}
return false;
}
function formatImgUrl(json) {
json.data && json.data.map(item => {
let replaceStr = '{width}';
let url = item.src;
url = url.replace(new RegExp('{width}'), windowWidth * 2).replace(new RegExp('{height}'), 100).replace(new RegExp('{mode}'), '2');
item.src = url;
});
return json;
}
function getImageUrl(url, windowWidth, height) {
if (!url) {
return '';
}
return url.replace(new RegExp('{width}'), windowWidth * 2).replace(new RegExp('{height}'), height ? height : 100).replace(new RegExp('{mode}'), '2');
}
// 自动识别 2倍 3倍图
function getImageUrlWithWH(image_url, image_width, image_height) {
let app = getApp();
const screenWidth = app.globalData.systemInfo.screenWidth;
const pixelRatio = app.globalData.systemInfo.pixelRatio;
const DEVICE_WIDTH_RATIO = screenWidth / 375.0;
if (!image_url) {
return '';
}
return image_url.replace(/{width}/g, parseInt(image_width * DEVICE_WIDTH_RATIO * pixelRatio)).replace(/{height}/g, parseInt(image_height * DEVICE_WIDTH_RATIO * pixelRatio)).replace('{mode}', 2);
}
function getBrandID(url) {
let params = url.split('openby:yohobuy=');
if (params.length === 2) {
let jsonParam = JSON.parse(params[1]);
return jsonParam.params.brand_id;
}
return null;
}
function isStringEmpty(str) {
if (str === undefined || str === null || str === '') {
return true;
} else {
return false;
}
}
function getGoodDetailParam(url) {
let params = url.split('openby:yohobuy=');
if (params.length === 2) {
let jsonParam = JSON.parse(params[1]);
return JSON.stringify(jsonParam.params);
}
return null;
}
function formatDateTime(inputTime) {
let date = new Date();
date.setTime(inputTime * 1000);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
return y + '.' + m + '.' + d;
}
function formateTimestamp(start, end) {
let startTime = formatDateTime(start);
let endTime = formatDateTime(end);
return startTime + '-' + endTime;
}
module.exports = {
formatTime: formatTime,
getDeviceInfo: getDeviceInfo,
shouldDiscardTap,
formatImgUrl,
getBrandID,
getGoodDetailParam,
getImageUrl,
formateTimestamp,
getImageUrlWithWH,
isStringEmpty,
};