index.js 1.7 KB

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