Authored by 肖亚东

工具类修改,使用utils下的index文件 — review by 李其昌

1 import MessageService from '../MessageService.js' 1 import MessageService from '../MessageService.js'
2 -import util from '../../../utils/util.js' 2 +import { formatTimeByDefined } from '../../../utils'
3 3
4 Page({ 4 Page({
5 5
@@ -94,7 +94,7 @@ Page({ @@ -94,7 +94,7 @@ Page({
94 if (data && data.code == 200 && data.data.list) { 94 if (data && data.code == 200 && data.data.list) {
95 let msgList = data.data.list; 95 let msgList = data.data.list;
96 const list = msgList.map(item => { 96 const list = msgList.map(item => {
97 - let createTime = util.formatTimeByDefined(item.createTime, 'Y.M.D h:m:s'); 97 + let createTime = formatTimeByDefined(item.createTime, 'Y.M.D h:m:s');
98 item.createTime = createTime; 98 item.createTime = createTime;
99 return item; 99 return item;
100 }) 100 })
1 import MessageService from '../MessageService.js' 1 import MessageService from '../MessageService.js'
2 -import util from '../../../utils/util.js' 2 +import { formatTimeByDefined } from '../../../utils'
3 3
4 Page({ 4 Page({
5 5
@@ -62,7 +62,7 @@ Page({ @@ -62,7 +62,7 @@ Page({
62 if (data && data.code == 200 && data.data.list) { 62 if (data && data.code == 200 && data.data.list) {
63 let msgList = data.data.list; 63 let msgList = data.data.list;
64 const list = msgList.map(item => { 64 const list = msgList.map(item => {
65 - let createTime = util.formatTimeByDefined(item.createTime, 'Y.M.D h:m:s'); 65 + let createTime = formatTimeByDefined(item.createTime, 'Y.M.D h:m:s');
66 item.createTime = createTime; 66 item.createTime = createTime;
67 return item; 67 return item;
68 }) 68 })
@@ -45,3 +45,36 @@ export const throttle = (delay, action) => { // 函数节流器,定义函数 @@ -45,3 +45,36 @@ export const throttle = (delay, action) => { // 函数节流器,定义函数
45 } 45 }
46 }; 46 };
47 }; 47 };
  48 +
  49 +/**
  50 + * 时间戳转化为年 月 日 时 分 秒
  51 + * time: 传入时间戳
  52 + * format:返回格式,支持自定义,但参数必须与formateArr里保持一致
  53 + * formatTimeByDefined(1488481383,'Y/M/D h:m:s') => 2017/03/03 03:03:03
  54 +*/
  55 +
  56 +export const formatTimeByDefined = function (time, format) {
  57 + var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  58 + var returnArr = [];
  59 +
  60 + var date = new Date(time * 1000);
  61 + returnArr.push(date.getFullYear());
  62 + returnArr.push(formatNumber(date.getMonth() + 1));
  63 + returnArr.push(formatNumber(date.getDate()));
  64 +
  65 + returnArr.push(formatNumber(date.getHours()));
  66 + returnArr.push(formatNumber(date.getMinutes()));
  67 + returnArr.push(formatNumber(date.getSeconds()));
  68 +
  69 + for (var i in returnArr) {
  70 + format = format.replace(formateArr[i], returnArr[i]);
  71 + }
  72 + return format;
  73 +}
  74 +
  75 +function formatNumber(n) {
  76 + n = n.toString()
  77 + return n[1] ? n : '0' + n
  78 +}
  79 +
  80 +
1 -'use strict'  
2 -  
3 -//获取应用实例  
4 -  
5 -/**  
6 - * 时间戳转化为年 月 日 时 分 秒  
7 - * time: 传入时间戳  
8 - * format:返回格式,支持自定义,但参数必须与formateArr里保持一致  
9 - * formatTimeByDefined(1488481383,'Y/M/D h:m:s') => 2017/03/03 03:03:03  
10 -*/  
11 -function formatTimeByDefined(time, format) {  
12 -  
13 - var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];  
14 - var returnArr = [];  
15 -  
16 - var date = new Date(time * 1000);  
17 - returnArr.push(date.getFullYear());  
18 - returnArr.push(formatNumber(date.getMonth() + 1));  
19 - returnArr.push(formatNumber(date.getDate()));  
20 -  
21 - returnArr.push(formatNumber(date.getHours()));  
22 - returnArr.push(formatNumber(date.getMinutes()));  
23 - returnArr.push(formatNumber(date.getSeconds()));  
24 -  
25 - for (var i in returnArr) {  
26 - format = format.replace(formateArr[i], returnArr[i]);  
27 - }  
28 - return format;  
29 -}  
30 -  
31 -/**  
32 - * 时间戳转化为 天 时 分 秒  
33 - * time: 传入时间戳  
34 - * format:返回格式,支持自定义,但参数必须与formateArr里保持一致  
35 - * formatTimeByDefined(1488481383,'Y/M/D h:m:s') => 2017/03/03 03:03:03  
36 -*/  
37 -function formatTimeByDay(time, format) {  
38 -  
39 - var formateArr = ['D', 'h', 'm', 's'];  
40 - var returnArr = [];  
41 - var int_day, int_hour, int_minute, int_second;  
42 - time = time * 1000;  
43 - if (time >= 0) {  
44 - int_day = Math.floor(time / 86400000)  
45 - time -= int_day * 86400000;  
46 - int_hour = Math.floor(time / 3600000)  
47 - time -= int_hour * 3600000;  
48 - int_minute = Math.floor(time / 60000)  
49 - time -= int_minute * 60000;  
50 - int_second = Math.floor(time / 1000)  
51 - }  
52 - returnArr.push(formatNumber(int_day));  
53 - returnArr.push(formatNumber(int_hour));  
54 - returnArr.push(formatNumber(int_minute));  
55 - returnArr.push(formatNumber(int_second));  
56 -  
57 - for (var i in returnArr) {  
58 - format = format.replace(formateArr[i], returnArr[i]);  
59 - }  
60 - return format;  
61 -}  
62 -  
63 -function formatTime(date) {  
64 - var year = date.getFullYear()  
65 - var month = date.getMonth() + 1  
66 - var day = date.getDate()  
67 -  
68 - var hour = date.getHours()  
69 - var minute = date.getMinutes()  
70 - var second = date.getSeconds()  
71 -  
72 - return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')  
73 -}  
74 -  
75 -function formatNumber(n) {  
76 - n = n.toString()  
77 - return n[1] ? n : '0' + n  
78 -}  
79 -  
80 -function getDeviceInfo(wx) {  
81 - let res = wx.getSystemInfoSync(),  
82 - windowWidth;  
83 -  
84 - windowWidth = res.windowWidth;  
85 - return { windowWidth };  
86 -}  
87 -  
88 -function shouldDiscardTap(currentTimeStamp, lastTimeStamp) {  
89 - if (lastTimeStamp != 0 && currentTimeStamp - lastTimeStamp < 250) {  
90 - return true;  
91 - }  
92 -  
93 - return false;  
94 -}  
95 -  
96 -function formatImgUrl (json) {  
97 - json.data && json.data.map((item, index) => {  
98 - let replaceStr = "{width}";  
99 - let url = item.src;  
100 - url = url.replace(new RegExp('{width}'), windowWidth * 2).replace(new RegExp('{height}'), 100).replace(new RegExp('{mode}'), '2');  
101 - item.src = url;  
102 - // console.log(url)  
103 - });  
104 - return json;  
105 -}  
106 -  
107 -function getImageUrl(url, windowWidth,height){  
108 - if(!url){  
109 - return '';  
110 - }  
111 - return url.replace(new RegExp('{width}'), windowWidth *2).replace(new RegExp('{height}'), height?height:100).replace(new RegExp('{mode}'), '2');  
112 -}  
113 -  
114 -function formatImageUrl(url, width, height, mode){  
115 - url = url || '';  
116 - if (url && url.indexOf('?') === -1) {  
117 - url = url + '?imageView2/{mode}/w/{width}/h/{height}';  
118 - }  
119 -  
120 - return url.replace(/{width}/g, width).replace(/{height}/g, height).replace('{mode}', mode || 2);  
121 -}  
122 -  
123 -// 自动识别 2倍 3倍图  
124 -function getImageUrlWithWH(image_url, image_width, image_height) {  
125 - let app = getApp();  
126 - const screenWidth = app.globalData.systemInfo.screenWidth;  
127 - const pixelRatio = app.globalData.systemInfo.pixelRatio;  
128 - const DEVICE_WIDTH_RATIO = screenWidth / 375.0;  
129 -  
130 - if (!image_url) {  
131 - return '';  
132 - }  
133 - 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);  
134 -}  
135 -  
136 -function getBrandID (url) {  
137 - let params = url.split("openby:yohobuy=")  
138 - if (params.length == 2) {  
139 - let jsonParam = JSON.parse(params[1])  
140 - return jsonParam.params.brand_id;  
141 - }  
142 - return null;  
143 -}  
144 -  
145 -function isStringEmpty(str){  
146 - if (str===undefined || str ===null || str ===''){  
147 - return true  
148 - }else{  
149 - return false  
150 - }  
151 - return true;  
152 -}  
153 -  
154 -function getGoodDetailParam(url) {  
155 - let params = url.split("openby:yohobuy=")  
156 - if (params.length == 2) {  
157 - let jsonParam = JSON.parse(params[1])  
158 - return JSON.stringify(jsonParam.params);  
159 - }  
160 - return null;  
161 -}  
162 -  
163 -function formatDateTime(inputTime) {  
164 - var date = new Date();  
165 - date.setTime(inputTime*1000);  
166 - var y = date.getFullYear();  
167 - var m = date.getMonth() + 1;  
168 - m = m < 10 ? ('0' + m) : m;  
169 - var d = date.getDate();  
170 - d = d < 10 ? ('0' + d) : d;  
171 - return y + '.' + m + '.'+d;  
172 -};  
173 -  
174 -function formateTimestamp(start,end){  
175 - var startTime = formatDateTime(start)  
176 - var endTime = formatDateTime(end)  
177 - return startTime+'-'+endTime  
178 -}  
179 -  
180 -function getParameterByName(name, url) {  
181 - if (!url) url = window.location.href;  
182 - name = name.replace(/[\[\]]/g, "\\$&");  
183 - var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),  
184 - results = regex.exec(url);  
185 - if (!results) return null;  
186 - if (!results[2]) return '';  
187 - return decodeURIComponent(results[2].replace(/\+/g, " "));  
188 -}  
189 -  
190 -function getSknFromUrl(url) {  
191 - if (!url) return '';  
192 - url = decodeURIComponent(url).toLowerCase();  
193 - let components = url.split('/');  
194 - let lastElement = components.pop();  
195 - let startLegal = url.startsWith('https://m.yohobuy.com/product') || url.startsWith('http://m.yohobuy.com/product');  
196 - let endLegal = lastElement.endsWith('.html');  
197 - if (!startLegal || !endLegal) {  
198 - return '';  
199 - }  
200 -  
201 - let lastElementArray = lastElement.split('.');  
202 - return lastElementArray[0];  
203 -}  
204 -  
205 -function getShopIdFromUrl(url) {  
206 - if (!url) return '';  
207 - url = decodeURIComponent(url).toLowerCase();  
208 - let components = url.split('/');  
209 - let lastElement = components.pop();  
210 - let startLegal = url.startsWith('https://m.yohobuy.com/shop') || url.startsWith('http://m.yohobuy.com/shop');  
211 - let endLegal = lastElement.endsWith('.html');  
212 - if (!startLegal || !endLegal) {  
213 - return '';  
214 - }  
215 -  
216 - let nameAndId = lastElement.split('.')[0];  
217 - let shopId = nameAndId.split('-').pop();  
218 - return shopId;  
219 -}  
220 -  
221 -function getShopID(url) {  
222 - let params = url.split("openby:yohobuy=")  
223 - if (params.length == 2) {  
224 - let jsonParam = JSON.parse(params[1])  
225 - return jsonParam.params.shop_id;  
226 - }  
227 - return null;  
228 -}  
229 -  
230 -function contains(arr, obj) {  
231 - for (var i = 0; i < arr.length; i++) {  
232 - if (arr[i] == obj) {  
233 - return i;  
234 - }  
235 - }  
236 - return -1;  
237 -}  
238 -  
239 -function getGuangType(url) {  
240 - let params = url.split("openby:yohobuy=")  
241 - if (params.length == 2) {  
242 - let jsonParam = JSON.parse(params[1])  
243 - return jsonParam.params.type;  
244 - }  
245 - return null;  
246 -}  
247 -  
248 -// public method for encoding  
249 -function base64Encode(input) {  
250 - let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";  
251 -  
252 - var output = "";  
253 - var chr1, chr2, chr3, enc1, enc2, enc3, enc4;  
254 - var i = 0;  
255 - input = _utf8_encode(input);  
256 - while (i < input.length) {  
257 - chr1 = input.charCodeAt(i++);  
258 - chr2 = input.charCodeAt(i++);  
259 - chr3 = input.charCodeAt(i++);  
260 - enc1 = chr1 >> 2;  
261 - enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  
262 - enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  
263 - enc4 = chr3 & 63;  
264 - if (isNaN(chr2)) {  
265 - enc3 = enc4 = 64;  
266 - } else if (isNaN(chr3)) {  
267 - enc4 = 64;  
268 - }  
269 - output = output +  
270 - _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +  
271 - _keyStr.charAt(enc3) + _keyStr.charAt(enc4);  
272 - }  
273 - return output;  
274 -}  
275 -  
276 -// public method for decoding  
277 -function base64Decode(input) {  
278 - let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";  
279 - var output = "";  
280 - var chr1, chr2, chr3;  
281 - var enc1, enc2, enc3, enc4;  
282 - var i = 0;  
283 - input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");  
284 - while (i < input.length) {  
285 - enc1 = _keyStr.indexOf(input.charAt(i++));  
286 - enc2 = _keyStr.indexOf(input.charAt(i++));  
287 - enc3 = _keyStr.indexOf(input.charAt(i++));  
288 - enc4 = _keyStr.indexOf(input.charAt(i++));  
289 - chr1 = (enc1 << 2) | (enc2 >> 4);  
290 - chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);  
291 - chr3 = ((enc3 & 3) << 6) | enc4;  
292 - output = output + String.fromCharCode(chr1);  
293 - if (enc3 != 64) {  
294 - output = output + String.fromCharCode(chr2);  
295 - }  
296 - if (enc4 != 64) {  
297 - output = output + String.fromCharCode(chr3);  
298 - }  
299 - }  
300 - output = _utf8_decode(output);  
301 - return output;  
302 -}  
303 -  
304 -function _utf8_encode(string) {  
305 - string = string.replace(/\r\n/g, "\n");  
306 - var utftext = "";  
307 - for (var n = 0; n < string.length; n++) {  
308 - var c = string.charCodeAt(n);  
309 - if (c < 128) {  
310 - utftext += String.fromCharCode(c);  
311 - } else if ((c > 127) && (c < 2048)) {  
312 - utftext += String.fromCharCode((c >> 6) | 192);  
313 - utftext += String.fromCharCode((c & 63) | 128);  
314 - } else {  
315 - utftext += String.fromCharCode((c >> 12) | 224);  
316 - utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
317 - utftext += String.fromCharCode((c & 63) | 128);  
318 - }  
319 -  
320 - }  
321 - return utftext;  
322 -}  
323 -  
324 -// private method for UTF-8 decoding  
325 -function _utf8_decode(utftext) {  
326 - var string = "";  
327 - var i = 0;  
328 - var c = 0;  
329 - var c1 = 0;  
330 - var c2 = 0;  
331 - var c3 = 0;  
332 - while (i < utftext.length) {  
333 - c = utftext.charCodeAt(i);  
334 - if (c < 128) {  
335 - string += String.fromCharCode(c);  
336 - i++;  
337 - } else if ((c > 191) && (c < 224)) {  
338 - c2 = utftext.charCodeAt(i + 1);  
339 - string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));  
340 - i += 2;  
341 - } else {  
342 - c2 = utftext.charCodeAt(i + 1);  
343 - c3 = utftext.charCodeAt(i + 2);  
344 - string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
345 - i += 3;  
346 - }  
347 - }  
348 - return string;  
349 -}  
350 -/*获取缓存  
351 -key: 关键词  
352 -fromPage: 来源页面名称  
353 -**/  
354 -function getYHStorageSync(key,fromPage) {  
355 - // console.log(key)  
356 - // console.log(fromPage)  
357 - try {  
358 - let res = wx.getStorageSync(key);  
359 - // console.log(res);  
360 - // console.log('-------------------------')  
361 - return res;  
362 - } catch (e) {  
363 - wx.reportAnalytics('get_storage_error', {  
364 - key: key,  
365 - error: e.message,  
366 - error_code: e.code,  
367 - from_page: fromPage,  
368 - });  
369 - // console.log(e)  
370 - return null;  
371 - }  
372 -}  
373 -  
374 -/*设置缓存  
375 -key: 关键词  
376 -value: 内容  
377 -fromPage: 来源页面名称  
378 -**/  
379 -function setStorageSync(key,value, fromPage) {  
380 - try {  
381 - wx.setStorageSync(key, value);  
382 - return true;  
383 - } catch (e) {  
384 - return false;  
385 - }  
386 -}  
387 -  
388 -/*清除缓存  
389 -key: 关键词  
390 -fromPage: 来源页面名称  
391 -**/  
392 -function removeStorageSync(key, fromPage) {  
393 - try {  
394 - wx.removeStorageSync(key);  
395 - return true;  
396 - } catch (e) {  
397 - return false;  
398 - }  
399 -}  
400 -  
401 -module.exports = {  
402 - formatTimeByDefined,  
403 - formatTime: formatTime,  
404 - formatTimeByDay,  
405 - getDeviceInfo: getDeviceInfo,  
406 - shouldDiscardTap,  
407 - formatImgUrl,  
408 - formatImageUrl,  
409 - getBrandID,  
410 - getGoodDetailParam,  
411 - getImageUrl,  
412 - formateTimestamp,  
413 - getImageUrlWithWH,  
414 - isStringEmpty,  
415 - getParameterByName,  
416 - getSknFromUrl,  
417 - getShopIdFromUrl,  
418 - getShopID,  
419 - contains,  
420 - getGuangType,  
421 - base64Encode,  
422 - base64Decode,  
423 - getYHStorageSync,  
424 - setStorageSync,  
425 - removeStorageSync  
426 -}