以下代码支持wxs中使用
/**
* 主要是用来处理聊天页面逻辑
*/
var durtionTime = 180000 //三分钟
var foo = "'hello world' from tools.wxs";
var bar = function (d) {
return d;
}
/**
* 判断消息朝向
*/
var isSelf = function (sendUser, selfUser) {
return sendUser == selfUser
}
var showTime = function (sendTime) {
let result = ""
let currentTime = getDate().getTime()
if (currentTime - sendTime < durtionTime) {
result = ""
} else {
result = "三分钟前"
}
return result
}
/**
*
* @param timestamp 时间戳类型
*/
var dateDiff = function (timestamp) {
// 补全为13位
var arrTimestamp = (timestamp + '').split('');
for (var start = 0; start < 13; start++) {
if (!arrTimestamp[start]) {
arrTimestamp[start] = '0';
}
}
timestamp = arrTimestamp.join('') * 1;
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = getDate().getTime();
var diffValue = now - timestamp;
// 如果本地时间反而小于变量时间
if (diffValue < 0) {
return '不久前';
}
// 计算差异时间的量级
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
// 数值补0方法
var zero = function (value) {
if (value < 10) {
return '0' + value;
}
return value;
};
// 使用
if (monthC > 12) {
// 超过1年,直接显示年月日
return (function () {
var date = getDate(timestamp);
return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
})();
} else if (monthC >= 1) {
return parseInt(monthC) + "月前";
} else if (weekC >= 1) {
return parseInt(weekC) + "周前";
} else if (dayC >= 1) {
return parseInt(dayC) + "天前";
} else if (hourC >= 1) {
return parseInt(hourC) + "小时前";
} else if (minC >= 1) {
return parseInt(minC) + "分钟前";
}
return '刚刚';
};
/**
*
* @param dateStr 时间格式为 2016-12-12 12:20:00
*/
function getDateDiff(dateStr) {
var publishTime = getDateTimeStamp(dateStr) / 1000,
d_seconds,
d_minutes,
d_hours,
d_days,
timeNow = parseInt(getDate().getTime() / 1000),
d,
date = getDate(publishTime * 1000),
Y = date.getFullYear(),
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
//小于10的在前面补0
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
if (H < 10) {
H = '0' + H;
}
if (m < 10) {
m = '0' + m;
}
if (s < 10) {
s = '0' + s;
}
d = timeNow - publishTime;
d_days = parseInt(d / 86400);
d_hours = parseInt(d / 3600);
d_minutes = parseInt(d / 60);
d_seconds = parseInt(d);
if (d_days > 0 && d_days < 3) {
return d_days + '天前';
} else if (d_days <= 0 && d_hours > 0) {
return d_hours + '小时前';
} else if (d_hours <= 0 && d_minutes > 0) {
return d_minutes + '分钟前';
} else if (d_seconds < 60) {
if (d_seconds <= 0) {
return '刚刚';
} else {
return d_seconds + '秒前';
}
} else if (d_days >= 3 && d_days < 30) {
return M + '-' + D + ' ' + H + ':' + m;
} else if (d_days >= 30) {
return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
}
}
module.exports = {
FOO: foo,
bar: bar,
isSelf: isSelf,
showTime: showTime,
dateDiff: dateDiff,
};
module.exports.msg = "some msg";