coupon.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. couponList: [],
  7. page: 1,
  8. limit: 10,
  9. count: 0,
  10. scrollTop: 0,
  11. showPage: false
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. this.getCouponList();
  18. },
  19. /**
  20. * 生命周期函数--监听页面初次渲染完成
  21. */
  22. onReady: function () {
  23. },
  24. /**
  25. * 生命周期函数--监听页面显示
  26. */
  27. onShow: function () {
  28. },
  29. /**
  30. * 生命周期函数--监听页面隐藏
  31. */
  32. onHide: function () {
  33. },
  34. /**
  35. * 生命周期函数--监听页面卸载
  36. */
  37. onUnload: function () {
  38. },
  39. /**
  40. * 页面相关事件处理函数--监听用户下拉动作
  41. */
  42. onPullDownRefresh: function () {
  43. },
  44. /**
  45. * 页面上拉触底事件的处理函数
  46. */
  47. onReachBottom: function () {
  48. },
  49. /**
  50. * 用户点击右上角分享
  51. */
  52. onShareAppMessage: function () {
  53. },
  54. getCouponList: function () {
  55. let that = this;
  56. that.setData({
  57. scrollTop: 0,
  58. showPage: false,
  59. couponList: []
  60. });
  61. // 页面渲染完成
  62. wx.showToast({
  63. title: '加载中...',
  64. icon: 'loading',
  65. duration: 2000
  66. });
  67. util.request(api.CouponList, {
  68. page: that.data.page,
  69. limit: that.data.limit
  70. }).then(function (res) {
  71. if (res.errno === 0) {
  72. that.setData({
  73. scrollTop: 0,
  74. couponList: res.data.list,
  75. showPage: true,
  76. count: res.data.total
  77. });
  78. }
  79. wx.hideToast();
  80. });
  81. },
  82. getCoupon(e) {
  83. if (!app.globalData.hasLogin) {
  84. wx.navigateTo({
  85. url: "/pages/auth/login/login"
  86. });
  87. return;
  88. }
  89. let couponId = e.currentTarget.dataset.index
  90. util.request(api.CouponReceive, {
  91. couponId: couponId
  92. }, 'POST').then(res => {
  93. if (res.errno === 0) {
  94. wx.showToast({
  95. title: "领取成功"
  96. })
  97. }
  98. else {
  99. util.showErrorToast(res.errmsg);
  100. }
  101. })
  102. },
  103. nextPage: function (event) {
  104. var that = this;
  105. if (this.data.page > that.data.count / that.data.limit) {
  106. return true;
  107. }
  108. that.setData({
  109. page: that.data.page + 1
  110. });
  111. this.getCouponList();
  112. },
  113. prevPage: function (event) {
  114. if (this.data.page <= 1) {
  115. return false;
  116. }
  117. var that = this;
  118. that.setData({
  119. page: that.data.page - 1
  120. });
  121. this.getCouponList();
  122. }
  123. })