// 用来保存图片的函数
|
|
// params: 需要授权的内容
|
|
// scope.userInfo 用户信息
|
|
// scope.userLocation 地理位置
|
|
// scope.userLocationBackground 后台定位 微信小程序
|
|
// scope.address 通信地址
|
|
// scope.record 录音功能
|
|
// scope.writePhotosAlbum 保存到相册 抖音小程序的返回值是scope.album
|
|
// scope.camera 摄像头
|
|
// scope.invoice 获取发票
|
|
// scope.invoiceTitle 发票抬头
|
|
// scope.werun wx.getWeRunData 微信运动步数
|
|
const scopeList = {
|
|
userInfo: 'scope.userInfo',
|
|
userLocation: 'scope.userLocation',
|
|
userLocationBackground: 'scope.userLocationBackground',
|
|
address: 'scope.address',
|
|
record: 'scope.record',
|
|
writePhotosAlbum: 'scope.writePhotosAlbum',
|
|
camera: 'scope.camera',
|
|
invoice: 'scope.invoice',
|
|
invoiceTitle: 'scope.invoiceTitle',
|
|
werun: 'scope.werun',
|
|
}
|
|
const authorize = ({
|
|
scope,
|
|
successfn,
|
|
failfn,
|
|
}) => {
|
|
if (!scopeList[scope]) {
|
|
uni.showToast({
|
|
title: 'scope参数错误',
|
|
icon: 'error'
|
|
})
|
|
return
|
|
}
|
|
uni.authorize({
|
|
scope: scopeList[scope],
|
|
success() {
|
|
successfn()
|
|
},
|
|
fail() {
|
|
failfn()
|
|
}
|
|
})
|
|
}
|
|
|
|
// 检验手机号格式
|
|
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
|
|
}
|
|
|
|
// 微信支付方法
|
|
// 传参1: 支付数据
|
|
// 传参2: 成功回调
|
|
// 传参3: 失败回调
|
|
// 传三个参数 支付数据 成功回调 失败回调
|
|
const wxPay = (paymentData, successCallback, failCallback) => {
|
|
uni.requestPayment({
|
|
provider: 'wxpay',
|
|
timeStamp: paymentData.timeStamp,
|
|
nonceStr: paymentData.nonceStr,
|
|
package: paymentData.packageValue,
|
|
signType: paymentData.signType,
|
|
paySign: paymentData.paySign,
|
|
success: (res) => {
|
|
if (successCallback) {
|
|
successCallback(res)
|
|
}
|
|
console.log('支付成功:', res)
|
|
uni.showToast({
|
|
title: '支付成功',
|
|
icon: 'success'
|
|
})
|
|
if (successCallback) {
|
|
successCallback(res)
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.log('支付失败:', err)
|
|
if (failCallback) {
|
|
failCallback(err)
|
|
}
|
|
if (err.errMsg === 'requestPayment:fail cancel') {
|
|
uni.showToast({
|
|
title: '支付已取消',
|
|
icon: 'none'
|
|
})
|
|
} else {
|
|
uni.showToast({
|
|
title: '支付失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
export {
|
|
authorize,
|
|
checkPhone,
|
|
formatTime,
|
|
calculateDateDifference,
|
|
wxPay
|
|
}
|