const { default: api } = require("../js/api") 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 formatDateNotSecond = isoString => { if (isoString == undefined) return ''; const date = new Date(isoString); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${year}-${month}-${day} ${hours}:${minutes}`; } const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } const formatPhone = phone => { // 检查手机号是否有效 if (!phone || phone.length !== 11) { return phone; } // 获取手机号前3位和后4位 const start = phone.substring(0, 3); const end = phone.substring(7); // 将中间4位数字替换为星号 const hidden = '****'; // 返回处理后的手机号 return start + hidden + end; } // 格式化小数点数字 const formatDiNumber = number => { var numberStr = number.toString() if(!numberStr.includes(".")){ numberStr += ".00" } return numberStr } //根据经纬度计算距离 const haversine = (lat1, lon1, lat2, lon2) => { const R = 6371; // 地球半径,单位:千米 const toRadians = (degrees) => degrees * (Math.PI / 180); // 将度数转换为弧度 // 将经纬度转换为弧度 const phi1 = toRadians(lat1); const phi2 = toRadians(lat2); const deltaPhi = toRadians(lat2 - lat1); const deltaLambda = toRadians(lon2 - lon1); // Haversine公式 const a = Math.sin(deltaPhi / 2) ** 2 + Math.cos(phi1) * Math.cos(phi2) * Math.sin(deltaLambda / 2) ** 2; const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // 计算距离 const distance = R * c; // 单位:千米 return distance * 1000; // 转换为米 } function subAndsendMessage(id,type) { return new Promise((resolve,reject)=>{ wx.requestSubscribeMessage({ tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条 success(res) { sendMessage(id,type,resolve,reject) // 'accept'表示用户同意订阅该条id对应的模板消息 if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') { // 用户同意订阅,调用云函数或服务器接口发送订阅消息 // wx.cloud.callFunction({ // name: 'sendSubscribeMessage', // data: { // templateId: '配置好的模板ID', // openid: 'o8pFb5cWH1KkBDvGls2X7yMiFkGA', // data: { // thing1: { // value: '活动名称' // }, // // 其他参数... // } // }, // success(res) { // console.log('订阅消息发送成功', res) // }, // fail(err) { // console.error('订阅消息发送失败', err) // } // }) } }, fail(err) { sendMessage(id,type,resolve,reject) } }) }) } function sendMessage(id,type,resolve,reject) { const message = { trxid:id, orderType:type } api.request_sendMessage(message).then(res => { console.log("发送消息模板结果",res) resolve() }).catch(err => { console.log("发送消息模板失败",err) reject() }) } module.exports = { formatTime, formatDateNotSecond, formatPhone, formatDiNumber, haversine, subAndsendMessage }