|
|
const formatTime = date => {
|
|
|
const year = date.getFullYear()
|
|
|
const month = date.getMonth() + 1
|
|
|
const day = date.getDate()
|
|
|
const hour = date.getHours()
|
|
|
const minute = date.getMinutes()
|
|
|
const second = date.getSeconds()
|
|
|
|
|
|
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
|
|
}
|
|
|
|
|
|
const formatNumber = n => {
|
|
|
n = n.toString()
|
|
|
return n[1] ? n : '0' + n
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取今天星期
|
|
|
*/
|
|
|
function getWeeker() {
|
|
|
var a = ["日", "一", "二", "三", "四", "五", "六"];
|
|
|
var week = new Date().getDay();
|
|
|
return "星期" + a[week];
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 比较两个日期的大小
|
|
|
*/
|
|
|
function compareDate(d1, d2) {
|
|
|
var date = new Date(d1);
|
|
|
var tempDate = new Date(d2);
|
|
|
return date.getTime() > tempDate.getTime();
|
|
|
}
|
|
|
|
|
|
/***
|
|
|
* data 时间
|
|
|
*
|
|
|
* sign 标识(0,1,2)
|
|
|
* 0:yyyy/MM/dd HH:mm:ss:ms
|
|
|
* 1:yyyy-MM-dd HH:mm:ss:ms(default)
|
|
|
* 2:yyyyMMddHHmmss
|
|
|
* 3:yyyy-MM-dd
|
|
|
*/
|
|
|
function getFormatTime(date, sign) {
|
|
|
if (undefined == sign || null == sign || sign == "" || sign > 5) {
|
|
|
sign = 0;
|
|
|
}
|
|
|
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()
|
|
|
var milliseconds = date.getMilliseconds()
|
|
|
var value = "";
|
|
|
switch (sign) {
|
|
|
case 0:
|
|
|
value = [hour, minute].map(formatNumber).join(':');
|
|
|
break;
|
|
|
case 1:
|
|
|
value = [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
|
|
|
break;
|
|
|
case 2:
|
|
|
value = [year, month, day].map(formatNumber).join('') + [hour, minute, second].map(formatNumber).join('');
|
|
|
break;
|
|
|
case 3:
|
|
|
value = [year, month, day].map(formatNumber).join('-');
|
|
|
break;
|
|
|
case 4:
|
|
|
value = [year, month].map(formatNumber).join('-') + "-01";
|
|
|
break;
|
|
|
case 5:
|
|
|
value = [hour, minute, second].map(formatNumber).join(':');
|
|
|
break;
|
|
|
}
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
|
|
|
// 对象转string
|
|
|
const parseToString = function(options) {
|
|
|
var content = "";
|
|
|
var keys = Object.keys(options);
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
var key = keys[i];
|
|
|
var value = "";
|
|
|
if (undefined != options[key]) {
|
|
|
value = options[key].toString();
|
|
|
}
|
|
|
if (content.length > 0) {
|
|
|
content += "&" + encodeURI(key, "utf-8") + "=" + encodeURI(value, "utf-8");
|
|
|
} else {
|
|
|
content += encodeURI(key, "utf-8") + "=" + encodeURI(value, "utf-8");
|
|
|
|
|
|
}
|
|
|
}
|
|
|
return content;
|
|
|
}
|
|
|
|
|
|
/**校验不为空 */
|
|
|
const isNotBlank = function(value) {
|
|
|
if (null != value && undefined != value && JSON.stringify(value) != "") {
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
/**校验为空 */
|
|
|
const isBlank = function(value) {
|
|
|
if (null == value || undefined == value || value == "" || JSON.stringify(value) == "") {
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
// 替换全部
|
|
|
function replaceAll(source, oldStr, newStr) {
|
|
|
while (source.indexOf(oldStr) >= 0) {
|
|
|
source = source.replace(oldStr, newStr);
|
|
|
}
|
|
|
return source;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加法运算,避免数据相加小数点后产生多位数和计算精度损失。
|
|
|
*
|
|
|
* @param num1加数1 |
|
|
|
* num2加数2
|
|
|
*/
|
|
|
var numAdd = function(num1, num2, decimal) {
|
|
|
var baseNum, baseNum1, baseNum2;
|
|
|
try {
|
|
|
baseNum1 = num1.toString().split(".")[1].length;
|
|
|
} catch (e) {
|
|
|
baseNum1 = 0;
|
|
|
}
|
|
|
try {
|
|
|
baseNum2 = num2.toString().split(".")[1].length;
|
|
|
} catch (e) {
|
|
|
baseNum2 = 0;
|
|
|
}
|
|
|
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
|
|
|
var value = (num1 * baseNum + num2 * baseNum) / baseNum
|
|
|
if (undefined == decimal || null == decimal || "" == decimal) {
|
|
|
return value;
|
|
|
} else {
|
|
|
if (isNaN(decimal)) {
|
|
|
decimal = 2
|
|
|
}
|
|
|
return value.toFixed(2);
|
|
|
}
|
|
|
};
|
|
|
/**
|
|
|
* 减法运算,避免数据相减小数点后产生多位数和计算精度损失。
|
|
|
*
|
|
|
* @param num1被减数 |
|
|
|
* num2减数
|
|
|
*/
|
|
|
var numSub = function(num1, num2, decimal) {
|
|
|
var baseNum, baseNum1, baseNum2;
|
|
|
var precision; /*** 精度***/
|
|
|
try {
|
|
|
baseNum1 = num1.toString().split(".")[1].length;
|
|
|
} catch (e) {
|
|
|
baseNum1 = 0;
|
|
|
}
|
|
|
try {
|
|
|
baseNum2 = num2.toString().split(".")[1].length;
|
|
|
} catch (e) {
|
|
|
baseNum2 = 0;
|
|
|
}
|
|
|
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
|
|
|
precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
|
|
|
var value = Number(((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision));
|
|
|
if (undefined == decimal || null == decimal || "" == decimal) {
|
|
|
return value;
|
|
|
} else {
|
|
|
if (isNaN(decimal)) {
|
|
|
decimal = 2
|
|
|
}
|
|
|
return value.toFixed(2);
|
|
|
}
|
|
|
};
|
|
|
/**
|
|
|
* 乘法运算,避免数据相乘小数点后产生多位数和计算精度损失。
|
|
|
*
|
|
|
* @param num1被乘数 |
|
|
|
* num2乘数
|
|
|
*/
|
|
|
var numMulti = function(num1, num2, decimal) {
|
|
|
var baseNum = 0;
|
|
|
try {
|
|
|
baseNum += num1.toString().split(".")[1].length;
|
|
|
} catch (e) {}
|
|
|
try {
|
|
|
baseNum += num2.toString().split(".")[1].length;
|
|
|
} catch (e) {}
|
|
|
var value = Number(num1.toString().replace(".", "")) *
|
|
|
Number(num2.toString().replace(".", "")) / Math.pow(10, baseNum);
|
|
|
|
|
|
if (undefined == decimal || null == decimal || "" == decimal) {
|
|
|
return value;
|
|
|
} else {
|
|
|
if (isNaN(decimal)) {
|
|
|
decimal = 2
|
|
|
}
|
|
|
return value.toFixed(2);
|
|
|
}
|
|
|
};
|
|
|
/**
|
|
|
* 除法运算,避免数据相除小数点后产生多位数和计算精度损失。
|
|
|
*
|
|
|
* @param num1被除数 |
|
|
|
* num2除数
|
|
|
*/
|
|
|
var numDiv = function(num1, num2, decimal) {
|
|
|
var baseNum1 = 0,
|
|
|
baseNum2 = 0;
|
|
|
var baseNum3, baseNum4;
|
|
|
try {
|
|
|
baseNum1 = num1.toString().split(".")[1].length;
|
|
|
} catch (e) {
|
|
|
baseNum1 = 0;
|
|
|
}
|
|
|
try {
|
|
|
baseNum2 = num2.toString().split(".")[1].length;
|
|
|
} catch (e) {
|
|
|
baseNum2 = 0;
|
|
|
}
|
|
|
|
|
|
baseNum3 = Number(num1.toString().replace(".", ""));
|
|
|
baseNum4 = Number(num2.toString().replace(".", ""));
|
|
|
var value = (baseNum3 / baseNum4) * pow(10, baseNum2 - baseNum1);
|
|
|
|
|
|
if (undefined == decimal || null == decimal || "" == decimal) {
|
|
|
return value;
|
|
|
} else {
|
|
|
if (isNaN(decimal)) {
|
|
|
decimal = 2
|
|
|
}
|
|
|
return value.toFixed(2);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
// 仿js each循环
|
|
|
var each = function(object, callback, args) {
|
|
|
var name, i = 0,
|
|
|
length = object.length,
|
|
|
isObj = length === undefined || typeof object == 'function';
|
|
|
|
|
|
if (args) {
|
|
|
if (isObj) {
|
|
|
for (name in object) {
|
|
|
if (callback.apply(object[name], args) === false) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
for (; i < length;) {
|
|
|
if (callback.apply(object[i++], args) === false) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// A special, fast, case for the most common use of each
|
|
|
} else {
|
|
|
if (isObj) {
|
|
|
for (name in object) {
|
|
|
if (callback.call(object[name], name, object[name]) === false) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
for (; i < length;) {
|
|
|
if (callback.call(object[i], i, object[i++]) === false) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return object;
|
|
|
};
|
|
|
|
|
|
|
|
|
// 显示繁忙提示
|
|
|
var showBusy = text => wx.showToast({
|
|
|
title: text,
|
|
|
icon: 'loading',
|
|
|
duration: 10000
|
|
|
})
|
|
|
|
|
|
// 显示成功提示
|
|
|
var showSuccess = text => wx.showToast({
|
|
|
title: text,
|
|
|
icon: 'success'
|
|
|
})
|
|
|
|
|
|
function alertErrorMsg(title, content) {
|
|
|
wx.showModal({
|
|
|
title: title,
|
|
|
content: content,
|
|
|
showCancel: false
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 显示失败提示
|
|
|
var showModel = (title, content) => {
|
|
|
wx.hideToast();
|
|
|
wx.showModal({
|
|
|
title,
|
|
|
content: JSON.stringify(content),
|
|
|
showCancel: false
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 根据下标删除对应的元素
|
|
|
Array.prototype.remove = function(dx) {
|
|
|
if (isNaN(dx) || dx > this.length) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
for (var i = 0, n = 0; i < this.length; i++) {
|
|
|
if (this[i] != this[dx]) {
|
|
|
this[n++] = this[i]
|
|
|
}
|
|
|
}
|
|
|
this.length -= 1
|
|
|
}
|
|
|
|
|
|
//
|
|
|
var deviceInfo = function() {
|
|
|
var model = "iPhone X";
|
|
|
var result = false;
|
|
|
wx.getSystemInfo({
|
|
|
success: function(res) {
|
|
|
console.log(res);
|
|
|
if (res.errMsg == "getSystemInfo:ok") {
|
|
|
console.log(model.indexOf(res.model));
|
|
|
if (res.model.indexOf(model) != -1) {
|
|
|
result = true;
|
|
|
}
|
|
|
wx.setStorageSync("model", result);
|
|
|
}
|
|
|
},
|
|
|
});
|
|
|
}
|
|
|
|
|
|
var getZero = function(num) {
|
|
|
return parseFloat(num).toFixed(0);
|
|
|
}
|
|
|
|
|
|
var getOne = function(num) {
|
|
|
return parseFloat(num).toFixed(1);
|
|
|
}
|
|
|
|
|
|
var getTwo = function(num) {
|
|
|
return parseFloat(num).toFixed(2);
|
|
|
}
|
|
|
|
|
|
var isOpen = function(dateTime, time) {
|
|
|
var result = false;
|
|
|
if (!time) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
var tempTimes = time.split(",");
|
|
|
for (var item of tempTimes) {
|
|
|
var temps = item.split("-");
|
|
|
|
|
|
var startTime = temps[0].split(":");
|
|
|
var time1 = startTime[0] * 3600 + startTime[1] * 60 + startTime[2];
|
|
|
|
|
|
var endTime = temps[1].split(":");
|
|
|
var time2 = endTime[0] * 3600 + endTime[1] * 60 + endTime[2];
|
|
|
|
|
|
var nowTime = dateTime.split(":");
|
|
|
var time3 = nowTime[0] * 3600 + nowTime[1] * 60 + nowTime[2];
|
|
|
|
|
|
if (time1 < time3 && time3 < time2) {
|
|
|
result = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
var getDistance = function(lat1, lng1, lat2, lng2) {
|
|
|
lat1 = lat1 || 0;
|
|
|
lng1 = lng1 || 0;
|
|
|
lat2 = lat2 || 0;
|
|
|
lng2 = lng2 || 0;
|
|
|
|
|
|
var rad1 = lat1 * Math.PI / 180.0;
|
|
|
var rad2 = lat2 * Math.PI / 180.0;
|
|
|
var a = rad1 - rad2;
|
|
|
var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
|
|
|
|
|
|
var r = 6378137;
|
|
|
var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
|
|
|
|
|
|
// if (distance > 1000){
|
|
|
distance = Math.round(distance / 1000);
|
|
|
// }
|
|
|
|
|
|
return distance;
|
|
|
}
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
getWeeker: getWeeker,
|
|
|
compareDate: compareDate,
|
|
|
each: each,
|
|
|
numAdd: numAdd,
|
|
|
numSub: numSub,
|
|
|
numMulti: numMulti,
|
|
|
numDiv: numDiv,
|
|
|
formatTime: formatTime,
|
|
|
replaceAll: replaceAll,
|
|
|
parseToString: parseToString,
|
|
|
isBlank: isBlank,
|
|
|
isNotBlank: isNotBlank,
|
|
|
formatNumber: formatNumber,
|
|
|
showBusy: showBusy,
|
|
|
showSuccess: showSuccess,
|
|
|
showModel: showModel,
|
|
|
alertErrorMsg: alertErrorMsg,
|
|
|
deviceInfo: deviceInfo,
|
|
|
getFormatTime: getFormatTime,
|
|
|
getZero: getZero,
|
|
|
getOne: getOne,
|
|
|
getTwo: getTwo,
|
|
|
isOpen: isOpen,
|
|
|
getDistance: getDistance
|
|
|
} |