import { couponDetail, queryCouponDetail } from '../../api/other'; import { BASE_URL } from '../../utils/request'; Page({ data: { baseUrl: BASE_URL, codeType: 'qr', status: 0, id: '', couponInfo: {}, timer: null }, onLoad(options) { this.setData({ id: options.id }); }, onReady() { this.getCouponDetail(); this.startPolling(); }, onUnload() { this.stopPolling(); }, async getCouponDetail() { const res = await couponDetail(this.data.id); if (res.code === 200) { this.updateCouponInfo(res.data); } else { wx.showToast({ title: res.message || '请求失败', icon: 'none', duration: 2000 }); } }, // 轮询接口 startPolling() { this.stopPolling(); this.data.timer = setInterval(() => { this.checkCouponStatus('check'); }, 10000); }, stopPolling() { if (this.data.timer) { clearInterval(this.data.timer); this.data.timer = null; } }, async checkCouponStatus() { const res = await queryCouponDetail(this.data.id, 1); if (res.code === 200) { const { verify_status } = res.data; if (verify_status !== 0) { this.updateCouponInfo(res.data); this.stopPolling(); } } }, updateCouponInfo(data) { let item = { ...data, verify_status_text: data.verify_status == 0 ? '未核销' : data.verify_status == 1 ? '已核销' : '已过期' }; this.setData({ couponInfo: item,status:data.verify_status}); }, handleBack() { this.checkCouponStatus(); }, toggleCode() { this.setData({ codeType: this.data.codeType === 'qr' ? 'bar' : 'qr' }); }, onChange() { this.getCouponDetail(); } });