123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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({
- status: options.status,
- 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(status) {
- const res = await queryCouponDetail(this.data.id, 1);
- if (res.code === 200 && status) {
- 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 });
- },
- handleBack() {
- this.checkCouponStatus();
- },
- toggleCode() {
- this.setData({
- codeType: this.data.codeType === 'qr' ? 'bar' : 'qr'
- });
- },
- onChange() {
- this.getCouponDetail();
- }
- });
|