|
|
'use strict'
|
|
|
'use strict';
|
|
|
|
|
|
//获取应用实例
|
|
|
|
|
|
/**
|
|
|
* 时间戳转化为年 月 日 时 分 秒
|
|
|
/**
|
|
|
* 时间戳转化为年 月 日 时 分 秒
|
|
|
* time: 传入时间戳
|
|
|
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
|
|
|
* 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 = [];
|
|
|
|
...
|
...
|
@@ -26,31 +25,35 @@ function formatTimeByDefined(time, format) { |
|
|
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(':')
|
|
|
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
|
|
|
n = n.toString();
|
|
|
return n[1] ? n : '0' + n;
|
|
|
}
|
|
|
|
|
|
function getDeviceInfo(wx) {
|
|
|
let res = wx.getSystemInfoSync(),
|
|
|
windowWidth;
|
|
|
let res = wx.getSystemInfoSync(),
|
|
|
windowWidth;
|
|
|
|
|
|
windowWidth = res.windowWidth;
|
|
|
return { windowWidth };
|
|
|
windowWidth = res.windowWidth;
|
|
|
return { windowWidth };
|
|
|
}
|
|
|
|
|
|
function shouldDiscardTap(currentTimeStamp, lastTimeStamp) {
|
...
|
...
|
@@ -61,31 +64,41 @@ function shouldDiscardTap(currentTimeStamp, lastTimeStamp) { |
|
|
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)
|
|
|
});
|
|
|
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){
|
|
|
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');
|
|
|
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){
|
|
|
function formatImageUrl(url, width, height, mode) {
|
|
|
url = url || '';
|
|
|
if (url && url.indexOf('?') === -1) {
|
|
|
url = url + '?imageView2/{mode}/w/{width}/h/{height}';
|
|
|
url = url + '?imageView2/{mode}/w/{width}/h/{height}';
|
|
|
}
|
|
|
|
|
|
return url.replace(/{width}/g, width).replace(/{height}/g, height).replace('{mode}', mode || 2);
|
|
|
return url
|
|
|
.replace(/{width}/g, width)
|
|
|
.replace(/{height}/g, height)
|
|
|
.replace('{mode}', mode || 2);
|
|
|
}
|
|
|
|
|
|
// 自动识别 2倍 3倍图
|
...
|
...
|
@@ -98,31 +111,40 @@ function getImageUrlWithWH(image_url, image_width, image_height) { |
|
|
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);
|
|
|
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=")
|
|
|
function getBrandID(url) {
|
|
|
let params = url.split('openby:yohobuy=');
|
|
|
if (params.length == 2) {
|
|
|
let jsonParam = JSON.parse(params[1])
|
|
|
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 isStringEmpty(str) {
|
|
|
if (str === undefined || str === null || str === '') {
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
function getGoodDetailParam(url) {
|
|
|
let params = url.split("openby:yohobuy=")
|
|
|
let params = url.split('openby:yohobuy=');
|
|
|
if (params.length == 2) {
|
|
|
let jsonParam = JSON.parse(params[1])
|
|
|
let jsonParam = JSON.parse(params[1]);
|
|
|
return JSON.stringify(jsonParam.params);
|
|
|
}
|
|
|
return null;
|
...
|
...
|
@@ -130,29 +152,29 @@ function getGoodDetailParam(url) { |
|
|
|
|
|
function formatDateTime(inputTime) {
|
|
|
var date = new Date();
|
|
|
date.setTime(inputTime*1000);
|
|
|
date.setTime(inputTime * 1000);
|
|
|
var y = date.getFullYear();
|
|
|
var m = date.getMonth() + 1;
|
|
|
m = m < 10 ? ('0' + m) : m;
|
|
|
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
|
|
|
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 + "(=([^&#]*)|&|#|$)"),
|
|
|
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, " "));
|
|
|
return decodeURIComponent(results[2].replace(/\+/g, ' '));
|
|
|
}
|
|
|
|
|
|
function getSknFromUrl(url) {
|
...
|
...
|
@@ -160,7 +182,9 @@ function getSknFromUrl(url) { |
|
|
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 startLegal =
|
|
|
url.startsWith('https://m.yohobuy.com/product') ||
|
|
|
url.startsWith('http://m.yohobuy.com/product');
|
|
|
let endLegal = lastElement.endsWith('.html');
|
|
|
if (!startLegal || !endLegal) {
|
|
|
return '';
|
...
|
...
|
@@ -175,7 +199,9 @@ function getShopIdFromUrl(url) { |
|
|
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 startLegal =
|
|
|
url.startsWith('https://m.yohobuy.com/shop') ||
|
|
|
url.startsWith('http://m.yohobuy.com/shop');
|
|
|
let endLegal = lastElement.endsWith('.html');
|
|
|
if (!startLegal || !endLegal) {
|
|
|
return '';
|
...
|
...
|
@@ -187,9 +213,9 @@ function getShopIdFromUrl(url) { |
|
|
}
|
|
|
|
|
|
function getShopID(url) {
|
|
|
let params = url.split("openby:yohobuy=")
|
|
|
let params = url.split('openby:yohobuy=');
|
|
|
if (params.length == 2) {
|
|
|
let jsonParam = JSON.parse(params[1])
|
|
|
let jsonParam = JSON.parse(params[1]);
|
|
|
return jsonParam.params.shop_id;
|
|
|
}
|
|
|
return null;
|
...
|
...
|
@@ -202,22 +228,23 @@ function contains(arr, obj) { |
|
|
}
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function getGuangType(url) {
|
|
|
let params = url.split("openby:yohobuy=")
|
|
|
let params = url.split('openby:yohobuy=');
|
|
|
if (params.length == 2) {
|
|
|
let jsonParam = JSON.parse(params[1])
|
|
|
let jsonParam = JSON.parse(params[1]);
|
|
|
return jsonParam.params.type;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
// public method for encoding
|
|
|
// public method for encoding
|
|
|
function base64Encode(input) {
|
|
|
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
|
let _keyStr =
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
|
|
|
|
var output = "";
|
|
|
var output = '';
|
|
|
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
|
|
var i = 0;
|
|
|
input = _utf8_encode(input);
|
...
|
...
|
@@ -234,21 +261,25 @@ function base64Encode(input) { |
|
|
} else if (isNaN(chr3)) {
|
|
|
enc4 = 64;
|
|
|
}
|
|
|
output = output +
|
|
|
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
|
|
|
_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
|
|
|
output =
|
|
|
output +
|
|
|
_keyStr.charAt(enc1) +
|
|
|
_keyStr.charAt(enc2) +
|
|
|
_keyStr.charAt(enc3) +
|
|
|
_keyStr.charAt(enc4);
|
|
|
}
|
|
|
return output;
|
|
|
}
|
|
|
|
|
|
// public method for decoding
|
|
|
// public method for decoding
|
|
|
function base64Decode(input) {
|
|
|
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
|
var output = "";
|
|
|
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, "");
|
|
|
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
|
|
|
while (i < input.length) {
|
|
|
enc1 = _keyStr.indexOf(input.charAt(i++));
|
|
|
enc2 = _keyStr.indexOf(input.charAt(i++));
|
...
|
...
|
@@ -270,13 +301,13 @@ function base64Decode(input) { |
|
|
}
|
|
|
|
|
|
function _utf8_encode(string) {
|
|
|
string = string.replace(/\r\n/g, "\n");
|
|
|
var utftext = "";
|
|
|
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)) {
|
|
|
} else if (c > 127 && c < 2048) {
|
|
|
utftext += String.fromCharCode((c >> 6) | 192);
|
|
|
utftext += String.fromCharCode((c & 63) | 128);
|
|
|
} else {
|
...
|
...
|
@@ -284,16 +315,15 @@ function _utf8_encode(string) { |
|
|
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
|
|
utftext += String.fromCharCode((c & 63) | 128);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return utftext;
|
|
|
}
|
|
|
|
|
|
// private method for UTF-8 decoding
|
|
|
// private method for UTF-8 decoding
|
|
|
function _utf8_decode(utftext) {
|
|
|
var string = "";
|
|
|
var string = '';
|
|
|
var i = 0;
|
|
|
var c = 0;
|
|
|
var c = 0;
|
|
|
var c1 = 0;
|
|
|
var c2 = 0;
|
|
|
var c3 = 0;
|
...
|
...
|
@@ -302,14 +332,16 @@ function _utf8_decode(utftext) { |
|
|
if (c < 128) {
|
|
|
string += String.fromCharCode(c);
|
|
|
i++;
|
|
|
} else if ((c > 191) && (c < 224)) {
|
|
|
} 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));
|
|
|
string += String.fromCharCode(
|
|
|
((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63),
|
|
|
);
|
|
|
i += 3;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -319,7 +351,7 @@ function _utf8_decode(utftext) { |
|
|
key: 关键词
|
|
|
fromPage: 来源页面名称
|
|
|
**/
|
|
|
function getYHStorageSync(key,fromPage) {
|
|
|
function getYHStorageSync(key, fromPage) {
|
|
|
// console.log(key)
|
|
|
// console.log(fromPage)
|
|
|
try {
|
...
|
...
|
@@ -344,7 +376,7 @@ key: 关键词 |
|
|
value: 内容
|
|
|
fromPage: 来源页面名称
|
|
|
**/
|
|
|
function setStorageSync(key,value, fromPage) {
|
|
|
function setStorageSync(key, value, fromPage) {
|
|
|
try {
|
|
|
wx.setStorageSync(key, value);
|
|
|
return true;
|
...
|
...
|
@@ -366,6 +398,34 @@ function removeStorageSync(key, fromPage) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 格式化倒计时时间
|
|
|
* time in seconds
|
|
|
*/
|
|
|
function fomartCountdownTime({ startTime, endTime }) {
|
|
|
let remainingTime = 0;
|
|
|
|
|
|
if (endTime) {
|
|
|
remainingTime = endTime - Date.now() / 1000;
|
|
|
} else if (startTime) {
|
|
|
remainingTime = startTime - Date.now() / 1000;
|
|
|
}
|
|
|
|
|
|
if (remainingTime <= 0) {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
const timeInHour = Math.floor(remainingTime / 3600);
|
|
|
remainingTime = remainingTime - timeInHour * 3600;
|
|
|
|
|
|
const timeInMinute = Math.floor(remainingTime / 60);
|
|
|
remainingTime = parseInt(remainingTime - timeInMinute * 60);
|
|
|
|
|
|
return [timeInHour, timeInMinute, remainingTime].map(time => {
|
|
|
return `${time > 9 ? time : `0${time}`}`;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
|
formatTimeByDefined,
|
|
|
formatTime: formatTime,
|
...
|
...
|
@@ -389,5 +449,6 @@ module.exports = { |
|
|
base64Decode,
|
|
|
getYHStorageSync,
|
|
|
setStorageSync,
|
|
|
removeStorageSync
|
|
|
} |
|
|
\ No newline at end of file |
|
|
removeStorageSync,
|
|
|
fomartCountdownTime,
|
|
|
}; |
...
|
...
|
|