| | |
| | | import { Document, User, Goods, List } from '@element-plus/icons-vue' |
| | | import { ElMessage, ElMessageBox } from 'element-plus' |
| | | import orderApi from '@/api/orderApi' |
| | | import pointsApi from '@/api/pointsApi' // 新增积分API导入 |
| | | import { useUserInfo } from '@/stores/modules/userInfo' |
| | | import createAxios from '@/utils/axios' |
| | | |
| | |
| | | return |
| | | } |
| | | |
| | | // 检查订单是否涉及积分扣减 |
| | | const hasPointsDeduction = detail.items && detail.items.some((item: any) => Number(item.pricePoint || 0) > 0) |
| | | |
| | | if (hasPointsDeduction) { |
| | | // 计算需要扣减的积分总额 |
| | | const totalPointsToDeduct = detail.items.reduce((sum: number, item: any) => { |
| | | return sum + (Number(item.pricePoint || 0) * Number(item.quantity || 0)) |
| | | }, 0) |
| | | |
| | | if (totalPointsToDeduct > 0) { |
| | | // 获取当前用户积分余额 |
| | | try { |
| | | const userPointsRes = await pointsApi.getUserPoints(userId) |
| | | if (userPointsRes.code !== 200 || !userPointsRes.data) { |
| | | ElMessage.error('获取积分余额失败') |
| | | return |
| | | } |
| | | |
| | | const currentPoints = userPointsRes.data.balance || 0 |
| | | |
| | | if (currentPoints < totalPointsToDeduct) { |
| | | ElMessage.error(`积分余额不足!当前积分:${currentPoints.toLocaleString()},需要积分:${totalPointsToDeduct.toLocaleString()}`) |
| | | return |
| | | } |
| | | |
| | | // 积分余额充足,继续执行 |
| | | console.log(`积分余额检查通过:当前${currentPoints},需要${totalPointsToDeduct}`) |
| | | } catch (error) { |
| | | console.error('获取积分余额失败:', error) |
| | | ElMessage.error('获取积分余额失败,请重试') |
| | | return |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 确认操作 |
| | | await ElMessageBox.confirm('确定要确认交易?', '确认操作', { |
| | | confirmButtonText: '确定', |
| | |
| | | type: 'warning' |
| | | }) |
| | | |
| | | // 更新订单状态进入下一个状态 |
| | | // 如果涉及积分扣减,先进行积分扣减和流水记录 |
| | | if (hasPointsDeduction) { |
| | | const totalPointsToDeduct = detail.items.reduce((sum: number, item: any) => { |
| | | return sum + (Number(item.pricePoint || 0) * Number(item.quantity || 0)) |
| | | }, 0) |
| | | |
| | | if (totalPointsToDeduct > 0) { |
| | | try { |
| | | // 获取用户单位ID |
| | | const unitId = userStore.getUserInfo?.unitId || userStore.getUserInfo?.unitId || '' |
| | | |
| | | // 获取当前订购的产品名称作为备注(使用产品名称,不是套件名称) |
| | | const productNames = detail.productName || '订单交易扣减积分' |
| | | |
| | | // 先执行积分扣减 |
| | | const deductRes: any = await pointsApi.deductPointsByFlow( |
| | | userId.toString(), |
| | | unitId, |
| | | totalPointsToDeduct, |
| | | orderId, |
| | | productNames || '订单交易扣减积分', // 使用产品名称作为备注 |
| | | '积分交易' // 数据类别 |
| | | ) |
| | | if (!deductRes || deductRes.code !== 200) { |
| | | ElMessage.error(deductRes?.msg || deductRes?.message || '积分扣减失败,交易确认终止') |
| | | return |
| | | } |
| | | console.log(`积分扣减成功:${totalPointsToDeduct}`) |
| | | } catch (error) { |
| | | console.error('积分扣减失败:', error) |
| | | ElMessage.error('积分扣减失败,交易确认终止') |
| | | return |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 积分扣减成功后,更新订单状态进入下一个状态 |
| | | await orderApi.updateOrderStatusToNext(orderId) |
| | | |
| | | ElMessage.success('交易确认成功') |
| | | router.back() |
| | | } catch (error) { |