util.js
3.01 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
'use strict'
//获取应用实例
function formatTime(date) {
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()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
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, index) => {
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;
// console.log(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
}
return true;
}
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) {
var date = new Date();
date.setTime(inputTime*1000);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
return y + '.' + m + '.'+d;
};
function formateTimestamp(start,end){
var startTime = formatDateTime(start)
var endTime = formatDateTime(end)
return startTime+'-'+endTime
}
module.exports = {
formatTime: formatTime,
getDeviceInfo: getDeviceInfo,
shouldDiscardTap,
formatImgUrl,
getBrandID,
getGoodDetailParam,
getImageUrl,
formateTimestamp,
getImageUrlWithWH,
isStringEmpty,
}