couponDetail.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { couponDetail, queryCouponDetail } from '../../api/other';
  2. import { BASE_URL } from '../../utils/request';
  3. Page({
  4. data: {
  5. baseUrl: BASE_URL,
  6. codeType: 'qr',
  7. status: 0,
  8. id: '',
  9. couponInfo: {},
  10. timer: null
  11. },
  12. onLoad(options) {
  13. this.setData({
  14. id: options.id
  15. });
  16. },
  17. onReady() {
  18. this.getCouponDetail();
  19. this.startPolling();
  20. },
  21. onUnload() {
  22. this.stopPolling();
  23. },
  24. async getCouponDetail() {
  25. const res = await couponDetail(this.data.id);
  26. if (res.code === 200) {
  27. this.updateCouponInfo(res.data);
  28. } else {
  29. wx.showToast({
  30. title: res.message || '请求失败',
  31. icon: 'none',
  32. duration: 2000
  33. });
  34. }
  35. },
  36. // 轮询接口
  37. startPolling() {
  38. this.stopPolling();
  39. this.data.timer = setInterval(() => {
  40. this.checkCouponStatus('check');
  41. }, 10000);
  42. },
  43. stopPolling() {
  44. if (this.data.timer) {
  45. clearInterval(this.data.timer);
  46. this.data.timer = null;
  47. }
  48. },
  49. async checkCouponStatus() {
  50. const res = await queryCouponDetail(this.data.id, 1);
  51. if (res.code === 200) {
  52. const { verify_status } = res.data;
  53. if (verify_status !== 0) {
  54. this.updateCouponInfo(res.data);
  55. this.stopPolling();
  56. }
  57. }
  58. },
  59. updateCouponInfo(data) {
  60. let item = {
  61. ...data,
  62. verify_status_text:
  63. data.verify_status == 0
  64. ? '未核销'
  65. : data.verify_status == 1
  66. ? '已核销'
  67. : '已过期'
  68. };
  69. this.setData({ couponInfo: item,status:data.verify_status});
  70. },
  71. handleBack() {
  72. this.checkCouponStatus();
  73. },
  74. toggleCode() {
  75. this.setData({
  76. codeType: this.data.codeType === 'qr' ? 'bar' : 'qr'
  77. });
  78. },
  79. onChange() {
  80. this.getCouponDetail();
  81. }
  82. });