lrf 2 years ago
parent
commit
68396c4397
1 changed files with 10 additions and 7 deletions
  1. 10 7
      app/service/trade/afterSale.js

+ 10 - 7
app/service/trade/afterSale.js

@@ -44,13 +44,14 @@ class AfterSaleService extends CrudService {
 
 
     // 修改数据
     // 修改数据
     try {
     try {
+      let refundInfo;
       this.tran.update('AfterSale', entity._id, update);
       this.tran.update('AfterSale', entity._id, update);
       await this.tran.run();
       await this.tran.run();
       const type = _.get(entity, 'type');
       const type = _.get(entity, 'type');
       const status = _.get(update, 'status');
       const status = _.get(update, 'status');
       // 同意退款/退货,则直接进行退款,然后再将状态修改为已退款
       // 同意退款/退货,则直接进行退款,然后再将状态修改为已退款
       if (type !== '2' && (status === '1' || status === '2')) {
       if (type !== '2' && (status === '1' || status === '2')) {
-        await this.toRefund({ afterSale_id: entity._id, goods_id: _.get(entity, 'goods._id') }, this.tran);
+        refundInfo = await this.toRefund({ afterSale_id: entity._id, goods_id: _.get(entity, 'goods._id') }, this.tran);
       }
       }
       // 2022-10-17 需求8:标记处理售后的人
       // 2022-10-17 需求8:标记处理售后的人
       if (entity.status === '0' && update.status !== '0') {
       if (entity.status === '0' && update.status !== '0') {
@@ -60,6 +61,11 @@ class AfterSaleService extends CrudService {
         this.tran.update('AfterSale', entity._id, { deal_person: admin._id });
         this.tran.update('AfterSale', entity._id, { deal_person: admin._id });
       }
       }
       await this.tran.run();
       await this.tran.run();
+      if (refundInfo) {
+        // 退款请求, 将请求退款滞后,所有的信息处理完之后,再去退款,避免退款成功但是数据处理失败的问题
+        const res = await this.ctx.service.trade.pay.refund(refundInfo);
+        if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
+      }
     } catch (error) {
     } catch (error) {
       console.error(error);
       console.error(error);
       await this.tran.rollback();
       await this.tran.rollback();
@@ -79,6 +85,7 @@ class AfterSaleService extends CrudService {
    * @param {Transaction} tran 事务的实例
    * @param {Transaction} tran 事务的实例
    */
    */
   async toRefund({ afterSale_id, goods_id }, tran) {
   async toRefund({ afterSale_id, goods_id }, tran) {
+    let refundInfo = {};
     const data = await this.model.findById(afterSale_id);
     const data = await this.model.findById(afterSale_id);
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到售后信息');
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到售后信息');
     const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
     const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
@@ -109,12 +116,7 @@ class AfterSaleService extends CrudService {
     // 组成退款单号
     // 组成退款单号
     const str = this.ctx.service.util.trade.createNonceStr();
     const str = this.ctx.service.util.trade.createNonceStr();
     const out_refund_no = `${order_no}-r-${str}`;
     const out_refund_no = `${order_no}-r-${str}`;
-    const obj = { reason, money: refundMoney, order_no, out_refund_no };
-    // 退款请求
-    if (refundMoney > 0) {
-      const res = await this.ctx.service.trade.pay.refund(obj);
-      if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
-    }
+    refundInfo = { reason, money: refundMoney, order_no, out_refund_no };
     if (data.status === '1') {
     if (data.status === '1') {
       tran.update('AfterSale', afterSale_id, { status: '-1', end_time: moment().format('YYYY-MM-DD HH:mm:ss') });
       tran.update('AfterSale', afterSale_id, { status: '-1', end_time: moment().format('YYYY-MM-DD HH:mm:ss') });
     }
     }
@@ -135,6 +137,7 @@ class AfterSaleService extends CrudService {
     // 退积分,退多钱就退多少积分
     // 退积分,退多钱就退多少积分
     const { customer } = orderDetail;
     const { customer } = orderDetail;
     await this.ctx.service.user.point.refundPoint({ customer, money: refundMoney, source_id: afterSale_id, source: '-1' }, tran);
     await this.ctx.service.user.point.refundPoint({ customer, money: refundMoney, source_id: afterSale_id, source: '-1' }, tran);
+    return refundInfo;
   }
   }
 
 
   /**
   /**