|
@@ -1,52 +1,90 @@
|
|
|
-import { couponDetail } from '../../api/other';
|
|
|
-import {BASE_URL} from '../../utils/request';
|
|
|
+import { couponDetail, queryCouponDetail } from '../../api/other';
|
|
|
+import { BASE_URL } from '../../utils/request';
|
|
|
+
|
|
|
Page({
|
|
|
data: {
|
|
|
- baseUrl:BASE_URL,
|
|
|
+ baseUrl: BASE_URL,
|
|
|
codeType: 'qr',
|
|
|
- status:0,
|
|
|
- id:'',
|
|
|
- couponInfo:{}
|
|
|
- },
|
|
|
- goPage(){
|
|
|
- wx.navigateTo({
|
|
|
- url: '/pages/storeIcon/storeIcon'
|
|
|
- })
|
|
|
+ status: 0,
|
|
|
+ id: '',
|
|
|
+ couponInfo: {},
|
|
|
+ timer: null
|
|
|
},
|
|
|
+
|
|
|
onLoad(options) {
|
|
|
this.setData({
|
|
|
- status:options.status,
|
|
|
- id:options.id
|
|
|
- })
|
|
|
+ status: options.status,
|
|
|
+ id: options.id
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ onReady() {
|
|
|
+ this.getCouponDetail();
|
|
|
+ this.startPolling();
|
|
|
},
|
|
|
- onReady(){
|
|
|
- this.getcouponDetail();
|
|
|
+
|
|
|
+ onUnload() {
|
|
|
+ this.stopPolling();
|
|
|
},
|
|
|
- async getcouponDetail(){
|
|
|
+
|
|
|
+ async getCouponDetail() {
|
|
|
const res = await couponDetail(this.data.id);
|
|
|
- if(res.code == 200){
|
|
|
- let item = {
|
|
|
- ...res.data,
|
|
|
- verify_status_text:res.data.verify_status == 0?'未核销':res.data.verify_status == 1?'已核销':'已过期'
|
|
|
- }
|
|
|
- this.setData({
|
|
|
- couponInfo:item
|
|
|
- })
|
|
|
- }else{
|
|
|
+ if (res.code === 200) {
|
|
|
+ this.updateCouponInfo(res.data);
|
|
|
+ } else {
|
|
|
wx.showToast({
|
|
|
title: res.message || '请求失败',
|
|
|
icon: 'none',
|
|
|
duration: 2000
|
|
|
});
|
|
|
}
|
|
|
- console.log(res)
|
|
|
+ },
|
|
|
+
|
|
|
+ // 轮询接口
|
|
|
+ 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();
|
|
|
+
|
|
|
+ onChange() {
|
|
|
+ this.getCouponDetail();
|
|
|
}
|
|
|
-})
|
|
|
+});
|