const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } // 获取当前页url const getCurrentPageUrl = () => { let pages = getCurrentPages(); let currentPage = pages[pages.length - 1]; let url = '/' + currentPage.route; let options = currentPage.options; for (let key in options) { url += (url.indexOf('?') >= 0 ? '&' : '?') + `${key}=${options[key]}`; } return url; } const setTimeOutStorage = (key,value,timeout) => { let timestamp = Date.parse(new Date()); wx.setStorageSync(key, { 'value': value, 'timeout': timestamp + timeout }) } const getTimeoutStorage = (key) => { // 当前时间 let timestamp = Date.parse(new Date()); // 缓存中的过期时间 let data = wx.getStorageSync(key); let data_expiration = data.timeout; let value = data.value; if (data_expiration) { // 如果超时了,清除缓存,重新登录 if (timestamp > data_expiration) { wx.removeStorageSync(key); return null; } } return value; } // 计算两点之间的距离(返回米) const getDistance = (lat1, lng1, lat2, lng2) => { const radLat1 = lat1 * Math.PI / 180.0; const radLat2 = lat2 * Math.PI / 180.0; const a = radLat1 - radLat2; const b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0; let 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) / 10; // 换算成米 return s; } module.exports = { formatTime: formatTime, getCurrentPageUrl: getCurrentPageUrl, setTimeOutStorage: setTimeOutStorage, getTimeoutStorage: getTimeoutStorage, getDistance: getDistance }