展品维保小程序前端代码接口
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.

101 lines
2.5 KiB

2 weeks ago
2 weeks ago
2 weeks 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. export {
  92. authorize,
  93. checkPhone,
  94. formatTime,
  95. calculateDateDifference
  96. }