index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // pages/coupon-list/index.js
  2. import {
  3. getCouponReceive
  4. } from '../../api/user.js';
  5. import {
  6. getCoupons
  7. } from '../../api/api.js';
  8. const app = getApp();
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. parameter: {
  15. 'navbar': '1',
  16. 'return': '1',
  17. 'title': '领取优惠券',
  18. 'color': false
  19. },
  20. couponsList: [],
  21. loading: false,
  22. loadend: false,
  23. page: 1,
  24. limit: 20,
  25. },
  26. /**
  27. * 授权回调
  28. */
  29. onLoadFun: function () {
  30. this.getUseCoupons();
  31. },
  32. /** 领取优惠券 */
  33. getCoupon: function (e) {
  34. var that = this;
  35. var id = e.currentTarget.dataset.id;
  36. var index = e.currentTarget.dataset.index;
  37. var list = that.data.couponsList;
  38. getCouponReceive({
  39. couponId: id
  40. }).then(function (res) {
  41. list[index].is_use = true;
  42. that.setData({
  43. couponsList: list
  44. });
  45. app.Tips({
  46. title: '领取成功'
  47. });
  48. }, function (res) {
  49. return app.Tips({
  50. title: res.msg
  51. });
  52. });
  53. },
  54. /**
  55. * 生命周期函数--监听页面加载
  56. */
  57. onLoad: function (options) {
  58. },
  59. /**
  60. * 获取领取优惠券列表
  61. */
  62. getUseCoupons: function () {
  63. var that = this
  64. if (this.data.loadend) return false;
  65. if (this.data.loading) return false;
  66. that.setData({
  67. loading: true,
  68. loadTitle: '正在搜索'
  69. });
  70. getCoupons({
  71. page: this.data.page,
  72. limit: this.data.limit
  73. }).then(res => {
  74. var list = res.data,
  75. loadend = list.length < that.data.limit;
  76. var couponsList = app.SplitArray(list, that.data.couponsList);
  77. that.setData({
  78. loading: false,
  79. couponsList: couponsList,
  80. page: that.data.page + 1,
  81. loadend: loadend,
  82. loadTitle: loadend ? '已全部加载' : '加载更多'
  83. });
  84. }).catch(err => {
  85. that.setData({
  86. loading: false,
  87. loadTitle: '加载更多'
  88. });
  89. });
  90. },
  91. /**
  92. * 页面上拉触底事件的处理函数
  93. */
  94. onReachBottom: function () {
  95. this.getUseCoupons();
  96. }
  97. })