index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. that.setData({loading:true,loadTitle:'正在搜索'});
  57. getCoupons({ page: this.data.page, limit: this.data.limit }).then(res=>{
  58. var list=res.data,loadend=list.length < that.data.limit;
  59. var couponsList = app.SplitArray(list, that.data.couponsList);
  60. that.setData({
  61. loading: false,
  62. couponsList: couponsList,
  63. page:that.data.page+1,
  64. loadend: loadend,
  65. loadTitle: loadend ? '已全部加载' : '加载更多'
  66. });
  67. }).catch(err=>{
  68. that.setData({ loading: false, loadTitle: '加载更多' });
  69. });
  70. },
  71. /**
  72. * 页面上拉触底事件的处理函数
  73. */
  74. onReachBottom: function () {
  75. this.getUseCoupons();
  76. },
  77. })