fix:修复bug
This commit is contained in:
+196
-322
@@ -59,345 +59,219 @@
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { queryById, updateOrderPackage } from '@/config/api/order.js'
|
||||
<script setup>
|
||||
import { ref, computed, reactive } from 'vue'
|
||||
import { onLoad } from '@dcloudio/uni-app'
|
||||
import { queryById, createWxPayment } from '@/config/api/order.js'
|
||||
import { getDeviceInfo } from '@/config/api/device.js'
|
||||
import { updateUserBalance } from '@/config/api/user.js'
|
||||
import {
|
||||
URL
|
||||
}from"@/config/url.js"
|
||||
import { useI18n } from '@/utils/i18n.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
orderId: null,
|
||||
deviceNo: null,
|
||||
orderInfo: {},
|
||||
packageInfo: {
|
||||
time: '',
|
||||
price: '0.00'
|
||||
},
|
||||
deviceInfo: null,
|
||||
passedTotalAmount: null,
|
||||
passedDepositAmount: null,
|
||||
orderStatus: {
|
||||
get text() { return this.$t('payment.waitingForPayment') },
|
||||
get desc() { return this.$t('payment.pleasePayIn15Min') },
|
||||
class: 'waiting'
|
||||
}
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
totalAmount() {
|
||||
if (this.passedTotalAmount !== null) {
|
||||
return parseFloat(this.passedTotalAmount).toFixed(2);
|
||||
}
|
||||
const deposit = parseFloat(this.orderInfo.deposit || this.passedDepositAmount || 99)
|
||||
const packagePrice = parseFloat(this.packageInfo.price || 0)
|
||||
return (deposit + packagePrice).toFixed(2)
|
||||
},
|
||||
// 计算每小时的价格
|
||||
hourlyRate() {
|
||||
const price = parseFloat(this.packageInfo.price || 0);
|
||||
let time = parseFloat(this.packageInfo.time || 1);
|
||||
|
||||
// 如果时间单位不是小时(例如分钟),需要转换
|
||||
if (this.packageInfo.time && this.packageInfo.time.includes('分钟')) {
|
||||
time = time / 60; // 将分钟转换为小时
|
||||
} else if (this.packageInfo.time && this.packageInfo.time.includes('次')) {
|
||||
// 按次计费时,暂时显示单次价格
|
||||
return price.toFixed(2);
|
||||
}
|
||||
|
||||
// 避免除以零
|
||||
if (time <= 0) time = 1;
|
||||
|
||||
// 计算每小时价格
|
||||
return (price / time).toFixed(2);
|
||||
}
|
||||
},
|
||||
onLoad(options) {
|
||||
// 设置页面标题
|
||||
uni.setNavigationBarTitle({
|
||||
title: this.$t('payment.orderPayment')
|
||||
const { t } = useI18n()
|
||||
|
||||
const orderId = ref(null)
|
||||
const deviceNo = ref(null)
|
||||
const orderInfo = ref({})
|
||||
const packageInfo = ref({
|
||||
time: '',
|
||||
price: '0.00'
|
||||
})
|
||||
const deviceInfo = ref(null)
|
||||
const passedTotalAmount = ref(null)
|
||||
const passedDepositAmount = ref(null)
|
||||
|
||||
const orderStatus = reactive({
|
||||
get text() { return t('payment.waitingForPayment') },
|
||||
get desc() { return t('payment.pleasePayIn15Min') },
|
||||
class: 'waiting'
|
||||
})
|
||||
|
||||
const totalAmount = computed(() => {
|
||||
if (passedTotalAmount.value !== null) {
|
||||
return parseFloat(passedTotalAmount.value).toFixed(2);
|
||||
}
|
||||
const deposit = parseFloat(orderInfo.value.deposit || passedDepositAmount.value || 99)
|
||||
const packagePrice = parseFloat(packageInfo.value.price || 0)
|
||||
return (deposit + packagePrice).toFixed(2)
|
||||
})
|
||||
|
||||
// 加载订单信息
|
||||
const loadOrderInfo = async () => {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: t('common.loading')
|
||||
})
|
||||
|
||||
if (options && options.orderId) {
|
||||
this.orderId = options.orderId
|
||||
const res = await queryById(orderId.value)
|
||||
if (res.code === 200 && res.data) {
|
||||
const orderData = res.data
|
||||
|
||||
if (options.totalAmount) {
|
||||
this.passedTotalAmount = options.depositAmount;
|
||||
// 处理创建时间
|
||||
let formattedTime;
|
||||
try {
|
||||
if (orderData.createTime) {
|
||||
formattedTime = formatTime(new Date(orderData.createTime));
|
||||
} else {
|
||||
formattedTime = formatTime(new Date());
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('时间格式化错误:', e);
|
||||
formattedTime = formatTime(new Date());
|
||||
}
|
||||
|
||||
if (options.depositAmount) {
|
||||
this.passedDepositAmount = options.depositAmount;
|
||||
orderInfo.value = {
|
||||
orderNo: orderData.orderNo || orderData.orderId,
|
||||
deviceNo: orderData.deviceNo,
|
||||
createTime: formattedTime,
|
||||
phone: orderData.phone,
|
||||
deposit: passedDepositAmount.value || orderData.depositAmount || '99.00',
|
||||
}
|
||||
|
||||
// 如果URL中包含了feeConfig参数,保存它
|
||||
if (options.feeConfig) {
|
||||
try {
|
||||
console.log('从URL获取到feeConfig:', options.feeConfig)
|
||||
const feeConfigStr = decodeURIComponent(options.feeConfig)
|
||||
// 创建一个临时的deviceInfo对象保存feeConfig
|
||||
this.deviceInfo = { feeConfig: feeConfigStr }
|
||||
} catch (e) {
|
||||
console.error('解析URL中的feeConfig失败:', e)
|
||||
if (orderData.packageTime && orderData.packagePrice) {
|
||||
const timeInHours = (parseFloat(orderData.packageTime) / 60).toFixed(1);
|
||||
packageInfo.value = {
|
||||
time: timeInHours.toString(),
|
||||
price: orderData.packagePrice.toString()
|
||||
}
|
||||
}
|
||||
|
||||
this.loadOrderInfo()
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: this.$t('order.orderNotExist'),
|
||||
icon: 'none'
|
||||
})
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
}, 1500)
|
||||
deviceNo.value = orderData.deviceNo;
|
||||
await loadDeviceInfo();
|
||||
} else {
|
||||
throw new Error(t('order.getOrderFailed'))
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 加载订单信息
|
||||
async loadOrderInfo() {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: this.$t('common.loading')
|
||||
})
|
||||
|
||||
const res = await queryById(this.orderId)
|
||||
if (res.code === 200 && res.data) {
|
||||
const orderData = res.data
|
||||
|
||||
// 处理创建时间,确保显示的是格式化后的时间
|
||||
let formattedTime;
|
||||
try {
|
||||
// 如果orderData.createTime存在并且是有效的日期字符串/时间戳,则格式化它
|
||||
if (orderData.createTime) {
|
||||
formattedTime = this.formatTime(new Date(orderData.createTime));
|
||||
} else {
|
||||
// 如果createTime不存在,使用当前时间作为创建时间
|
||||
formattedTime = this.formatTime(new Date());
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('时间格式化错误:', e);
|
||||
formattedTime = this.formatTime(new Date());
|
||||
}
|
||||
|
||||
this.orderInfo = {
|
||||
orderNo: orderData.orderNo || orderData.orderId,
|
||||
deviceNo: orderData.deviceNo,
|
||||
createTime: formattedTime,
|
||||
phone: orderData.phone,
|
||||
deposit: this.passedDepositAmount || orderData.depositAmount || '99.00',
|
||||
}
|
||||
|
||||
// 直接从订单数据中获取套餐信息
|
||||
if (orderData.packageTime && orderData.packagePrice) {
|
||||
// 将分钟转换为小时
|
||||
const timeInHours = (parseFloat(orderData.packageTime) / 60).toFixed(1);
|
||||
this.packageInfo = {
|
||||
time: timeInHours.toString(),
|
||||
price: orderData.packagePrice.toString()
|
||||
}
|
||||
}
|
||||
|
||||
// 获取设备信息(但不再用于设置套餐信息)
|
||||
this.deviceNo = orderData.deviceNo;
|
||||
await this.loadDeviceInfo();
|
||||
} else {
|
||||
throw new Error('获取订单信息失败')
|
||||
}
|
||||
|
||||
uni.hideLoading()
|
||||
} catch (error) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: error.message || '获取订单信息失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 加载设备信息
|
||||
async loadDeviceInfo() {
|
||||
if (!this.deviceNo) return;
|
||||
|
||||
try {
|
||||
const res = await getDeviceInfo(this.deviceNo);
|
||||
if (res.code === 200 && res.data) {
|
||||
this.deviceInfo = res.data.device;
|
||||
|
||||
// 设置存款金额
|
||||
if (this.deviceInfo && this.deviceInfo.depositAmount) {
|
||||
this.orderInfo.deposit = this.deviceInfo.depositAmount;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备信息失败:', error);
|
||||
}
|
||||
},
|
||||
|
||||
// 处理支付
|
||||
async handlePayment() {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: this.$t('common.processing')
|
||||
})
|
||||
|
||||
// 调用后端创建微信支付订单接口
|
||||
const res = await uni.request({
|
||||
url: `${URL || 'http://127.0.0.1:8080'}/app/wx-payment/create/${this.orderInfo.orderNo}`,
|
||||
method: 'GET',
|
||||
header: {
|
||||
'Authorization': "Bearer " + uni.getStorageSync('token'),
|
||||
'Clientid': uni.getStorageSync('client_id')
|
||||
}
|
||||
})
|
||||
|
||||
if (res.statusCode === 200 && res.data.code === 200) {
|
||||
const payParams = res.data.data
|
||||
|
||||
// 调用微信支付
|
||||
await uni.requestPayment({
|
||||
...payParams,
|
||||
success: async () => {
|
||||
uni.showToast({
|
||||
title: this.$t('payment.paymentSuccess'),
|
||||
icon: 'success'
|
||||
});
|
||||
|
||||
// 更新用户余额
|
||||
try {
|
||||
await updateUserBalance(this.orderId);
|
||||
} catch (error) {
|
||||
console.warn('更新用户余额失败:', error);
|
||||
}
|
||||
|
||||
// 支付成功后直接跳转到订单页面,不再轮询
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({
|
||||
url: `/pages/order/index?orderId=${this.orderId}`
|
||||
});
|
||||
}, 1500);
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('支付失败:', err)
|
||||
throw new Error(this.$t('payment.paymentFailedRetry'))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
throw new Error(res.data.msg || '创建支付订单失败')
|
||||
}
|
||||
} catch (error) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: error.message || '支付失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 发送租借指令
|
||||
async sendRentCommand() {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: this.$t('common.processing')
|
||||
})
|
||||
|
||||
// 调用发送租借指令的接口
|
||||
const res = await this.sendRentRequest()
|
||||
|
||||
if (res.code === 200) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: this.$t('device.rentSuccess'),
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 跳转到订单列表页面
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({
|
||||
url: `/pages/order/index?orderId=${this.orderId}`
|
||||
})
|
||||
}, 1500)
|
||||
} else {
|
||||
throw new Error(res.msg || this.$t('device.rentFailed'))
|
||||
}
|
||||
} catch (error) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: error.message || this.$t('device.rentFailed'),
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 发送租借请求
|
||||
sendRentRequest() {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: `${URL || '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 checkOrderStatus() {
|
||||
try {
|
||||
const res = await uni.request({
|
||||
url: `${URL || 'http://127.0.0.1:8080'}/app/wx-payment/status/${this.orderInfo.orderNo}`,
|
||||
method: 'GET',
|
||||
header: {
|
||||
'Authorization': "Bearer " + uni.getStorageSync('token'),
|
||||
'Clientid': uni.getStorageSync('client_id')
|
||||
}
|
||||
});
|
||||
|
||||
if (res.statusCode === 200 && res.data.code === 200) {
|
||||
return res.data.data;
|
||||
} else {
|
||||
throw new Error('查询订单状态失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('查询订单状态错误:', error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
} catch (error) {
|
||||
console.error('获取订单信息失败:', error)
|
||||
uni.showToast({
|
||||
title: error.message || t('order.getOrderFailed'),
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
|
||||
// 加载设备信息
|
||||
const loadDeviceInfo = async () => {
|
||||
if (!deviceNo.value) return;
|
||||
|
||||
try {
|
||||
const res = await getDeviceInfo(deviceNo.value);
|
||||
if (res.code === 200 && res.data) {
|
||||
deviceInfo.value = res.data.device;
|
||||
|
||||
if (deviceInfo.value && deviceInfo.value.depositAmount) {
|
||||
orderInfo.value.deposit = deviceInfo.value.depositAmount;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取设备信息失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理支付
|
||||
const handlePayment = async () => {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: t('common.processing')
|
||||
})
|
||||
|
||||
const res = await createWxPayment(orderInfo.value.orderNo)
|
||||
|
||||
if (res && res.code === 200) {
|
||||
const payParams = res.data
|
||||
|
||||
await uni.requestPayment({
|
||||
...payParams,
|
||||
success: async () => {
|
||||
uni.showToast({
|
||||
title: t('payment.paymentSuccess'),
|
||||
icon: 'success'
|
||||
});
|
||||
|
||||
try {
|
||||
await updateUserBalance(orderId.value);
|
||||
} catch (error) {
|
||||
console.warn('更新用户余额失败:', error);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({
|
||||
url: `/pages/order/index?orderId=${orderId.value}`
|
||||
});
|
||||
}, 1500);
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('支付失败:', err)
|
||||
uni.showToast({
|
||||
title: t('payment.paymentFailedRetry'),
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
throw new Error(res?.msg || t('payment.createPayOrderFailed'))
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('支付失败:', error)
|
||||
uni.showToast({
|
||||
title: error.message || t('payment.paymentFailed'),
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化时间
|
||||
const 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}`
|
||||
}
|
||||
|
||||
onLoad((options) => {
|
||||
uni.setNavigationBarTitle({
|
||||
title: t('payment.orderPayment')
|
||||
})
|
||||
|
||||
if (options && options.orderId) {
|
||||
orderId.value = options.orderId
|
||||
|
||||
if (options.totalAmount) {
|
||||
passedTotalAmount.value = options.totalAmount;
|
||||
}
|
||||
|
||||
if (options.depositAmount) {
|
||||
passedDepositAmount.value = options.depositAmount;
|
||||
}
|
||||
|
||||
if (options.feeConfig) {
|
||||
try {
|
||||
const feeConfigStr = decodeURIComponent(options.feeConfig)
|
||||
deviceInfo.value = { feeConfig: feeConfigStr }
|
||||
} catch (e) {
|
||||
console.error('解析URL中的feeConfig失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
loadOrderInfo()
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: t('order.orderNotExist'),
|
||||
icon: 'none'
|
||||
})
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
}, 1500)
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
||||
Reference in New Issue
Block a user