Commonly used public methods when writing projects
It will be updated from time to time. If you have any additions, please feel free to chat privately.
var method = {}
//Copy
method.setClipboardData = (text) => { uni.setClipboardData({ data: text, //The content to be copied success: () => { //Callback function for successful copying uni. showToast({ //Prompt title: 'Copy successful', icon: "none" }) } }); } //Save the picture method.saveImageToPhotosAlbum = (url) => { uni.saveImageToPhotosAlbum({ filePath: url, success: function() { uni.showToast({ //Prompt title: 'Save successfully', icon: "none" }) } }); } //Preview the image Just preview the image without any operation url One-dimensional array method.previewImage = (url) => { uni.previewImage({ urls: url }); }
//Calculate the distance between two latitude and longitude
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; //Earth radius 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 ; } } //Remove special symbols from rich text method.quhtml = (msg) => { console.log(msg) var msg = msg.replace(/<\/?[^>]*>/g, '' ); //Remove HTML Tag msg = msg.replace(/[|]*\n/, ''); //Remove trailing spaces msg = msg.replace(/&npsp;/gi, ''); // Remove npsp return msg; } //Get the current timestamp to milliseconds method.newdate1000 = () => { return Math.round(new Date()) } //Get the current timestamp method.mathround = () => { return Math.round(new Date() / 1000) } //Convert timestamp to time--ymd 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; } //Convert timestamp to time--ymd 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; } //Convert timestamp to time-- 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; } //Convert timestamp to time-- MD 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; } //Convert time to timestamp method.oldtime = (times) => { var oldTime = (new Date(times)).getTime() / 1000; return oldTime; } //According to timestamp Calculate the number of days method.utc = (start) => { var end = module.mathround(); //2017-5-10 10:00 var utc = end - start; return Math.round(utc / (24 * 60 * 60)); // days } //Calculate the character length method.getlength = (str) => { return str.replace(/[\u0391-\uFFE5]/g, 'aa').length; //First Replace Chinese with two bytes of English and calculate the length }
//Get the file name
method.imgname = (img) => { var gang = img.lastIndexOf('/') var length = img.length return img.slice(gang, length) }
//Convert the timestamp in the two-digit array into the specific conversion format of time. Call the corresponding practical conversion method above
. 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