refactor(订单管理): 提取通用订单状态更新函数并重构相关逻辑
将订单状态更新逻辑提取为通用函数updateOrderStatus 重构支付成功、评价提交、确认收货等操作使用新函数 修复确认收货时使用错误订单ID的问题
Showing
1 changed file
with
56 additions
and
33 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2022-09-19 14:11:06 | 2 | * @Date: 2022-09-19 14:11:06 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-08-08 17:38:16 | 4 | + * @LastEditTime: 2025-08-09 18:33:54 |
| 5 | * @FilePath: /jgdl/src/pages/myOrders/index.vue | 5 | * @FilePath: /jgdl/src/pages/myOrders/index.vue |
| 6 | * @Description: 订单管理页面 | 6 | * @Description: 订单管理页面 |
| 7 | --> | 7 | --> |
| ... | @@ -928,6 +928,37 @@ const clearAllCountdowns = () => { | ... | @@ -928,6 +928,37 @@ const clearAllCountdowns = () => { |
| 928 | } | 928 | } |
| 929 | 929 | ||
| 930 | /** | 930 | /** |
| 931 | + * 通用的订单状态更新函数 | ||
| 932 | + * @param {string} orderId - 订单ID | ||
| 933 | + * @param {number} newStatus - 新的订单状态 | ||
| 934 | + * @param {Object} additionalData - 额外需要更新的数据 | ||
| 935 | + * @param {string} viewModeOverride - 可选的buy/sell视图模式覆盖 | ||
| 936 | + */ | ||
| 937 | +const updateOrderStatus = async (orderId, newStatus, additionalData = {}, viewModeOverride = null) => { | ||
| 938 | + const targetViewMode = viewModeOverride || viewMode.value | ||
| 939 | + const orders = targetViewMode === 'buy' ? boughtOrders.value : targetViewMode === 'verification' ? verificationOrders.value : soldOrders.value | ||
| 940 | + const targetOrderIndex = orders.findIndex(o => o.id === orderId) | ||
| 941 | + | ||
| 942 | + if (targetOrderIndex !== -1) { | ||
| 943 | + // 创建新的订单对象来触发响应式更新 | ||
| 944 | + const updatedOrder = { | ||
| 945 | + ...orders[targetOrderIndex], | ||
| 946 | + status: newStatus, | ||
| 947 | + ...additionalData | ||
| 948 | + } | ||
| 949 | + | ||
| 950 | + // 替换数组中的订单对象 | ||
| 951 | + orders.splice(targetOrderIndex, 1, updatedOrder) | ||
| 952 | + | ||
| 953 | + // 使用nextTick确保DOM更新 | ||
| 954 | + await nextTick() | ||
| 955 | + | ||
| 956 | + return updatedOrder | ||
| 957 | + } | ||
| 958 | + return null | ||
| 959 | +} | ||
| 960 | + | ||
| 961 | +/** | ||
| 931 | * 处理支付 | 962 | * 处理支付 |
| 932 | */ | 963 | */ |
| 933 | const handlePayment = ({ id, total_amount, remain_time }) => { | 964 | const handlePayment = ({ id, total_amount, remain_time }) => { |
| ... | @@ -964,15 +995,11 @@ const onPayClose = () => { | ... | @@ -964,15 +995,11 @@ const onPayClose = () => { |
| 964 | * @param {Object} data - 支付成功数据 | 995 | * @param {Object} data - 支付成功数据 |
| 965 | * @param {string} data.orderId - 订单ID | 996 | * @param {string} data.orderId - 订单ID |
| 966 | */ | 997 | */ |
| 967 | -const onPaySuccess = ({ orderId }) => { | 998 | +const onPaySuccess = async ({ orderId }) => { |
| 968 | - // 找到对应的订单并更新状态 | 999 | + // 更新订单状态为已支付 |
| 969 | - const orders = viewMode.value === 'buy' ? boughtOrders.value : viewMode.value === 'verification' ? verificationOrders.value : soldOrders.value | 1000 | + const updatedOrder = await updateOrderStatus(orderId, 5) |
| 970 | - const order = orders.find(o => o.id === orderId) | ||
| 971 | - | ||
| 972 | - if (order) { | ||
| 973 | - // 更新订单状态为已完成 | ||
| 974 | - order.status = 5 | ||
| 975 | 1001 | ||
| 1002 | + if (updatedOrder) { | ||
| 976 | Taro.showToast({ | 1003 | Taro.showToast({ |
| 977 | title: '支付成功,订单已更新', | 1004 | title: '支付成功,订单已更新', |
| 978 | icon: 'success', | 1005 | icon: 'success', |
| ... | @@ -1068,9 +1095,6 @@ const submitRate = async () => { | ... | @@ -1068,9 +1095,6 @@ const submitRate = async () => { |
| 1068 | }) | 1095 | }) |
| 1069 | if (response.code) { | 1096 | if (response.code) { |
| 1070 | // API提交成功后的处理 | 1097 | // API提交成功后的处理 |
| 1071 | - const currentOrders = viewMode.value === 'buy' ? boughtOrders : viewMode.value === 'verification' ? verificationOrders : soldOrders | ||
| 1072 | - const order = currentOrders.value.find(o => o.id === currentRateOrder.value.id) | ||
| 1073 | - if (order) { | ||
| 1074 | // 创建评价数据对象 | 1098 | // 创建评价数据对象 |
| 1075 | const reviewData = { | 1099 | const reviewData = { |
| 1076 | rating: rateScore.value, | 1100 | rating: rateScore.value, |
| ... | @@ -1078,16 +1102,24 @@ const submitRate = async () => { | ... | @@ -1078,16 +1102,24 @@ const submitRate = async () => { |
| 1078 | created_time: new Date().toLocaleString('zh-CN') | 1102 | created_time: new Date().toLocaleString('zh-CN') |
| 1079 | } | 1103 | } |
| 1080 | 1104 | ||
| 1081 | - // 同时更新order.review和order.details.review,确保状态一致 | 1105 | + // 准备额外的更新数据 |
| 1082 | - order.review = reviewData | 1106 | + const additionalData = { |
| 1083 | - if (order.details) { | 1107 | + review: reviewData |
| 1084 | - order.details.review = reviewData | ||
| 1085 | } | 1108 | } |
| 1086 | 1109 | ||
| 1087 | - // 确保订单状态为已完成 | 1110 | + // 如果订单有details,也需要更新details.review |
| 1088 | - order.status = 11 | 1111 | + const currentOrders = viewMode.value === 'buy' ? boughtOrders : viewMode.value === 'verification' ? verificationOrders : soldOrders |
| 1112 | + const existingOrder = currentOrders.value.find(o => o.id === currentRateOrder.value.id) | ||
| 1113 | + if (existingOrder && existingOrder.details) { | ||
| 1114 | + additionalData.details = { | ||
| 1115 | + ...existingOrder.details, | ||
| 1116 | + review: reviewData | ||
| 1117 | + } | ||
| 1089 | } | 1118 | } |
| 1090 | 1119 | ||
| 1120 | + // 使用通用函数更新订单状态 | ||
| 1121 | + await updateOrderStatus(currentRateOrder.value.id, 11, additionalData) | ||
| 1122 | + | ||
| 1091 | Taro.showToast({ | 1123 | Taro.showToast({ |
| 1092 | title: response.msg || '评价提交成功', | 1124 | title: response.msg || '评价提交成功', |
| 1093 | icon: 'success', | 1125 | icon: 'success', |
| ... | @@ -1296,7 +1328,7 @@ const performConfirmReceive = async (orderId) => { | ... | @@ -1296,7 +1328,7 @@ const performConfirmReceive = async (orderId) => { |
| 1296 | businessType: 'weappOrderConfirm', | 1328 | businessType: 'weappOrderConfirm', |
| 1297 | extraData: { | 1329 | extraData: { |
| 1298 | merchant_id: order.merchant_id || '', | 1330 | merchant_id: order.merchant_id || '', |
| 1299 | - merchant_trade_no: order.id, | 1331 | + merchant_trade_no: order.merchant_trade_no, |
| 1300 | transaction_id: order.transaction_id || '' | 1332 | transaction_id: order.transaction_id || '' |
| 1301 | }, | 1333 | }, |
| 1302 | success() { | 1334 | success() { |
| ... | @@ -1366,13 +1398,9 @@ const performConfirmShip = async (orderId) => { | ... | @@ -1366,13 +1398,9 @@ const performConfirmShip = async (orderId) => { |
| 1366 | 1398 | ||
| 1367 | if (response.code) { | 1399 | if (response.code) { |
| 1368 | // API发货成功后的处理 | 1400 | // API发货成功后的处理 |
| 1369 | - const orders = viewMode.value === 'buy' ? boughtOrders : viewMode.value === 'verification' ? verificationOrders : soldOrders | 1401 | + const updatedOrder = await updateOrderStatus(orderId, 9) |
| 1370 | - const order = orders.value.find(o => o.id === orderId) | ||
| 1371 | - | ||
| 1372 | - if (order) { | ||
| 1373 | - // 更新订单状态为待收货 | ||
| 1374 | - order.status = 9 | ||
| 1375 | 1402 | ||
| 1403 | + if (updatedOrder) { | ||
| 1376 | Taro.showToast({ | 1404 | Taro.showToast({ |
| 1377 | title: response.msg || '发货成功', | 1405 | title: response.msg || '发货成功', |
| 1378 | icon: 'success', | 1406 | icon: 'success', |
| ... | @@ -1451,17 +1479,13 @@ const performDeleteOrder = async (orderId) => { | ... | @@ -1451,17 +1479,13 @@ const performDeleteOrder = async (orderId) => { |
| 1451 | * @param {Object} data - 事件数据 | 1479 | * @param {Object} data - 事件数据 |
| 1452 | */ | 1480 | */ |
| 1453 | const handleConfirmReceiveSuccess = async ({ merchantId, transactionId, merchantTradeNo }) => { | 1481 | const handleConfirmReceiveSuccess = async ({ merchantId, transactionId, merchantTradeNo }) => { |
| 1454 | - const orders = boughtOrders.value | ||
| 1455 | - const order = orders.find(o => o.id === merchantTradeNo) | ||
| 1456 | - | ||
| 1457 | - if (order) { | ||
| 1458 | try { | 1482 | try { |
| 1459 | // 调用买家查询收货状态接口 | 1483 | // 调用买家查询收货状态接口 |
| 1460 | - const response = await receiptOrderStatusAPI({ order_id: merchantTradeNo }) | 1484 | + const response = await receiptOrderStatusAPI({ order_id: transactionId }) |
| 1461 | 1485 | ||
| 1462 | if (response.code && response.data && response.data.status === 11) { | 1486 | if (response.code && response.data && response.data.status === 11) { |
| 1463 | // 接口返回状态为11,确认收货成功 | 1487 | // 接口返回状态为11,确认收货成功 |
| 1464 | - order.status = 11 // 更新为已完成状态 | 1488 | + await updateOrderStatus(transactionId, 11, {}, 'buy') |
| 1465 | 1489 | ||
| 1466 | Taro.showToast({ | 1490 | Taro.showToast({ |
| 1467 | title: '确认收货成功', | 1491 | title: '确认收货成功', |
| ... | @@ -1474,8 +1498,7 @@ const handleConfirmReceiveSuccess = async ({ merchantId, transactionId, merchant | ... | @@ -1474,8 +1498,7 @@ const handleConfirmReceiveSuccess = async ({ merchantId, transactionId, merchant |
| 1474 | } | 1498 | } |
| 1475 | } catch (error) { | 1499 | } catch (error) { |
| 1476 | // 查询收货状态失败,即使接口调用失败,也更新本地状态 | 1500 | // 查询收货状态失败,即使接口调用失败,也更新本地状态 |
| 1477 | - order.status = 11 | 1501 | + await updateOrderStatus(transactionId, 11, {}, 'buy') |
| 1478 | - } | ||
| 1479 | } | 1502 | } |
| 1480 | } | 1503 | } |
| 1481 | 1504 | ... | ... |
-
Please register or login to post a comment