From dca5f1d18e790f4bfa8070410c220922cbd5457d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=96=B5=E5=92=AA?= <15233722133@163.com> Date: Sat, 8 Aug 2026 17:18:02 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=94=AE=E5=90=8E=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/order/aftersale/apply.vue | 66 +++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/pages/order/aftersale/apply.vue b/pages/order/aftersale/apply.vue index d7a0acf..6f087a8 100644 --- a/pages/order/aftersale/apply.vue +++ b/pages/order/aftersale/apply.vue @@ -175,12 +175,39 @@ }); const rules = reactive({}); + // 整数安全求和(避免浮点误差,兼容 iOS / 安卓 10 / 鸿蒙 / 低版本 Android WebView): + // 以分为单位的 payPrice 理论上应是整数,但接口返回偶尔会是浮点数(比如 100.00)。 + // 用 Math.round(Number(x)*100)/100 做归一化后再累加,保证 0.1+0.2===0.3 的情况不出错。 + function safeAddInt(...nums) { + let total = 0; + for (let i = 0; i < nums.length; i++) { + const raw = Number(nums[i] ?? 0) || 0; + const normalized = Math.round(raw * 100) / 100; + total = Math.round((total + normalized) * 100) / 100; + } + return Math.round(total); // 最终一定是整数分 + } + + // 按用户要求:申请售后展示/计算只处理 afterSaleStatus===0 的商品;其他状态的商品不展示、不算退款金额、不提交到接口。 + // 单品售后(itemId>0)保持原逻辑,因为单品是用户从某个具体商品点进来的,一定是这一个。 + // 整单售后(itemId===0): + // ① state.orderItems = data.items 里 afterSaleStatus===0 的那些; + // ② state.payPrice = 过滤后的商品 payPrice 整数累加(不用外层 order.payPrice,避免把其他状态的商品金额算进去); + // ③ submit() 里 targetItems 直接用 state.orderItems(已过滤,自然就不会包含非 0 的商品)。 + function filterAfterSaleItems(items) { + if (!Array.isArray(items)) return []; + return items.filter((it) => { + const v = it?.afterSaleStatus === undefined ? it?.after_sale_status : it.afterSaleStatus; + return Number(v) === 0; + }); + } + // 提交表单:接口 POST /trade/after-sale/create // Body 直接就是数组:AppAfterSaleCreateReqVO[] // - 单品售后(state.itemId > 0):数组只含 1 项,orderItemId = 当前商品项 id - // - 整单售后(state.itemId = 0):数组包含订单全部商品,每一项 orderItemId = 对应商品项 id,refundPrice = 该商品项自己的 payPrice + // - 整单售后(state.itemId = 0):数组包含【仅 afterSaleStatus===0】的商品,每一项 orderItemId=对应商品项 id,refundPrice=对应商品项自己的 payPrice // 每个 VO 字段:orderItemId(必填)、way(1仅退款/2退货退款 必填)、refundPrice(单位分 必填) - // applyReason(必填 string)、applyDescription(选填)、applyPicUrls(选填 逗号拼接字符串) + // applyReason(必填 string)、applyDescription(选填)、applyPicUrls(选填 数组或null) async function submit() { if (!formData.way) { uni.showToast({ title: '请选择售后类型', icon: 'none' }); @@ -212,8 +239,10 @@ // 单品售后:只传当前选择的这一个订单项 targetItems = [state.item]; } else { - // 整单售后:传所有商品项(orderItems 就是整个订单的 items 列表) + // 整单售后:只传 afterSaleStatus===0 的商品(state.orderItems 已经在 onLoad 里过滤过一遍) targetItems = Array.isArray(state.orderItems) ? state.orderItems.slice() : []; + // 保险:再过滤一次 afterSaleStatus===0,避免后续代码改动导致 orderItems 混入非 0 状态的商品 + targetItems = filterAfterSaleItems(targetItems); } if (!targetItems.length) { uni.showToast({ title: '缺少可申请售后的商品', icon: 'none' }); @@ -226,8 +255,8 @@ .map((it) => ({ orderItemId: it.id, way, - // 退款金额:按订单项自己的 payPrice(单品=选中项payPrice,整单每项=对应项payPrice),安全兜底转 Number - refundPrice: Number(it.payPrice ?? 0) || 0, + // 退款金额:按订单项自己的 payPrice;统一经 safeAddInt 归一化后取整数,避免浮点 + refundPrice: safeAddInt(it.payPrice ?? 0), applyReason, applyDescription, applyPicUrls, @@ -344,19 +373,32 @@ state.order = data; // 按itemId精确查找订单项(单品入口),否则默认取第1项(整单入口仅用于商品卡片展示) + // 注意:无论单品还是整单,都只展示 + 计算 + 提交 afterSaleStatus===0 的商品。 + const allItems = Array.isArray(data.items) ? data.items : []; + const applyableItems = filterAfterSaleItems(allItems); // 只留 afterSaleStatus===0 if (state.itemId > 0) { - state.item = data.items.find((item) => item.id === state.itemId) || {}; - state.orderItems = state.item?.id ? [state.item] : []; + // 单品售后:按用户点进来的 itemId 精确查找(理论上应该已经是 afterSaleStatus===0 的商品,不是的话就为空) + const picked = applyableItems.find((it) => Number(it.id) === Number(state.itemId)) || null; + state.item = picked || {}; + state.orderItems = picked ? [picked] : []; } else { - state.item = data.items?.[0] || {}; - state.orderItems = Array.isArray(data.items) ? data.items : []; + // 整单售后:仅展示 afterSaleStatus===0 的商品 + state.item = applyableItems[0] || data.items?.[0] || {}; + state.orderItems = applyableItems; } - // 退款金额:单品用订单项的payPrice,整单使用订单外层的payPrice(含randomDiscountPrice等优惠抵扣后的值) + // 退款金额: + // - 单品售后(itemId>0):用订单项的 payPrice(精确匹配到的那一件) + // - 整单售后(itemId===0):使用【过滤后 afterSaleStatus===0 的商品的 payPrice 整数累加】 + // 不再使用外层 order.payPrice,避免把其他状态的商品金额(已申请/已取消/已完成售后的商品)算进来导致误差。 if (state.itemId > 0 && state.item?.id) { - state.payPrice = state.item.payPrice; + state.payPrice = safeAddInt(state.item.payPrice ?? 0); } else { - state.payPrice = state.order.payPrice; + if (!applyableItems.length) { + state.payPrice = 0; + } else { + state.payPrice = safeAddInt(...applyableItems.map((it) => it.payPrice ?? 0)); + } } // 设置选项