小程序 - 旧请求 组件计时器
旧的请求 组件计时器的回调。
// ============ 支付结果轮询 ============ /** * 查询微信预支付状态入口 * data == 1 支付处理中(继续轮询) * data == 2 支付成功 * 其他值视为失败 */ gePrePayStatus: function (prePayId) { const that = this; this._prePayId = prePayId; this.queryPrePayStatus(prePayId); }, /** * 单次状态查询,根据结果决定是否继续轮询 */ queryPrePayStatus: function (prePayId) { const that = this; service.gePrePayStatus({ prePayId: prePayId || that._prePayId, callback: (resp, error) => { // 先释放"查询中"锁,保证定时器下一轮能继续发请求 that._prePayQuerying = false; if (error) { that.stopPrePayTimer(); wx.showToast({ title: error, duration: 3000, icon: 'none', }); return; } const data = resp.data; if (data == 1) { // 支付处理中 → 启动/继续轮询 that.startPrePayTimer(that._prePayId); } else if (data == 2) { // 支付成功 that.stopPrePayTimer(); that.triggerEvent('onBalanceRecharge', null); } else { // 其他状态都视为失败 that.stopPrePayTimer(); wx.showToast({ title: '启动失败', duration: 3000, icon: 'none', }); } } }); }, /** * 启动轮询定时器:每 3 秒查一次,已有定时器则复用 */ startPrePayTimer: function (prePayId) { const that = this; if (this._prePayTimer) return; this._prePayId = prePayId; this._prePayTimer = setInterval(function () { if (that._prePayQuerying) return; // 上一次未返回,跳过本轮 that._prePayQuerying = true; that.queryPrePayStatus(that._prePayId); }, 3000); }, /** * 停止轮询定时器 */ stopPrePayTimer: function () { if (this._prePayTimer) { clearInterval(this._prePayTimer); this._prePayTimer = null; } this._prePayQuerying = false; this._prePayId = null; },
如需转载原创文章,请标注原文地址,版权所有!
浙公网安备 33010602011771号