常用自定义函数:
//补零
function fillZero(data){
return data<10?'0'+data:data
}
//获取指定范围的随机整数的方法
function randomInt(min,max){
return Math.floor(Math.random()*(max-min+1)+min)
}
/*
根据秒数获取时分秒并转换成00:00:00格式
*/
function getHHMMSS(time){
let h=window.$utils.fillZero(Math.floor(time/3600))
let m=window.$utils.fillZero(Math.floor(time/60%60))
let s=window.$utils.fillZero(Math.floor(time%60))
return [h,m,s].join(':')
}
/*
根据当前时间获取年月日(参数用来表示用什么分隔数据)
*/
function getYMD(sign){
let date=new Date()
let y=fillZero(date.getFullYear())
let m=fillZero(date.getMonth()+1)
let d=fillZero(date.getDate())
return [y,m,d].join(sign)
}
/*
根据经纬度计算距离
startPoint: 起点
curPoint: 终点
*/
function calcDistance(startPoint, curPoint) {
let lat1 = startPoint.lat
let lng1 = startPoint.lng
let lat2 = curPoint.lat
let lng2 = curPoint.lng
var radLat1 = lat1 * Math.PI / 180.0;
var radLat2 = lat2 * Math.PI / 180.0;
var a = radLat1 - radLat2;
var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378.137;
s = Math.round(s * 10000) / 10000;
return s.toFixed(2) // 单位千米
}
/* 计算配速函数
d: 距离
s: 时间
*/
function calcPace(d, s) {
let distance = d
let spendTime = s / 60
if (distance > 0 && spendTime > 0) {
let pace = parseFloat(spendTime / distance)
let m = Math.floor(pace)
let s = ((pace - m) * 60).toFixed(0)
return `${m}'${s}"`
}
return "--"
}
/* 计算卡路里 */
function calCalorie(miles) {
if (miles > 0) {
let calorie = 60 * miles * 1.036;
return calorie.toFixed(2)
}
return "--"
}
封装本地存储的工具js
export default {
setLocal(key,value){
return localStorage.setItem(key,JSON.stringify(value))
},
getLocal(key){
return JSON.parse(localStorage.getItem(key))
},
removeLocal(key){
return localStorage.removeItem(key)
},
clearLocal(){
return localStorage.clear()
}
}