feat: 添加归还成功页面及相关功能

在 `pages.json` 中新增归还成功页面的配置,并在 `order/success.vue` 中实现设备状态提示和加载动画。同时,更新了订单支付逻辑,确保在支付成功后能够正确弹出充电宝。优化了订单状态查询和处理逻辑,提升用户体验。
This commit is contained in:
8vd8
2025-04-11 18:03:32 +08:00
parent 2da6ef8f41
commit f96ff2b030
33 changed files with 1505 additions and 536 deletions
+95 -104
View File
@@ -203,26 +203,69 @@ export default {
title: '处理中'
})
// 调用后端创建微信支付订单接口
console.log('开始处理支付订单号:', this.orderId)
// 调用后端支付API
const res = await uni.request({
url: `${uni.getStorageSync('baseUrl') || 'http://127.0.0.1:8080'}/app/wx-payment/create/${this.orderInfo.orderNo}`,
url: `${uni.getStorageSync('baseUrl') || 'http://127.0.0.1:8080'}/app/payment/${this.orderInfo.orderNo}`,
method: 'GET',
header: {
'Authorization': "Bearer " + uni.getStorageSync('token'),
'Authorization': 'Bearer ' + uni.getStorageSync('token'),
'Clientid': uni.getStorageSync('client_id')
}
})
console.log('支付API返回结果:', res.data)
if (res.statusCode === 200 && res.data.code === 200) {
// 支付成功,跳转到支付成功页面
uni.hideLoading()
uni.redirectTo({
url: `/pages/order/success?orderId=${this.orderId}`
// 获取微信支付所需参数
const payParams = res.data.data
console.log('准备调用微信支付,参数:', payParams)
// 验证支付参数是否完整
if (!payParams.timeStamp || !payParams.nonceStr ||
!payParams.packageValue || !payParams.paySign) {
console.error('支付参数不完整:', payParams)
throw new Error('支付参数不完整,请联系客服')
}
// 调用微信支付API
uni.requestPayment({
provider: 'wxpay',
timeStamp: payParams.timeStamp,
nonceStr: payParams.nonceStr,
package: payParams.packageValue, // 后端返回的是packageValue字段
signType: payParams.signType || 'MD5',
paySign: payParams.paySign,
success: (payRes) => {
console.log('支付成功:', payRes)
uni.hideLoading()
// 支付成功后开始轮询订单状态
this.pollOrderStatus()
},
fail: (err) => {
console.error('支付失败:', err)
uni.hideLoading()
// 用户取消支付的情况,不显示错误提示
if (err.errMsg === 'requestPayment:fail cancel') {
console.log('用户取消了支付')
return
}
uni.showToast({
title: err.errMsg || '支付失败',
icon: 'none'
})
},
complete: () => {
console.log('支付流程结束')
}
})
} else {
throw new Error(res.data.msg || '支付失败')
throw new Error(res.data?.msg || '支付请求失败')
}
} catch (error) {
console.error('支付处理错误:', error)
uni.hideLoading()
uni.showToast({
title: error.message || '支付失败',
@@ -231,105 +274,32 @@ export default {
}
},
// 发送租借指令
async sendRentCommand() {
try {
uni.showLoading({
title: '处理中'
})
// 调用发送租借指令的接口
const res = await this.sendRentRequest()
if (res.code === 200) {
uni.hideLoading()
uni.showToast({
title: '租借成功',
icon: 'success'
})
// 跳转到订单列表页面
setTimeout(() => {
uni.redirectTo({
url: `/pages/order/index?orderId=${this.orderId}`
})
}, 1500)
} else {
throw new Error(res.msg || '租借失败')
}
} catch (error) {
uni.hideLoading()
uni.showToast({
title: error.message || '租借失败',
icon: 'none'
})
}
},
// 发送租借请求
sendRentRequest() {
return new Promise((resolve, reject) => {
uni.request({
url: `${uni.getStorageSync('baseUrl') || 'http://127.0.0.1:8080'}/app/device/sendRentCommand`,
method: 'POST',
data: {
orderId: this.orderId
},
header: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': "Bearer " + uni.getStorageSync('token'),
'Clientid': uni.getStorageSync('client_id')
},
success(res) {
if (res.statusCode === 200) {
resolve(res.data)
} else {
reject(new Error('请求失败'))
}
},
fail(err) {
reject(err)
}
})
})
},
// 格式化时间
formatTime(date) {
const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const hour = date.getHours().toString().padStart(2, '0')
const minute = date.getMinutes().toString().padStart(2, '0')
return `${year}-${month}-${day} ${hour}:${minute}`
},
// 轮询订单状态
async pollOrderStatus() {
let retryCount = 0;
const maxRetries = 10;
const interval = 1000; // 1秒间隔
const maxRetries = 30; // 增加最大重试次数,允许更长时间检测
const interval = 2000; // 调整为2秒间隔
const checkStatus = async () => {
try {
const res = await uni.request({
url: `${uni.getStorageSync('baseUrl') || 'http://127.0.0.1:8080'}/app/payment/status/${this.orderInfo.orderNo}`,
method: 'GET',
header: {
'Authorization': "Bearer " + uni.getStorageSync('token'),
'Clientid': uni.getStorageSync('client_id')
}
});
// 使用queryById方法直接查询订单状态
const res = await queryById(this.orderId);
console.log('轮询订单状态结果:', res);
if (res.statusCode === 200 && res.data.code === 200) {
const orderData = res.data.data;
if (orderData.orderStatus === 'IN_USED') {
// 支付成功,订单已开始使用
if (res.code === 200 && res.data) {
const orderData = res.data;
// 检查订单是否已支付成功
if (orderData.orderStatus === 'IN_USED' ||
orderData.orderStatus === 'PAYMENT_SUCCESSFUL') {
console.log('支付成功,订单状态:', orderData.orderStatus);
// 显示弹出充电宝的提示
uni.showToast({
title: '支付成功',
icon: 'success'
title: '支付成功,充电宝已弹出',
icon: 'success',
duration: 2000
});
// 延迟跳转到支付成功页面
setTimeout(() => {
uni.redirectTo({
url: `/pages/order/success?orderId=${this.orderId}`
@@ -341,18 +311,39 @@ export default {
if (retryCount < maxRetries) {
retryCount++;
console.log(`${retryCount}次轮询,等待订单状态更新...`);
setTimeout(checkStatus, interval);
} else {
throw new Error('订单状态查询超时');
console.error('轮询订单状态超时');
uni.showModal({
title: '提示',
content: '订单状态查询超时,请在"我的订单"中查看订单状态',
showCancel: false,
success: function(res) {
if (res.confirm) {
uni.redirectTo({
url: '/pages/order/index'
});
}
}
});
}
} catch (error) {
uni.showToast({
title: error.message || '查询订单状态失败',
icon: 'none'
});
console.error('查询订单状态失败:', error);
// 出错时继续轮询,不要中断
if (retryCount < maxRetries) {
retryCount++;
setTimeout(checkStatus, interval);
} else {
uni.showToast({
title: '查询订单状态失败',
icon: 'none'
});
}
}
};
// 开始轮询
checkStatus();
},
}