1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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();
- }
- });
|