From b1537ea43a45e2a90cf5b3f16aa58779acd3aa23 Mon Sep 17 00:00:00 2001 From: chen wei bo <1025839511@qq.com> Date: Tue, 26 Aug 2025 16:41:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scripts/chat18x/payment/PaymentApi.ts | 26 ++++---------- .../Scripts/chat18x/payment/PaymentPoller.ts | 12 +++---- .../Scripts/chat18x/payment/PaymentService.ts | 35 ++++++------------- 3 files changed, 24 insertions(+), 49 deletions(-) diff --git a/assets/Scripts/chat18x/payment/PaymentApi.ts b/assets/Scripts/chat18x/payment/PaymentApi.ts index 5a3b9fdb..1af6feb9 100644 --- a/assets/Scripts/chat18x/payment/PaymentApi.ts +++ b/assets/Scripts/chat18x/payment/PaymentApi.ts @@ -5,7 +5,6 @@ import { ApiClient } from "../network/client/ApiClient"; import type { Endpoint } from "../network/client/endpoints"; import type { ApiResponse } from "../network/client/types"; -import { OrderStatus, CreateOrderReq, CreateOrderData, QueryOrderData } from "./types"; import proto from 'db://assets/Scripts/proto/proto.pb.js'; export class PaymentApi { @@ -14,9 +13,9 @@ export class PaymentApi { private constructor(private api = ApiClient.I) {} // 下单 - createOrder(req: CreateOrderReq): Promise> { - let epData: Endpoint = { - path: "pay/create", + createOrder(req: proto.cs.ICSCreateOrderReq): Promise> { + let epData: Endpoint = { + path: "api/logic/createOrder", method: "POST", codec: "json", needsAuth: true, @@ -25,24 +24,13 @@ export class PaymentApi { } // 查询订单 - queryOrder(orderId: string): Promise> { - let epData: Endpoint<{ orderId: string }, { orderId: string; status: OrderStatus; paidAt?: number; failureReason?: string; }> = { - path: "pay/query", + queryOrder(req: proto.cs.ICSQueryOrderReq): Promise> { + let epData: Endpoint = { + path: "api/logic/queryOrder", method: "POST", codec: "json", needsAuth: true, }; - return this.api.call(epData, { orderId }); - } - - // 取消订单 - cancelOrder(orderId: string): Promise> { - let epData: Endpoint<{ orderId: string }, { ok: boolean; }> = { - path: "pay/cancel", - method: "POST", - codec: "json", - needsAuth: true, - }; - return this.api.call(epData, { orderId }); + return this.api.call(epData, req); } } diff --git a/assets/Scripts/chat18x/payment/PaymentPoller.ts b/assets/Scripts/chat18x/payment/PaymentPoller.ts index 1da29a15..c5c1f150 100644 --- a/assets/Scripts/chat18x/payment/PaymentPoller.ts +++ b/assets/Scripts/chat18x/payment/PaymentPoller.ts @@ -4,8 +4,7 @@ */ import { PaymentApi } from "./PaymentApi"; -import type { QueryOrderData } from "./types"; -import { ApiCode } from "../network/client/types"; +import proto from 'db://assets/Scripts/proto/proto.pb.js'; export interface PollOptions { maxDurationMs: number; // 轮询总时长(例如 2分钟) @@ -21,16 +20,17 @@ export class PaymentPoller { cancel() { this.cancelled = true; } // 轮询订单 - async poll(orderId: string, opt: PollOptions, onTick?: (d: QueryOrderData) => void): Promise { + async poll(orderId: string, opt: PollOptions, onTick?: (d: proto.cs.ICSQueryOrderRes) => void): Promise { const t0 = Date.now(); let interval = opt.startIntervalMs; while (!this.cancelled) { // 查询 - const r = await PaymentApi.I.queryOrder(orderId); - if (r.code === ApiCode.OK && r.data) { + let reqData = {orderId: orderId}; + const r = await PaymentApi.I.queryOrder(reqData); + if (r.code === proto.cs.EnmRetCode.SUCCESS && r.data) { // 回调 onTick?.(r.data); - if (["SUCCESS", "FAILED", "CANCELED", "EXPIRED"].indexOf(r.data.status) !== -1) { + if ([-1, 0].indexOf(r.data.status) !== -1) { return r.data; } } diff --git a/assets/Scripts/chat18x/payment/PaymentService.ts b/assets/Scripts/chat18x/payment/PaymentService.ts index 58c185a3..2328caec 100644 --- a/assets/Scripts/chat18x/payment/PaymentService.ts +++ b/assets/Scripts/chat18x/payment/PaymentService.ts @@ -7,8 +7,8 @@ import { PaymentApi } from "./PaymentApi"; import { PaymentOpenerFactory, type OpenStrategy } from "./PaymentOpener"; import { PendingOrderRepo } from "./PendingOrderRepo"; import PaymentPoller from "./PaymentPoller"; -import type { CreateOrderReq, PaymentResult, PendingOrder, OrderStatus } from "./types"; -import { ApiCode } from "../network/client/types"; +import type { PaymentResult, PendingOrder, OrderStatus } from "./types"; +import proto from 'db://assets/Scripts/proto/proto.pb.js'; export const PaymentEvents = { Started: "pay-started", // { orderId } @@ -32,13 +32,13 @@ export class PaymentService { } /** 发起支付:创建订单→打开URL→回到App后开始轮询 */ - async startPayment(req: CreateOrderReq, open: OpenStrategy = "system"): Promise { + async startPayment(req: proto.cs.ICSCreateOrderReq, open: OpenStrategy = "system"): Promise { // 下单 const create = await PaymentApi.I.createOrder(req); - if (create.code !== ApiCode.OK || !create.data) { - return { orderId: "", status: "FAILED", message: create.msg || "create order failed" }; + if (create.code !== proto.cs.EnmRetCode.SUCCESS || !create.data) { + return { status: -1, retCode: -1 }; } - const { orderId, payUrl, expireAt } = create.data; + const { orderId, payUrl } = create.data; this.bus.emit(PaymentEvents.Started, { orderId }); // 存为未决订单(用于异常恢复) @@ -62,19 +62,10 @@ export class PaymentService { jitter: true, }, (d) => this.bus.emit(PaymentEvents.PollTick, { orderId, status: d.status })); - // 产出结果 - let res: PaymentResult; - if (!data) { - res = { orderId, status: "EXPIRED", message: "poll timeout" }; - } else if (data.status === "SUCCESS") { - res = { orderId, status: "SUCCESS" }; - } else { - res = { orderId, status: data.status as OrderStatus, message: data.failureReason }; - } - PendingOrderRepo.instance.remove(orderId); - this.bus.emit(PaymentEvents.Finished, res); - return res; + // 返回结果 + this.bus.emit(PaymentEvents.Finished, data); + return data; } /** App 回到前台后恢复未完成订单的轮询(在 onShow 时调用) */ @@ -105,13 +96,9 @@ export class PaymentService { // 轮询最新且未过期的订单 const r = await this.startPollingOnly(o.orderId); - if (r && r.status !== "PENDING" && r.status !== "CREATED") { + if (r && r.status !== 1) { PendingOrderRepo.instance.remove(o.orderId); - this.bus.emit(PaymentEvents.Finished, { - orderId: o.orderId, - status: r.status as OrderStatus, - message: r.failureReason, - } as PaymentResult); + this.bus.emit(PaymentEvents.Finished, r); } // r === null(轮询超时)时保留在仓库,等待下一次 resume 再查 }