fix: 设备扫码检测到订单为in_used状态,跳转不成功的问题

This commit is contained in:
8vd8
2025-04-10 14:46:29 +08:00
parent 3491d93e27
commit 2da6ef8f41
12 changed files with 269 additions and 55 deletions
+13 -2
View File
@@ -62,6 +62,7 @@
let deviceNo = getQueryString(scanResult.path, 'deviceNo')
console.log('扫码路径:', scanResult.path)
console.log('解析到的设备号:', deviceNo)
// 检查是否有使用中的订单
const inUseRes = await uni.request({
@@ -73,12 +74,18 @@
}
})
console.log('使用中订单检查结果:', JSON.stringify(inUseRes))
if (inUseRes.statusCode === 200 && inUseRes.data.code === 200 && inUseRes.data.data) {
// 存在使用中的订单,跳转到归还页面
const inUseOrder = inUseRes.data.data
uni.navigateTo({
url: `/pages/return/index?orderId=${inUseOrder.orderId}&deviceId=${deviceNo}`
console.log('检测到使用中订单,准备跳转:', inUseOrder)
// 直接使用reLaunch而不是navigateTo以确保页面跳转
uni.reLaunch({
url: `/pages/return/index?orderId=${inUseOrder.orderId}&deviceId=${deviceNo || inUseOrder.deviceNo}`
})
console.log('已发起页面跳转')
return
}
@@ -92,14 +99,18 @@
}
})
console.log('待支付订单检查结果:', JSON.stringify(orderRes))
if (orderRes.statusCode === 200 && orderRes.data.code === 200 && orderRes.data.data) {
// 存在待支付订单,跳转到支付页面
const unpaidOrder = orderRes.data.data
console.log('检测到待支付订单,准备跳转:', unpaidOrder)
uni.navigateTo({
url: `/pages/order/payment?orderId=${unpaidOrder.orderId}`
})
} else {
// 无待支付订单,正常跳转到设备检查页面
console.log('无待支付订单,跳转到设备检查页面, deviceNo:', deviceNo)
uni.navigateTo({
url: `/pages/serve/bagCheck/index?deviceNo=${deviceNo}`
})
+118 -21
View File
@@ -74,14 +74,36 @@ export default {
}
},
onLoad(options) {
// 获取传递的参数
this.deviceId = options.deviceId || ''
this.orderInfo.orderId = options.orderId || ''
console.log('Return page loaded with options:', JSON.stringify(options))
// 获取订单详情
this.getOrderDetails()
// 启动定时器更新使用时长
this.startTimer()
// 获取传递的参数
this.orderInfo.orderId = options.orderId || ''
this.deviceId = options.deviceId || ''
console.log(`初始化参数: orderId=${this.orderInfo.orderId}, deviceId=${this.deviceId}`)
// 如果没有orderId但有deviceId, 尝试通过设备号查询订单
if (!this.orderInfo.orderId && this.deviceId) {
this.getOrderByDevice()
} else if (this.orderInfo.orderId) {
// 获取订单详情
this.getOrderDetails()
// 启动定时器更新使用时长
this.startTimer()
} else {
// 缺少必要参数
uni.showToast({
title: '缺少订单信息',
icon: 'none'
})
// 返回首页
setTimeout(() => {
uni.reLaunch({
url: '/pages/index/index'
})
}, 1500)
}
},
onUnload() {
// 页面卸载时清除定时器
@@ -97,30 +119,43 @@ export default {
throw new Error('订单ID不能为空')
}
console.log('请求订单详情, orderId:', this.orderInfo.orderId)
const result = await queryById(this.orderInfo.orderId)
console.log('订单详情结果:', JSON.stringify(result))
if (result.code === 200 && result.data) {
const orderData = result.data
console.log('订单数据:', JSON.stringify(orderData))
// 更新订单信息
this.updateOrderInfo(orderData)
// 格式化开始时间
const rawTime = orderData.startTime
console.log('始时间:', rawTime)
const fixedTime = rawTime.replace(' ', 'T')
const date = new Date(fixedTime)
console.log('原始时间:', rawTime);
if (isNaN(date.getTime())) {
this.orderInfo.startTime = '时间解析错误'
console.log('始时间:', rawTime)
if (rawTime) {
try {
// 尝试格式化时间
const date = new Date(rawTime.replace(' ', 'T'))
if (!isNaN(date.getTime())) {
this.orderInfo.startTime = this.formatTime(date)
} else {
// 如果时间解析失败,直接使用原始时间字符串
this.orderInfo.startTime = rawTime
}
} catch (e) {
console.error('时间格式化错误:', e)
this.orderInfo.startTime = rawTime
}
} else {
console.log('原始时间:', rawTime);
this.orderInfo.startTime = this.formatTime(date)
this.orderInfo.startTime = '未知'
}
// 计算使用时长
this.calculateUsedTime(orderData.createTime)
// 设置当前费用
this.orderInfo.currentFee = orderData.amount || '0.00'
} else {
throw new Error('获取订单详情失败')
throw new Error(result.msg || '获取订单详情失败')
}
} catch (error) {
console.error('获取订单详情错误:', error)
uni.showToast({
title: error.message || '获取订单信息失败',
icon: 'none'
@@ -150,8 +185,14 @@ export default {
// 使用后端返回的使用时长和费用数据
updateOrderInfo(orderData) {
console.log('更新订单信息:', JSON.stringify(orderData))
this.orderInfo.usedTime = orderData.usedTime || '0分钟';
this.orderInfo.currentFee = orderData.currentFee || '0.00';
// 如果有设备号,更新设备号
if (orderData.deviceNo && !this.deviceId) {
this.deviceId = orderData.deviceNo;
}
},
// 更新使用时长的定时器
@@ -195,9 +236,13 @@ export default {
return
}
console.log(`准备结束订单, orderId: ${this.orderInfo.orderId}`)
// 调用结束订单接口
const result = await overOrderById(this.orderInfo.orderId)
console.log('结束订单结果:', JSON.stringify(result))
if (result.code === 200) {
uni.showToast({
title: '归还成功',
@@ -214,6 +259,7 @@ export default {
throw new Error(result.msg || '归还失败')
}
} catch (error) {
console.error('归还操作错误:', error)
uni.showToast({
title: error.message || '归还失败,请重试',
icon: 'none'
@@ -222,6 +268,57 @@ export default {
this.unlocking = false
uni.hideLoading()
}
},
// 通过设备号查询使用中的订单
async getOrderByDevice() {
try {
uni.showLoading({ title: '加载中' })
if (!this.deviceId) {
throw new Error('设备号不能为空')
}
// 这里调用API查询该设备的使用中订单
const inUseRes = await uni.request({
url: `${uni.getStorageSync('baseUrl') || 'http://127.0.0.1:8080'}/app/order/inUse`,
method: 'GET',
header: {
'Authorization': "Bearer " + uni.getStorageSync('token'),
'Clientid': uni.getStorageSync('client_id')
}
})
console.log('通过设备号查询订单结果:', JSON.stringify(inUseRes))
if (inUseRes.statusCode === 200 && inUseRes.data.code === 200 && inUseRes.data.data) {
const inUseOrder = inUseRes.data.data
// 更新订单ID
this.orderInfo.orderId = inUseOrder.orderId
// 获取详细订单信息
this.getOrderDetails()
// 启动定时器
this.startTimer()
} else {
throw new Error('未找到使用中的订单')
}
} catch (error) {
console.error('通过设备号查询订单失败:', error)
uni.showToast({
title: error.message || '获取订单信息失败',
icon: 'none'
})
// 如果获取订单失败,返回首页
setTimeout(() => {
uni.reLaunch({
url: '/pages/index/index'
})
}, 1500)
} finally {
uni.hideLoading()
}
}
}
}