修复了停止轮询后 onShow 不恢复的问题,以及支付状态获取异常的问题

This commit is contained in:
2026-07-10 13:52:48 +08:00
parent 4b60ebbc10
commit 6f6dac422f
6 changed files with 89 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
// pages/alipay-webview/alipay-webview.js // pages/alipay-webview/alipay-webview.js
// 支付宝当面付二维码展示页(替代原 web-view 方案,避免 alipay:// scheme 限制) // 支付宝当面付二维码展示页(替代原 web-view 方案,避免 alipay:// scheme 限制)
// 通用化:支持通过 pollUrl 参数指定轮询接口,兼容 code===0订单和 code===200Czjilu类双成功码 // 通用化:支持通过 pollUrl 参数指定轮询接口,兼容 code===0订单和 code===200Czjilu类双成功码
// 支付成功后通过 EventChannel 通知原页面刷新状态
const app = getApp() const app = getApp()
import request from '../../utils/request.js' import request from '../../utils/request.js'
@@ -24,6 +25,10 @@ Page({
// 允许调用方指定轮询接口(需 URL 编码),未指定则用默认订单接口 // 允许调用方指定轮询接口(需 URL 编码),未指定则用默认订单接口
const pollUrl = options.pollUrl ? decodeURIComponent(options.pollUrl) : '/dingdan/fucha' const pollUrl = options.pollUrl ? decodeURIComponent(options.pollUrl) : '/dingdan/fucha'
this.setData({ qrImage, orderId, pollUrl }) this.setData({ qrImage, orderId, pollUrl })
// 获取 opener 事件通道,用于支付成功后通知原页面刷新状态
this.openerEventChannel = this.getOpenerEventChannel ? this.getOpenerEventChannel() : null
this._pollTimer = null
this._hidden = false
if (orderId) { if (orderId) {
this.startPolling() this.startPolling()
} }
@@ -52,14 +57,20 @@ Page({
if (res.statusCode === 200 && res.data && (res.data.code === 0 || res.data.code === 200)) { if (res.statusCode === 200 && res.data && (res.data.code === 0 || res.data.code === 200)) {
this.setData({ isPaid: true, statusText: '支付成功!', showBack: true }) this.setData({ isPaid: true, statusText: '支付成功!', showBack: true })
wx.showToast({ title: '支付成功!', icon: 'success', duration: 1500 }) wx.showToast({ title: '支付成功!', icon: 'success', duration: 1500 })
// 通知原页面刷新状态(原页面通过 wx.navigateTo events 注册 onAlipaySuccess
if (this.openerEventChannel && this.openerEventChannel.emit) {
try {
this.openerEventChannel.emit('onAlipaySuccess', { orderId: this.data.orderId, data: res.data })
} catch (e) {}
}
} else { } else {
this.setData({ statusText: '等待支付...' }) this.setData({ statusText: '等待支付...' })
setTimeout(() => this.startPolling(), 3000) this._pollTimer = setTimeout(() => this.startPolling(), 3000)
} }
}) })
.catch(() => { .catch(() => {
if (!this.data.isPaid) { if (!this.data.isPaid) {
setTimeout(() => this.startPolling(), 3000) this._pollTimer = setTimeout(() => this.startPolling(), 3000)
} }
}) })
}, },
@@ -69,6 +80,20 @@ Page({
}, },
onHide() { onHide() {
this.data.isPaid = true // 切后台时暂停轮询并清理定时器,但不标记为已支付,以便 onShow 恢复
this._hidden = true
if (this._pollTimer) {
clearTimeout(this._pollTimer)
this._pollTimer = null
}
},
onShow() {
// 从后台恢复时,如果未支付且未超时,恢复轮询
if (this._hidden && !this.data.isPaid && this.data.orderId &&
this.data.pollCount < this.data.maxPollCount) {
this._hidden = false
this.startPolling()
}
} }
}) })

View File

@@ -382,8 +382,19 @@ Page({
const app = getApp(); const app = getApp();
app.globalData.alipayQrImage = qrImage; app.globalData.alipayQrImage = qrImage;
const pollUrl = encodeURIComponent('/yonghu/khzffc'); const pollUrl = encodeURIComponent('/yonghu/khzffc');
const self = this;
wx.navigateTo({ wx.navigateTo({
url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}` url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}`,
events: {
onAlipaySuccess: () => {
// 复用微信流成功逻辑:关闭弹窗并刷新考核记录列表
wx.showToast({ title: '申请成功', icon: 'success' });
self.closeApplyModal();
self.fetchAllTags();
self.setData({ recordList: [], recordPage: 1 });
self.fetchMyRecords(true);
}
}
}); });
}, },

View File

@@ -322,8 +322,19 @@ Page({
const app = getApp(); const app = getApp();
app.globalData.alipayQrImage = qrImage; app.globalData.alipayQrImage = qrImage;
const pollUrl = encodeURIComponent('/shangpin/yajinlunxun'); const pollUrl = encodeURIComponent('/shangpin/yajinlunxun');
const self = this;
wx.navigateTo({ wx.navigateTo({
url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}`, url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}`,
events: {
// 支付宝支付成功后,更新押金全局变量并刷新页面
onAlipaySuccess: (res) => {
if (res && res.data && res.data.yajin !== undefined) {
app.globalData.yajin = res.data.yajin;
}
self.loadYajin();
wx.showToast({ title: '履约金缴纳成功', icon: 'success', duration: 2000 });
}
}
}); });
}, },

View File

@@ -548,8 +548,19 @@ onHide() {
if (payType === 'huiyuan') pollUrl = '/shangpin/huiyuanlx'; if (payType === 'huiyuan') pollUrl = '/shangpin/huiyuanlx';
else if (payType === 'jifen') pollUrl = '/shangpin/jifenlunxun'; else if (payType === 'jifen') pollUrl = '/shangpin/jifenlunxun';
const encodedPollUrl = encodeURIComponent(pollUrl); const encodedPollUrl = encodeURIComponent(pollUrl);
const self = this;
wx.navigateTo({ wx.navigateTo({
url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${encodedPollUrl}` url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${encodedPollUrl}`,
events: {
// 支付宝支付成功后,复用微信支付的更新逻辑刷新全局变量与页面
onAlipaySuccess: (res) => {
if (self.updateAfterPayment) {
self.updateAfterPayment(payType, null, res && res.data).then(() => {
wx.showToast({ title: self.getSuccessMessage(payType), icon: 'success', duration: 2000 });
}).catch(() => {});
}
}
}
}); });
}, },

View File

@@ -277,8 +277,24 @@ Page({
const app = getApp(); const app = getApp();
app.globalData.alipayQrImage = qrImage; app.globalData.alipayQrImage = qrImage;
const pollUrl = encodeURIComponent('/shangpin/sjcg'); const pollUrl = encodeURIComponent('/shangpin/sjcg');
const self = this;
wx.navigateTo({ wx.navigateTo({
url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}` url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}`,
events: {
onAlipaySuccess: (res) => {
// 复用微信流成功逻辑:更新全局商家余额并刷新页面显示
try {
if (app.globalData && app.globalData.shangjia) {
const yue = (res && res.data && res.data.yue !== undefined) ? res.data.yue : null;
if (yue !== null && typeof yue === 'number') {
app.globalData.shangjia.sjyue = yue.toFixed(2);
}
}
} catch (e) {}
self.refreshBalance();
self.showStatusMessage('充值成功', '余额已到账', true, '完成');
}
}
}); });
}, },

View File

@@ -94,8 +94,17 @@ Component({
const app = getApp(); const app = getApp();
app.globalData.alipayQrImage = qrImage; app.globalData.alipayQrImage = qrImage;
const pollUrl = encodeURIComponent('/yonghu/fkzffc'); const pollUrl = encodeURIComponent('/yonghu/fkzffc');
const self = this;
wx.navigateTo({ wx.navigateTo({
url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}`, url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}`,
events: {
onAlipaySuccess: () => {
// 复用微信流成功逻辑:通知父组件刷新罚单列表并关闭弹窗
self.triggerEvent('success');
self.close();
wx.showToast({ title: '缴纳成功', icon: 'success' });
}
}
}); });
}, },