couponDetail.js 1.8 KB

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