| // 检验手机号格式 | |
| const checkPhone = (phone) => { | |
|   if (!phone || !/^1[3-9]\d{9}$/.test(phone)) { | |
|     return false | |
|   } | |
|   return true; | |
| } | |
| 
 | |
| // 转换时间戳为yyyy-mm-dd | |
| // params: 时间戳 | |
| // return yyyy-mmm-dd | |
| const formatTime = (time) => { | |
|   if (!time) { | |
|     return '时间格式错误,需要传入时间戳' | |
|   } | |
|   const date = new Date(time) | |
|   const year = date.getFullYear() | |
|   const month = String(date.getMonth() + 1).padStart(2, '0') | |
|   const day = String(date.getDate()).padStart(2, '0') | |
|   return `${year}-${month}-${day}` | |
| } | |
| 
 | |
| // 计算yyyy-mm-dd与当前时间的差值 | |
| // params: yyyy-mm-dd格式的字符串 | |
| // return: 差值(天) | |
| const calculateDateDifference = (dateString) => { | |
|   if (!dateString) { | |
|     return '时间格式错误,需要传入yyyy-mm-dd格式的字符串' | |
|   } | |
|   // 传入值为yyyy-mm-dd格式的字符串 | |
|   const inputDate = new Date(dateString) | |
|   // 化为时间戳 | |
|   // const inputTime = inputDate.getTime() | |
|   const currentDate = new Date() | |
|   const timeDifference = inputDate - currentDate | |
| 
 | |
|   if (!(currentDate.setHours(0, 0, 0, 0) - inputDate.setHours(0, 0, 0, 0))) { | |
|     return 0 | |
|   } | |
|   // 如果为负 返回-1 | |
|   if (timeDifference < 0) { | |
|     return -1 | |
|   } | |
|   // 计算天数 | |
|   const dayDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24)) | |
| 
 | |
|   return dayDifference | |
| } | |
| 
 | |
| // #ifdef H5 | |
| import jWeixin from './lib/jweixin-module.js' | |
| // #endif | |
|  | |
| /** | |
|  * 微信支付方法 - 支持小程序和公众号多环境 | |
|  * @param {Object} paymentData - 支付数据对象 | |
|  * @param {Function} successCallback - 成功回调函数 | |
|  * @param {Function} failCallback - 失败回调函数 | |
|  * @returns {Promise} - H5环境返回Promise,小程序环境直接执行回调 | |
|  */ | |
| const wxPay = (paymentData, successCallback, failCallback) => { | |
|   // #ifdef MP-WEIXIN | |
|   // 小程序环境使用uni.requestPayment | |
|   uni.requestPayment({ | |
|     provider: 'wxpay', | |
|     timeStamp: paymentData.timeStamp, | |
|     nonceStr: paymentData.nonceStr, | |
|     package: paymentData.packageValue, | |
|     signType: paymentData.signType, | |
|     paySign: paymentData.paySign, | |
|     success: (res) => { | |
|       console.log('小程序支付成功:', res) | |
|       uni.showToast({ | |
|         title: '支付成功', | |
|         icon: 'success' | |
|       }) | |
|       if (successCallback) { | |
|         successCallback(res) | |
|       } | |
|     }, | |
|     fail: (err) => { | |
|       console.log('小程序支付失败:', err) | |
|       if (err.errMsg === 'requestPayment:fail cancel') { | |
|         uni.showToast({ | |
|           title: '支付已取消', | |
|           icon: 'none' | |
|         }) | |
|       } else { | |
|         uni.showToast({ | |
|           title: '支付失败', | |
|           icon: 'none' | |
|         }) | |
|       } | |
|       if (failCallback) { | |
|         failCallback(err) | |
|       } | |
|     } | |
|   }) | |
|   // #endif | |
|  | |
|   // #ifdef H5 | |
|   // H5环境使用微信JSSDK | |
|   return new Promise((resolve, reject) => { | |
|     // JSSDK配置成功后的回调 | |
|     jWeixin.ready(function() { | |
|       // 调用微信支付接口 | |
|       jWeixin.chooseWXPay({ | |
|         appId: paymentData.appId, | |
|         timestamp: paymentData.timeStamp, // 支付签名时间戳 | |
|         nonceStr: paymentData.nonceStr, // 支付签名随机串 | |
|         package: paymentData.packageValue, // 统一支付接口返回的prepay_id参数值 | |
|         signType: paymentData.signType, // 签名类型,默认为MD5 | |
|         paySign: paymentData.paySign, // 支付签名 | |
|         success: function(result) { | |
|           console.log('H5支付成功:', result) | |
|           uni.showToast({ | |
|             title: '支付成功', | |
|             icon: 'success' | |
|           }) | |
|           if (successCallback) { | |
|             successCallback(result) | |
|           } | |
|           resolve(result) | |
|         }, | |
|         fail: function(error) { | |
|           console.log('H5支付失败:', error) | |
|           uni.showToast({ | |
|             title: '支付失败', | |
|             icon: 'none' | |
|           }) | |
|           if (failCallback) { | |
|             failCallback(error) | |
|           } | |
|           reject(error) | |
|         }, | |
|         cancel: function(error) { | |
|           console.log('H5支付取消:', error) | |
|           uni.showToast({ | |
|             title: '支付已取消', | |
|             icon: 'none' | |
|           }) | |
|           const cancelError = { type: 'cancel', ...error } | |
|           if (failCallback) { | |
|             failCallback(cancelError) | |
|           } | |
|           reject(cancelError) | |
|         } | |
|       }) | |
|     }) | |
| 
 | |
|     // JSSDK配置失败处理 | |
|     jWeixin.error(function(error) { | |
|       console.log('JSSDK配置失败:', error) | |
|       uni.showToast({ | |
|         title: 'JSSDK配置失败', | |
|         icon: 'none' | |
|       }) | |
|       const configError = { type: 'config_error', ...error } | |
|       if (failCallback) { | |
|         failCallback(configError) | |
|       } | |
|       reject(configError) | |
|     }) | |
|   }) | |
|   // #endif | |
| } | |
| 
 | |
| 
 | |
| 
 | |
| export { | |
|   checkPhone, | |
|   formatTime, | |
|   calculateDateDifference, | |
|   wxPay | |
| }
 |