美文网首页vue
关于订单支付结果轮训查询接口

关于订单支付结果轮训查询接口

作者: Morbid_D | 来源:发表于2025-04-25 15:17 被阅读0次

import { ref } from "vue";
import { ElMessage } from "element-plus";

const pageClosing = ref(false);

// 监听页面关闭事件
window.addEventListener("beforeunload", () => {
pageClosing.value = true;
});

// 维护当前的轮询控制器
let currentPollingAbortController: { cancel: () => void } | null = null;

// 轮询结果接口
interface PollingResult<T> {
result?: T;
canceled: boolean;
}

const changePayState = async (orderIdForPaying: any) => {
const pollingInterval = 3000; // 设置轮询间隔(毫秒)
let data = {
orderIdForPaying: orderIdForPaying,
};

// 启动新轮询前,取消之前的轮询
if (currentPollingAbortController) {
    currentPollingAbortController.cancel();
}

// 定义轮询函数
const pollingFunction = async () => {
    try {
        const result = await authApi.userCheckPayingOrder(data);
        return result;
    } catch (error) {
        return { result: null, canceled: false };
    }
};

// 新建轮询
const pollingAbort = createPollingAbort();
currentPollingAbortController = pollingAbort;

const pollingResult: any = await pollUntilResult(
    pollingFunction,
    pollingInterval,
    pollingAbort
);

console.log("pollingResult", pollingResult);

if (pollingResult.canceled) {
    console.log("轮询被取消或页面关闭");
} else {
    console.log("获取到结果:", pollingResult.result);
    if (pollingResult.result.status === "01") {
        console.log("支付成功");
        clearInterval(timer);
        ElMessage.success("支付成功");
        userBalance();
        onDialogClosed();
    }
}

};

// 定义轮询函数,增加 cancel 支持
async function pollUntilResult<T>(
pollingFunction: () => Promise<T | null>,
pollingInterval: number,
abortController: { isCanceled: () => boolean }
): Promise<PollingResult<T>> {
return new Promise((resolve) => {
const poll = async () => {
if (abortController.isCanceled() || pageClosing.value) {
resolve({ canceled: true });
return;
}

        const result: any = await pollingFunction();
        console.log("result1111", result);

        if (result.status === "01") {
            resolve({ result, canceled: false });
        } else {
            setTimeout(poll, pollingInterval);
        }
    };

    poll();
});

}

// 创建一个简单的取消控制器
function createPollingAbort() {
let canceled = false;

return {
    cancel() {
        canceled = true;
    },
    isCanceled() {
        return canceled;
    },
};

}
onUnmounted(() => {
pageClosing.value = true;
clearInterval(timer.value);
});
//多次调用轮训之后轮训最新的
根据实际情况调用changePayState方法

相关文章

  • Java支付宝订单查询

    电脑网站支付成功后可通过支付宝接口主动查询订单结果 更多精彩 更多技术博客,请移步 asing1elife's b...

  • 对接杭州银行支付的总结(一)

    本文主要整理杭州银行的接口示例报文。 支付下单接口 支付回调通知 退款接口 查询支付结果接口 关单接口 对账接口 ...

  • 网络接口返回值出错

    由于 余额充值接口 和 订单 ali支付接口 是同一个, 导致 返回结果 responseObject 其实...

  • python 支付-查询-退款-退款查询-调取excle每行数据

    单独调取excle每行数据对支付接口进行测试: 单独调取excle每行数据对订单查询接口进行测试: 单独调取exc...

  • 订单查询改造

    情景: 订单量的日益增长,以及他复杂的查询纬度导致后台管理以及用户端查询订单接口耗时严重,遂对订单查询相关接口进行...

  • PC端第三方支付接口

    applica.yml文件配置微信、支付宝支付接口调用参数; 支付宝支付 第一步:生成订单,保存订单信息(除了支付...

  • TrPay支付sdk,图灵支付sdk订单查询接口,详细说明

    订单查询接口 统一请求网关地址 请求方式 公共入参 订单查询业务参数 响应格式 响应报文说明 目前接口只返回jso...

  • 订单成功页面

    前端,src/views/OrderSuccess.vue 后端,根据订单Id去查询订单信息接口

  • 支付流程整体设计

    一、主要思想 无需支付校验,直接查询支付结果 支付流程整体设计,兼容支付宝、微信的支付设计 二、关于支付校验 支付...

  • 浅析微信支付:查询订单和关闭订单

    本文是【浅析微信支付】系列文章的第七篇,主要讲解微信商户平台的订单查询和关闭接口的使用。 浅析微信支付系列已经更新...

网友评论

    本文标题:关于订单支付结果轮训查询接口

    本文链接:https://www.haomeiwen.com/subject/khsobjtx.html