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

100 lines
2.5 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. // 微信支付方法
  46. // 传参1: 支付数据
  47. // 传参2: 成功回调
  48. // 传参3: 失败回调
  49. // 传三个参数 支付数据 成功回调 失败回调
  50. const wxPay = (paymentData, successCallback, failCallback) => {
  51. uni.requestPayment({
  52. provider: 'wxpay',
  53. timeStamp: paymentData.timeStamp,
  54. nonceStr: paymentData.nonceStr,
  55. package: paymentData.packageValue,
  56. signType: paymentData.signType,
  57. paySign: paymentData.paySign,
  58. success: (res) => {
  59. if (successCallback) {
  60. successCallback(res)
  61. }
  62. console.log('支付成功:', res)
  63. uni.showToast({
  64. title: '支付成功',
  65. icon: 'success'
  66. })
  67. if (successCallback) {
  68. successCallback(res)
  69. }
  70. },
  71. fail: (err) => {
  72. console.log('支付失败:', err)
  73. if (failCallback) {
  74. failCallback(err)
  75. }
  76. if (err.errMsg === 'requestPayment:fail cancel') {
  77. uni.showToast({
  78. title: '支付已取消',
  79. icon: 'none'
  80. })
  81. } else {
  82. uni.showToast({
  83. title: '支付失败',
  84. icon: 'none'
  85. })
  86. }
  87. }
  88. })
  89. }
  90. export {
  91. checkPhone,
  92. formatTime,
  93. calculateDateDifference,
  94. wxPay
  95. }