coupon.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. used: '/static/image/yhx.png', // 已核销
  22. unused: '/static/image/whx.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. // typeText: item.type === 1 ? '菜品券' : '通用券',
  29. // statusImg: statusImgMap[item.status] || '/static/image/whx.png',
  30. // expireTime: item.expire_time || '--'
  31. // }));
  32. this.setData({
  33. couponList: allList,
  34. loadingMore: false,
  35. page: page + 1,
  36. noMore: allList.length >= res.data.total,
  37. refreshing: false
  38. });
  39. } catch (err) {
  40. console.error(err);
  41. wx.showToast({ title: '加载失败', icon: 'none' });
  42. this.setData({
  43. loadingMore: false,
  44. refreshing: false
  45. });
  46. }
  47. },
  48. // 上拉触底
  49. onReachBottom() {
  50. if (this.data.noMore) return;
  51. this.loadData();
  52. },
  53. // 下拉刷新
  54. onPullDownRefresh() {
  55. if (this.data.refreshing) return;
  56. this.setData({ refreshing: true, noMore: false, page: 1 });
  57. this.loadData(true);
  58. },
  59. goPage(e) {
  60. const coupon = e.currentTarget.dataset.item;
  61. wx.navigateTo({
  62. url: `/pages/coupon-detail/coupon-detail?id=${coupon.id}`
  63. });
  64. }
  65. });