You've already forked guangan-mp
90 lines
2.0 KiB
JavaScript
90 lines
2.0 KiB
JavaScript
function friendlyDate(timestamp) {
|
|
var formats = {
|
|
'year': '%n% 年前',
|
|
'month': '%n% 月前',
|
|
'day': '%n% 天前',
|
|
'hour': '%n% 小时前',
|
|
'minute': '%n% 分钟前',
|
|
'second': '%n% 秒前',
|
|
};
|
|
|
|
var now = Date.now();
|
|
var seconds = Math.floor((now - timestamp) / 1000);
|
|
var minutes = Math.floor(seconds / 60);
|
|
var hours = Math.floor(minutes / 60);
|
|
var days = Math.floor(hours / 24);
|
|
var months = Math.floor(days / 30);
|
|
var years = Math.floor(months / 12);
|
|
|
|
var diffType = '';
|
|
var diffValue = 0;
|
|
if (years > 0) {
|
|
diffType = 'year';
|
|
diffValue = years;
|
|
} else {
|
|
if (months > 0) {
|
|
diffType = 'month';
|
|
diffValue = months;
|
|
} else {
|
|
if (days > 0) {
|
|
diffType = 'day';
|
|
diffValue = days;
|
|
} else {
|
|
if (hours > 0) {
|
|
diffType = 'hour';
|
|
diffValue = hours;
|
|
} else {
|
|
if (minutes > 0) {
|
|
diffType = 'minute';
|
|
diffValue = minutes;
|
|
} else {
|
|
diffType = 'second';
|
|
diffValue = seconds === 0 ? (seconds = 1) : seconds;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return formats[diffType].replace('%n%', diffValue);
|
|
}
|
|
|
|
function objectHasKey(o, k) {
|
|
return Object.keys(o).some((v) => v.toLowerCase() === k.toLowerCase())
|
|
}
|
|
|
|
|
|
// 下载图片到本地
|
|
function TaImageDownload(imgUrl, name = '') {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// #ifdef H5
|
|
const link = document.createElement('a')
|
|
link.href = imgUrl
|
|
link.download = name || (Math.random().toString().replace('0.', 'img') + `.png`)
|
|
link.style.display = 'none'
|
|
document.body.appendChild(link)
|
|
link.click()
|
|
document.body.removeChild(link)
|
|
resolve(true)
|
|
// #endif
|
|
|
|
// #ifndef H5
|
|
uni.downloadFile({
|
|
url: imgUrl, success: (ret) => {
|
|
if (ret.statusCode === 200) uni.saveImageToPhotosAlbum({
|
|
filePath: ret.tempFilePath,
|
|
fail: (err) => TaToast.error('保存失败!', err).then(reject),
|
|
success: () => TaToast.success('保存成功!').then(resolve),
|
|
});
|
|
}
|
|
});
|
|
// #endif
|
|
})
|
|
}
|
|
|
|
export {
|
|
TaImageDownload,
|
|
friendlyDate,
|
|
objectHasKey
|
|
}
|