Przeglądaj źródła

定时查询退款接口

test 7 miesięcy temu
rodzic
commit
e47d4421b4
1 zmienionych plików z 21 dodań i 3 usunięć
  1. 21 3
      src/modules/dj/service/refund.ts

+ 21 - 3
src/modules/dj/service/refund.ts

@@ -74,6 +74,7 @@ export class RefundService extends BaseService {
       const { refundTraceNo } = await this.dispatchService.refund(refund);
       refund.refundTraceNo = refundTraceNo;
       await this.refundEntity.update(refund.id, refund);
+      this.startQueryRefund(refund.id);
       return {
         outOrderNo: refund.outOrderNo,
         orderNo: refund.orderNo,
@@ -89,6 +90,23 @@ export class RefundService extends BaseService {
     }
   }
 
+  async startQueryRefund(id, times = 10) {
+    await new Promise(resolve => {
+      setTimeout(() => { resolve(1) }, 8000);
+    })
+    if (times === 0) {
+      return;
+    }
+    try {
+      const result = await this.queryByApi(id);
+      if (+result.status === 0) {
+        this.startQueryRefund(id, times - 1);
+      }
+    } catch (e) {
+      this.startQueryRefund(id, times - 1);
+    }
+  }
+
   async updateRefundFail(refund, message) {
     refund.remark = message;
     refund.status = 2;
@@ -108,7 +126,7 @@ export class RefundService extends BaseService {
 
   async updateRefundSuccess(data) {
     const refund = await this.refundEntity.findOneBy({ id: data.id });
-    if (!refund || +refund.status === 1) {
+    if (!refund || +refund.status === 1 || +refund.status === 2) {
       // 订单早就已退款成功,无需做任何操作
       return;
     }
@@ -134,7 +152,7 @@ export class RefundService extends BaseService {
     if (!refund) {
       throw new CoolCommException('订单不存在');
     }
-    if (+refund.status === 0 || +refund.status === 2) {
+    if (+refund.status === 0) {
       const { status, message, refundTraceNo, date } =
         await this.dispatchService.queryRefund(refund);
       if (+status === 1) {
@@ -149,7 +167,7 @@ export class RefundService extends BaseService {
       }
       return { status, message };
     } else {
-      return refund.status;
+      return { status: refund.status, message: "订单已有结果,请勿重复查询" }
     }
   }