var businessType = require('../public/business-type.js') var _debug =false function isDebug() { return _debug } function debugLog(e) { if (_debug) { console.log(e) } } function action(f, t) { var after = t || 500 return function () { var app = getApp() if (app.globalData.isClicked) { return } app.globalData.isClicked = true f.apply(this, arguments) setTimeout(function() { app.globalData.isClicked = false }, after) } } function getPhone() { var tel try { tel = swan.getStorageSync(key.StorageKey.servicePhone) } catch (e) { } if (!tel) { tel = '400-881-9211' } return tel } function changeTimeToNumber(t) { return t.slice(0, 4) + t.slice(5, 7) } Date.prototype.format = function (format) { var date = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S+": this.getMilliseconds() }; if (/(y+)/i.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in date) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); } } return format; } /*时间差比较 *interval :D表示查询精确到天数的之差 interval :H表示查询精确到小时之差 interval :M表示查询精确到分钟之差 interval :S表示查询精确到秒之差 interval :T表示查询精确到毫秒之差 */ function dateDiff(interval, date1, date2) { var objInterval = { 'D': 1000 * 60 * 60 * 24, 'H': 1000 * 60 * 60, 'M': 1000 * 60, 'S': 1000, 'T': 1 }; interval = interval.toUpperCase(); var dt1 = new Date(Date.parse(date1.replace(/-/g, '/'))); var dt2 = new Date(Date.parse(date2.replace(/-/g, '/'))); try { // debugLog(dt2.getTime() - dt1.getTime()); // debugLog('objInterval.'+interval); // debugLog((dt2.getTime() - dt1.getTime()) / eval_r('objInterval.'+interval)); var d = (dt2.getTime() - dt1.getTime()) / objInterval[interval] var _d = d | 0 if (d != _d) { return _d + 1; } return _d; } catch (e) { return 0; } } function showFailTips (err, p) { if (err.code == businessType.NetworkErrorCode.netwrokMiss) { swan.showToast({ title: err.msg, icon: 'none' }) } else if (err.code == businessType.NetworkErrorCode.connectErrorMessage) { swan.showToast({ title: err.msg, icon: 'none' }) } else if (p) { swan.showToast({ title: p, icon: 'none' }) } } function selectFrom(lowerValue, upperValue) { var choice = upperValue - lowerValue + 1; return Math.floor(Math.random() * choice + lowerValue); } module.exports = { debugLog: debugLog, action: action, isDebug: isDebug, getPhone: getPhone, changeTimeToNumber: changeTimeToNumber, showFailTips: showFailTips, dateDiff: dateDiff, selectFrom: selectFrom }