coupon.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { couponListApi } from '../../api/other';
  2. Page({
  3. data: {
  4. couponList: [],
  5. page: 1,
  6. pageSize: 10,
  7. loadingMore: false,
  8. noMore: false,
  9. refreshing: false
  10. },
  11. onLoad() {
  12. this.loadData(true);
  13. },
  14. async loadData(isRefresh = false) {
  15. if (this.data.loadingMore) return;
  16. let page = isRefresh ? 1 : this.data.page;
  17. this.setData({ loadingMore: true });
  18. try {
  19. const res = await couponListApi({ page, pageSize: this.data.pageSize });
  20. const statusImgMap = {
  21. 0: '/static/image/whx.png', // 未核销
  22. 1: '/static/image/yhx.png' // 已核销
  23. };
  24. const newList = res.data.list || [];
  25. let allList = isRefresh ? newList : [...this.data.couponList, ...newList];
  26. allList = allList.map(item => ({
  27. ...item,
  28. statusImg: statusImgMap[item.verify_status] || '/static/image/whx.png',
  29. }));
  30. this.setData({
  31. couponList: allList,
  32. loadingMore: false,
  33. page: page + 1,
  34. noMore: allList.length >= res.data.total,
  35. refreshing: false
  36. });
  37. } catch (err) {
  38. console.error(err);
  39. wx.showToast({ title: '加载失败', icon: 'none' });
  40. this.setData({
  41. loadingMore: false,
  42. refreshing: false
  43. });
  44. }
  45. },
  46. // 上拉触底
  47. onReachBottom() {
  48. if (this.data.noMore) return;
  49. this.loadData();
  50. },
  51. // 下拉刷新
  52. onPullDownRefresh() {
  53. if (this.data.refreshing) return;
  54. this.setData({ refreshing: true, noMore: false, page: 1 });
  55. this.loadData(true);
  56. },
  57. goPage(e) {
  58. const coupon = e.currentTarget.dataset.item;
  59. wx.navigateTo({
  60. url: `/pages/couponDetail/couponDetail?id=${coupon.id}&status=${coupon.verify_status}`
  61. });
  62. }
  63. });