'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 }