四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
  1. // 检验手机号格式
  2. const checkPhone = (phone) => {
  3. if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
  4. return false
  5. }
  6. return true;
  7. }
  8. // 转换时间戳为yyyy-mm-dd
  9. // params: 时间戳
  10. // return yyyy-mmm-dd
  11. const formatTime = (time) => {
  12. if (!time) {
  13. return '时间格式错误,需要传入时间戳'
  14. }
  15. const date = new Date(time)
  16. const year = date.getFullYear()
  17. const month = String(date.getMonth() + 1).padStart(2, '0')
  18. const day = String(date.getDate()).padStart(2, '0')
  19. return `${year}-${month}-${day}`
  20. }
  21. // 计算yyyy-mm-dd与当前时间的差值
  22. // params: yyyy-mm-dd格式的字符串
  23. // return: 差值(天)
  24. const calculateDateDifference = (dateString) => {
  25. if (!dateString) {
  26. return '时间格式错误,需要传入yyyy-mm-dd格式的字符串'
  27. }
  28. // 传入值为yyyy-mm-dd格式的字符串
  29. const inputDate = new Date(dateString)
  30. // 化为时间戳
  31. // const inputTime = inputDate.getTime()
  32. const currentDate = new Date()
  33. const timeDifference = inputDate - currentDate
  34. if (!(currentDate.setHours(0, 0, 0, 0) - inputDate.setHours(0, 0, 0, 0))) {
  35. return 0
  36. }
  37. // 如果为负 返回-1
  38. if (timeDifference < 0) {
  39. return -1
  40. }
  41. // 计算天数
  42. const dayDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24))
  43. return dayDifference
  44. }
  45. // #ifdef H5
  46. import jWeixin from './lib/jweixin-module.js'
  47. // #endif
  48. /**
  49. * 微信支付方法 - 支持小程序和公众号多环境
  50. * @param {Object} paymentData - 支付数据对象
  51. * @param {Function} successCallback - 成功回调函数
  52. * @param {Function} failCallback - 失败回调函数
  53. * @returns {Promise} - H5环境返回Promise小程序环境直接执行回调
  54. */
  55. const wxPay = (paymentData, successCallback, failCallback) => {
  56. // #ifdef MP-WEIXIN
  57. // 小程序环境使用uni.requestPayment
  58. uni.requestPayment({
  59. provider: 'wxpay',
  60. timeStamp: paymentData.timeStamp,
  61. nonceStr: paymentData.nonceStr,
  62. package: paymentData.packageValue,
  63. signType: paymentData.signType,
  64. paySign: paymentData.paySign,
  65. success: (res) => {
  66. console.log('小程序支付成功:', res)
  67. uni.showToast({
  68. title: '支付成功',
  69. icon: 'success'
  70. })
  71. if (successCallback) {
  72. successCallback(res)
  73. }
  74. },
  75. fail: (err) => {
  76. console.log('小程序支付失败:', err)
  77. if (err.errMsg === 'requestPayment:fail cancel') {
  78. uni.showToast({
  79. title: '支付已取消',
  80. icon: 'none'
  81. })
  82. } else {
  83. uni.showToast({
  84. title: '支付失败',
  85. icon: 'none'
  86. })
  87. }
  88. if (failCallback) {
  89. failCallback(err)
  90. }
  91. }
  92. })
  93. // #endif
  94. // #ifdef H5
  95. // H5环境使用微信JSSDK
  96. return new Promise((resolve, reject) => {
  97. // JSSDK配置成功后的回调
  98. jWeixin.ready(function() {
  99. // 调用微信支付接口
  100. jWeixin.chooseWXPay({
  101. appId: paymentData.appId,
  102. timestamp: paymentData.timeStamp, // 支付签名时间戳
  103. nonceStr: paymentData.nonceStr, // 支付签名随机串
  104. package: paymentData.packageValue, // 统一支付接口返回的prepay_id参数值
  105. signType: paymentData.signType, // 签名类型,默认为MD5
  106. paySign: paymentData.paySign, // 支付签名
  107. success: function(result) {
  108. console.log('H5支付成功:', result)
  109. uni.showToast({
  110. title: '支付成功',
  111. icon: 'success'
  112. })
  113. if (successCallback) {
  114. successCallback(result)
  115. }
  116. resolve(result)
  117. },
  118. fail: function(error) {
  119. console.log('H5支付失败:', error)
  120. uni.showToast({
  121. title: '支付失败',
  122. icon: 'none'
  123. })
  124. if (failCallback) {
  125. failCallback(error)
  126. }
  127. reject(error)
  128. },
  129. cancel: function(error) {
  130. console.log('H5支付取消:', error)
  131. uni.showToast({
  132. title: '支付已取消',
  133. icon: 'none'
  134. })
  135. const cancelError = { type: 'cancel', ...error }
  136. if (failCallback) {
  137. failCallback(cancelError)
  138. }
  139. reject(cancelError)
  140. }
  141. })
  142. })
  143. // JSSDK配置失败处理
  144. jWeixin.error(function(error) {
  145. console.log('JSSDK配置失败:', error)
  146. uni.showToast({
  147. title: 'JSSDK配置失败',
  148. icon: 'none'
  149. })
  150. const configError = { type: 'config_error', ...error }
  151. if (failCallback) {
  152. failCallback(configError)
  153. }
  154. reject(configError)
  155. })
  156. })
  157. // #endif
  158. }
  159. export {
  160. checkPhone,
  161. formatTime,
  162. calculateDateDifference,
  163. wxPay
  164. }