util.js
9.85 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
'use strict'
//获取应用实例
/**
* 时间戳转化为年 月 日 时 分 秒
* time: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
* formatTimeByDefined(1488481383,'Y/M/D h:m:s') => 2017/03/03 03:03:03
*/
function formatTimeByDefined(time, format) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
var returnArr = [];
var date = new Date(time * 1000);
returnArr.push(date.getFullYear());
returnArr.push(formatNumber(date.getMonth() + 1));
returnArr.push(formatNumber(date.getDate()));
returnArr.push(formatNumber(date.getHours()));
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber(date.getSeconds()));
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
return format;
}
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 = tt.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');
}
function formatImageUrl(url, width, height, mode){
url = url || '';
if (url && url.indexOf('?') === -1) {
url = url + '?imageView2/{mode}/w/{width}/h/{height}';
}
return url.replace(/{width}/g, width).replace(/{height}/g, height).replace('{mode}', 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
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function getSknFromUrl(url) {
if (!url) return '';
url = decodeURIComponent(url).toLowerCase();
let components = url.split('/');
let lastElement = components.pop();
let startLegal = url.startsWith('https://m.yohobuy.com/product') || url.startsWith('http://m.yohobuy.com/product');
let endLegal = lastElement.endsWith('.html');
if (!startLegal || !endLegal) {
return '';
}
let lastElementArray = lastElement.split('.');
return lastElementArray[0];
}
function getShopIdFromUrl(url) {
if (!url) return '';
url = decodeURIComponent(url).toLowerCase();
let components = url.split('/');
let lastElement = components.pop();
let startLegal = url.startsWith('https://m.yohobuy.com/shop') || url.startsWith('http://m.yohobuy.com/shop');
let endLegal = lastElement.endsWith('.html');
if (!startLegal || !endLegal) {
return '';
}
let nameAndId = lastElement.split('.')[0];
let shopId = nameAndId.split('-').pop();
return shopId;
}
function getShopID(url) {
let params = url.split("openby:yohobuy=")
if (params.length == 2) {
let jsonParam = JSON.parse(params[1])
return jsonParam.params.shop_id;
}
return null;
}
function contains(arr, obj) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == obj) {
return i;
}
}
return -1;
}
function getGuangType(url) {
let params = url.split("openby:yohobuy=")
if (params.length == 2) {
let jsonParam = JSON.parse(params[1])
return jsonParam.params.type;
}
return null;
}
// public method for encoding
function base64Encode(input) {
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = _utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
}
// public method for decoding
function base64Decode(input) {
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = _utf8_decode(output);
return output;
}
function _utf8_encode(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
// private method for UTF-8 decoding
function _utf8_decode(utftext) {
var string = "";
var i = 0;
var c = 0;
var c1 = 0;
var c2 = 0;
var c3 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
/*获取缓存
key: 关键词
fromPage: 来源页面名称
**/
function getYHStorageSync(key,fromPage) {
// console.log(key)
// console.log(fromPage)
try {
let res = tt.getStorageSync(key);
// console.log(res);
// console.log('-------------------------')
return res;
} catch (e) {
tt.reportAnalytics('get_storage_error', {
key: key,
error: e.message,
error_code: e.code,
from_page: fromPage,
});
console.log(e)
return null;
}
}
/*设置缓存
key: 关键词
value: 内容
fromPage: 来源页面名称
**/
function setStorageSync(key,value, fromPage) {
try {
tt.setStorageSync(key, value);
return true;
} catch (e) {
return false;
}
}
/*清除缓存
key: 关键词
fromPage: 来源页面名称
**/
function removeStorageSync(key, fromPage) {
try {
tt.removeStorageSync(key);
return true;
} catch (e) {
return false;
}
}
module.exports = {
formatTimeByDefined,
formatTime: formatTime,
getDeviceInfo: getDeviceInfo,
shouldDiscardTap,
formatImgUrl,
formatImageUrl,
getBrandID,
getGoodDetailParam,
getImageUrl,
formateTimestamp,
getImageUrlWithWH,
isStringEmpty,
getParameterByName,
getSknFromUrl,
getShopIdFromUrl,
getShopID,
contains,
getGuangType,
base64Encode,
base64Decode,
getYHStorageSync,
setStorageSync,
removeStorageSync
}