最新版公众号前端
This commit is contained in:
157
src/utils/utils.js
Normal file
157
src/utils/utils.js
Normal file
@@ -0,0 +1,157 @@
|
||||
//时间转换
|
||||
function timestampToTime(timestamp, layout) {
|
||||
let flag = timestamp+'';
|
||||
if(flag.length!=13){
|
||||
var date = new Date(timestamp * 1000);
|
||||
}else{
|
||||
var date = new Date(timestamp);
|
||||
}
|
||||
var Y = date.getFullYear()
|
||||
var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
|
||||
var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
|
||||
var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
|
||||
var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
|
||||
var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
|
||||
if (layout == 1) {
|
||||
return Y + "-" + M + "-" + D;
|
||||
} else if (layout == 2) {
|
||||
return h + ":" + m + ":" + s;
|
||||
} else if (layout == 3) {
|
||||
return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
|
||||
} else if (layout == 4) {
|
||||
return Y + "年" + M + "月" + D + "日"
|
||||
} else if (layout == 5) {
|
||||
return Y + "/" + M + "/" + D
|
||||
} else if (layout == 6) {
|
||||
return M + "-" + D
|
||||
} else if (layout == 7) {
|
||||
return h + ":" + m
|
||||
} else if (layout == 8) {
|
||||
return Y + "-" + M + "-" + D + " " + h + ":" + m;
|
||||
} else if (layout == 9) {
|
||||
return Y + "/" + M + "/" + D + " " + h + ":" + m;
|
||||
} else if (layout == 10) {
|
||||
return M + "/" + D
|
||||
} else if (layout == 11) {
|
||||
return Y + "." + M + "." + D
|
||||
} else if (layout == 12) {
|
||||
return Y + "." + M + "." + D + " " + h + ":" + m + ":" + s;
|
||||
} else if (layout == 13) {
|
||||
return Y + "年" + M + "月" + D + "日 " + (h>12?"下午":"上午") + h + ":" + m;
|
||||
}
|
||||
}
|
||||
// 时间差
|
||||
function differenceTime(timestamp) {
|
||||
// 补全为13位
|
||||
let arrTimestamp = (timestamp + '').split('');
|
||||
for (let start = 0; start < 13; start++) {
|
||||
if (!arrTimestamp[start]) {
|
||||
arrTimestamp[start] = '0';
|
||||
}
|
||||
}
|
||||
timestamp = arrTimestamp.join('') * 1;
|
||||
let minute = 1000 * 60;
|
||||
let hour = minute * 60;
|
||||
let day = hour * 24;
|
||||
let month = day * 30;
|
||||
let now = new Date().getTime();
|
||||
let diffValue = now - timestamp;
|
||||
// 如果本地时间反而小于变量时间
|
||||
if (diffValue < 0) {
|
||||
return '即将';
|
||||
}
|
||||
// 计算差异时间的量级
|
||||
let monthC = diffValue / month;
|
||||
let weekC = diffValue / (7 * day);
|
||||
let dayC = diffValue / day;
|
||||
let hourC = diffValue / hour;
|
||||
let minC = diffValue / minute;
|
||||
// 数值补0方法
|
||||
let zero = function (value) {
|
||||
if (value < 10) {
|
||||
return '0' + value;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
// 使用
|
||||
if (monthC > 12) {
|
||||
return timestampToTime(timestamp,1)
|
||||
} 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 '刚刚';
|
||||
}
|
||||
//判断后缀类型
|
||||
function matchType(fileName) {
|
||||
console.log(fileName)
|
||||
// 后缀获取
|
||||
let suffix = "";
|
||||
// 获取类型结果
|
||||
let result;
|
||||
try {
|
||||
const fileArr = fileName.split('.')
|
||||
suffix = fileArr[fileArr.length - 1];
|
||||
} catch (err) {
|
||||
suffix = "";
|
||||
}
|
||||
// fileName无后缀返回 false
|
||||
if (!suffix) {
|
||||
result = false
|
||||
return result
|
||||
}
|
||||
// 文件格式
|
||||
const fileList = ['docx', 'doc', 'pdf'];
|
||||
result = fileList.some(function(item) {
|
||||
return item === suffix
|
||||
})
|
||||
if (result) {
|
||||
return 'file'
|
||||
}
|
||||
// 图片格式
|
||||
// const imgList = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'svg', 'icon', 'ico'];
|
||||
// 进行图片匹配
|
||||
// result = imgList.some(function(item) {
|
||||
// return item === suffix
|
||||
// })
|
||||
// if (result) {
|
||||
// result = 'image'
|
||||
// return result
|
||||
// }
|
||||
}
|
||||
//转义
|
||||
function transferred(html) {
|
||||
return html
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/&nbsp;/g, " ")
|
||||
.replace(/ /g, " ")
|
||||
}
|
||||
//富文本解析
|
||||
function formatRichText(html) {
|
||||
//控制小程序中图片大小
|
||||
let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
|
||||
console.log(match.search(/style=/gi));
|
||||
if (match.search(/style=/gi) == -1) {
|
||||
match = match.replace(/\<img/gi, '<img style=""');
|
||||
}
|
||||
return match;
|
||||
});
|
||||
newContent = newContent.replace(/style="/gi, '$& max-width:100% !important; ');
|
||||
newContent = newContent.replace(/<br[^>]*\/>/gi, '');
|
||||
return newContent;
|
||||
}
|
||||
export {
|
||||
timestampToTime,
|
||||
differenceTime,
|
||||
matchType,
|
||||
transferred,
|
||||
formatRichText
|
||||
}
|
||||
Reference in New Issue
Block a user