123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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,
- loading: true // 增加 loading 状态
- },
- goPage(){
- wx.navigateTo({
- url: '/pages/storeIcon/storeIcon'
- })
- },
- onLoad(options) {
- this.setData({
- id: options.id
- });
- },
- onReady() {
- this.getCouponDetail();
- },
- onUnload() {
- this.stopPolling();
- },
- async getCouponDetail() {
- this.setData({ loading: true }); // 请求前显示加载
- try {
- const res = await couponDetail(this.data.id);
- if (res.code === 200) {
- this.updateCouponInfo(res.data);
- this.startPolling();
- } else {
- wx.showToast({
- title: res.message || '请求失败',
- icon: 'none',
- duration: 2000
- });
- }
- } catch (error) {
- console.log(error)
- } finally{
- this.setData({ loading: false });
- }
- },
- // 轮询接口
- startPolling() {
- this.stopPolling();
- this.data.timer = setTimeout(async () => {
- await this.checkCouponStatus('check');
- this.startPolling();
- }, 10000);
- },
- stopPolling() {
- if (this.data.timer) {
- clearTimeout(this.data.timer);
- this.data.timer = null;
- }
- },
- async checkCouponStatus(type) {
- const res = await queryCouponDetail(this.data.id, 1);
- if (res.code === 200 && type) {
- 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();
- }
- });
|