浏览代码

退款优化

gcz 9 月之前
父节点
当前提交
9d63654dff
共有 1 个文件被更改,包括 87 次插入72 次删除
  1. 87 72
      src/views/order/viewers/index.vue

+ 87 - 72
src/views/order/viewers/index.vue

@@ -708,86 +708,101 @@ export default {
     },
     /**  打开弹窗  */
     openRefundSubmitModel(data, type) {
-        if(data.some(item => item.source === 20 || item.source === 3)) {
-        // 弹出确认框
-        this.$confirm(`请确认在OTA平台中已完成对应订单的退款操作流程,否则修改后无法恢复,请谨慎修改!`, '警告', {
+      // 判断是否需要弹出确认框
+      let needsConfirmation = data.some(item => item.source === 20 || item.source === 3);
+
+      // 弹出确认框的逻辑
+      const showConfirmationDialog = () => {
+        return this.$confirm(`请确认在OTA平台中已完成对应订单的退款操作流程,否则修改后无法恢复,请谨慎修改!`, '警告', {
           confirmButtonText: '确认退款',
           cancelButtonText: '取消',
           type: 'warning'
-        })
-        .then(() => {
-          // 用户点击了确认退款,执行以下逻辑
-
-          // 初始化变量
-          this.refundSubmitSuccess = []
-          this.refundSubmitError = []
-          let list = []
-
-          // 构建订单列表
-          data.forEach((item,index)=>{
-            let flog = false
-            let indexNew = null
-            list.forEach((item1,index1)=>{
-              if(item.orderId == item1.orderId) {
-                flog = true
-                indexNew = index1
-              }
-            })
-            if(!flog){
-              list.push({
-                "orderId": item.orderId,
-                "refundReason": null,
-                "remark": null,
-                "refundAmount": 0,
-                "refundPeople": 0,
-                "ifDirectRefund": 1,
-                "errorRemark": '',
-                "viewerList": [
-                  {
-                    "viewerId": item.id,
-                    "salePrice": item.realPrice ? item.realPrice : 0
-                  }
-                ]
-              })
-            } else {
-              list[indexNew].viewerList.push({
-                "viewerId": item.id,
-                "salePrice": item.realPrice ? item.realPrice : 0
-              })
-            }
-          })
+        });
+      };
 
-          // 计算退款总额和人数
-          let refundAmountAll = 0
-          let refundPeople = 0
-          list.forEach((item,index)=>{
-            if(item.viewerList && item.viewerList.length > 0){
-              let refundAmount = 0
-              refundPeople = refundPeople + item.viewerList.length
-              item.viewerList.forEach((item1,index1)=>{
-                refundAmount = mathM.format(Number(refundAmount) + Number(item1.salePrice),10)
-                refundAmountAll = mathM.format(Number(refundAmountAll) + Number(item1.salePrice),10)
-              })
-              list[index].refundAmount = refundAmount
-            }
-          })
+      // 构建订单列表的逻辑
+      const buildOrderList = (data) => {
+        let list = [];
+        data.forEach(item => {
+          let existingOrder = list.find(order => order.orderId === item.orderId);
+          if (!existingOrder) {
+            list.push({
+              orderId: item.orderId,
+              refundReason: null,
+              remark: null,
+              refundAmount: 0,
+              refundPeople: 0,
+              ifDirectRefund: 1,
+              errorRemark: '',
+              viewerList: [
+                {
+                  viewerId: item.id,
+                  salePrice: item.realPrice ? item.realPrice : 0
+                }
+              ]
+            });
+          } else {
+            existingOrder.viewerList.push({
+              viewerId: item.id,
+              salePrice: item.realPrice ? item.realPrice : 0
+            });
+          }
+        });
+        return list;
+      };
 
-          // 更新表单数据和列表数据
-          this.tkRuleForm['refundAmountAll'] = refundAmountAll
-          this.tkRuleForm['refundPeople'] = refundPeople
-          this.tkSelectList = JSON.parse(JSON.stringify(list))
-          console.log("dfsfdsfsdfds====",this.tkSelectList)
-          this.tkDialogVisible = true
-        })
-        .catch(() => {
-          // 用户点击了取消,给出取消信息
-          this.$message({
-            type: 'info',
-            message: '已取消'
+      // 计算退款总额和人数的逻辑
+      const calculateRefunds = (list) => {
+        let refundAmountAll = 0;
+        let refundPeople = 0;
+
+        list.forEach(item => {
+          let refundAmount = 0;
+          item.viewerList.forEach(viewer => {
+            refundAmount = mathM.format(Number(refundAmount) + Number(viewer.salePrice), 10);
+            refundAmountAll = mathM.format(Number(refundAmountAll) + Number(viewer.salePrice), 10);
           });
+          item.refundAmount = refundAmount;
+          refundPeople += item.viewerList.length;
         });
-        return
+
+        return { refundAmountAll, refundPeople };
+      };
+
+      // 更新表单数据和列表数据的逻辑
+      const updateFormAndList = (refundAmountAll, refundPeople, list) => {
+        this.tkRuleForm['refundAmountAll'] = refundAmountAll;
+        this.tkRuleForm['refundPeople'] = refundPeople;
+        this.tkSelectList = JSON.parse(JSON.stringify(list));
+        console.log("Updated list:", this.tkSelectList);
+        this.tkDialogVisible = true;
+      };
+
+      const executeRefundProcess = () => {
+        this.refundSubmitSuccess = [];
+        this.refundSubmitError = [];
+        let orderList = buildOrderList(data);
+        let { refundAmountAll, refundPeople } = calculateRefunds(orderList);
+        updateFormAndList(refundAmountAll, refundPeople, orderList);
       };
+
+      if (needsConfirmation) {
+        showConfirmationDialog()
+          .then(() => {
+            // 用户点击了确认退款,执行以下逻辑
+            executeRefundProcess();
+          })
+          .catch(() => {
+            // 用户点击了取消,给出取消信息
+            this.$message({
+              type: 'info',
+              message: '已取消'
+            });
+          });
+      } else {
+        // 不需要确认退款,直接执行退款流程
+        executeRefundProcess();
+      }
     },
     deleteSelect(index,row){
       let refundAmountAll = 0