四零语境前端代码仓库
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.

148 lines
3.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
  1. // 用来保存图片的函数
  2. // params: 需要授权的内容
  3. // scope.userInfo 用户信息
  4. // scope.userLocation 地理位置
  5. // scope.userLocationBackground 后台定位 微信小程序
  6. // scope.address 通信地址
  7. // scope.record 录音功能
  8. // scope.writePhotosAlbum 保存到相册 抖音小程序的返回值是scope.album
  9. // scope.camera 摄像头
  10. // scope.invoice 获取发票
  11. // scope.invoiceTitle 发票抬头
  12. // scope.werun wx.getWeRunData 微信运动步数
  13. const scopeList = {
  14. userInfo: 'scope.userInfo',
  15. userLocation: 'scope.userLocation',
  16. userLocationBackground: 'scope.userLocationBackground',
  17. address: 'scope.address',
  18. record: 'scope.record',
  19. writePhotosAlbum: 'scope.writePhotosAlbum',
  20. camera: 'scope.camera',
  21. invoice: 'scope.invoice',
  22. invoiceTitle: 'scope.invoiceTitle',
  23. werun: 'scope.werun',
  24. }
  25. const authorize = ({
  26. scope,
  27. successfn,
  28. failfn,
  29. }) => {
  30. if (!scopeList[scope]) {
  31. uni.showToast({
  32. title: 'scope参数错误',
  33. icon: 'error'
  34. })
  35. return
  36. }
  37. uni.authorize({
  38. scope: scopeList[scope],
  39. success() {
  40. successfn()
  41. },
  42. fail() {
  43. failfn()
  44. }
  45. })
  46. }
  47. // 检验手机号格式
  48. const checkPhone = (phone) => {
  49. if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
  50. return false
  51. }
  52. return true;
  53. }
  54. // 转换时间戳为yyyy-mm-dd
  55. // params: 时间戳
  56. // return yyyy-mmm-dd
  57. const formatTime = (time) => {
  58. if (!time) {
  59. return '时间格式错误,需要传入时间戳'
  60. }
  61. const date = new Date(time)
  62. const year = date.getFullYear()
  63. const month = String(date.getMonth() + 1).padStart(2, '0')
  64. const day = String(date.getDate()).padStart(2, '0')
  65. return `${year}-${month}-${day}`
  66. }
  67. // 计算yyyy-mm-dd与当前时间的差值
  68. // params: yyyy-mm-dd格式的字符串
  69. // return: 差值(天)
  70. const calculateDateDifference = (dateString) => {
  71. if (!dateString) {
  72. return '时间格式错误,需要传入yyyy-mm-dd格式的字符串'
  73. }
  74. // 传入值为yyyy-mm-dd格式的字符串
  75. const inputDate = new Date(dateString)
  76. // 化为时间戳
  77. // const inputTime = inputDate.getTime()
  78. const currentDate = new Date()
  79. const timeDifference = inputDate - currentDate
  80. if (!(currentDate.setHours(0, 0, 0, 0) - inputDate.setHours(0, 0, 0, 0))) {
  81. return 0
  82. }
  83. // 如果为负 返回-1
  84. if (timeDifference < 0) {
  85. return -1
  86. }
  87. // 计算天数
  88. const dayDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24))
  89. return dayDifference
  90. }
  91. // 微信支付方法
  92. // 传参1: 支付数据
  93. // 传参2: 成功回调
  94. // 传参3: 失败回调
  95. // 传三个参数 支付数据 成功回调 失败回调
  96. const wxPay = (paymentData, successCallback, failCallback) => {
  97. uni.requestPayment({
  98. provider: 'wxpay',
  99. timeStamp: paymentData.timeStamp,
  100. nonceStr: paymentData.nonceStr,
  101. package: paymentData.packageValue,
  102. signType: paymentData.signType,
  103. paySign: paymentData.paySign,
  104. success: (res) => {
  105. if (successCallback) {
  106. successCallback(res)
  107. }
  108. console.log('支付成功:', res)
  109. uni.showToast({
  110. title: '支付成功',
  111. icon: 'success'
  112. })
  113. if (successCallback) {
  114. successCallback(res)
  115. }
  116. },
  117. fail: (err) => {
  118. console.log('支付失败:', err)
  119. if (failCallback) {
  120. failCallback(err)
  121. }
  122. if (err.errMsg === 'requestPayment:fail cancel') {
  123. uni.showToast({
  124. title: '支付已取消',
  125. icon: 'none'
  126. })
  127. } else {
  128. uni.showToast({
  129. title: '支付失败',
  130. icon: 'none'
  131. })
  132. }
  133. }
  134. })
  135. }
  136. export {
  137. authorize,
  138. checkPhone,
  139. formatTime,
  140. calculateDateDifference,
  141. wxPay
  142. }