赞
踩
本人写项目常用的公共方法
不定期更新,有什么补充欢迎私聊
var method = {}
//复制
method.setClipboardData = (text) => {
uni.setClipboardData({
data: text, //要被复制的内容
success: () => { //复制成功的回调函数
uni.showToast({ //提示
title: '复制成功',
icon: "none"
})
}
});
}
//保存图片
method.saveImageToPhotosAlbum = (url) => {
uni.saveImageToPhotosAlbum({
filePath: url,
success: function() {
uni.showToast({ //提示
title: '保存成功',
icon: "none"
})
}
});
}
//预览图片 只是预览图片,不做任何操作 url 一维数组
method.previewImage = (url) => {
uni.previewImage({
urls: url
});
}
//计算两个经纬度之间的距离
method.distance = (la1, lo1, la2, lo2) => {
var La1 = la1 * Math.PI / 180.0;
var La2 = la2 * Math.PI / 180.0;
var La3 = La1 - La2;
var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math
.sin(Lb3 / 2), 2)));
s = s * 6378.137; //地球半径
s = Math.round(s * 10000) / 10000;
return s;
}
method.compare = (property) => {
return function(a, b) {
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
//去除富文本的特殊符号
method.quhtml = (msg) => {
console.log(msg)
var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
msg = msg.replace(/[|]*\n/, ''); //去除行尾空格
msg = msg.replace(/&npsp;/gi, ''); //去掉npsp
return msg;
}
//获取当前时间戳 到毫秒
method.newdate1000 = () => {
return Math.round(new Date())
}
//获取当前时间戳
method.mathround = () => {
return Math.round(new Date() / 1000)
}
//将时间戳转化为时间--y-m-d
method.dateymd = (time) => {
console.log(time)
var date = new Date(time * 1000);
let Y = date.getFullYear() + '-';
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
let D = date.getDate() + ' ';
let h = date.getHours() + ':';
let m = date.getMinutes() + ':';
let s = date.getSeconds();
return Y + M + D;
}
//将时间戳转化为时间--y-m-d h:i:s
method.dateymdhis = (time) => {
console.log(123)
var date = new Date(time * 1000);
let Y = date.getFullYear() + '-';
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
let D = date.getDate() + ' ';
let h = date.getHours() + ':';
let m = date.getMinutes() + ':';
let s = date.getSeconds();
return Y + M + D + h + m + s;
}
//将时间戳转化为时间-- h:i:s
method.datehis = (time) => {
var date = new Date(time * 1000);
let h = date.getHours() + ':';
let m = date.getMinutes() + ':';
let s = date.getSeconds();
return h + m + s;
}
//将时间戳转化为时间-- M-D h:i
method.datemdhi = (time) => {
var date = new Date(time * 1000);
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
let D = date.getDate() + ' ';
let h = date.getHours() + ':';
let m = date.getMinutes();
return M + D + h + m;
}
//将时间转化为时间戳
method.oldtime = (times) => {
var oldTime = (new Date(times)).getTime() / 1000;
return oldTime;
}
//根据时间戳计算天数
method.utc = (start) => {
var end = module.mathround(); //2017-5-10 10:00
var utc = end - start;
return Math.round(utc / (24 * 60 * 60)); // 天
}
//计算字符长度
method.getlength = (str) => {
return str.replace(/[\u0391-\uFFE5]/g, 'aa').length; //先把中文替换成两个字节的英文,在计算长度
}
//获取文件名称
method.imgname = (img) => {
var gang = img.lastIndexOf('/')
var length = img.length
return img.slice(gang, length)
}
//将二位数组中的时间戳转化为时间 具体转化的格式调用以上对应的实践转化
method.datamdhi = (data,time='createtime') => {
for(var i=0;i<data.length;i++){
console.log(data[i][time])
data[i][time]=method.datemdhi(data[i][time])
}
return data;
}
module.exports = method
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。