coupon.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  88. let couponId = e.currentTarget.dataset.index
  89. util.request(api.CouponReceive, {
  90. couponId: couponId
  91. }, 'POST').then(res => {
  92. if (res.errno === 0) {
  93. wx.showToast({
  94. title: "领取成功"
  95. })
  96. }
  97. else {
  98. util.showErrorToast(res.errmsg);
  99. }
  100. })
  101. },
  102. nextPage: function (event) {
  103. var that = this;
  104. if (this.data.page > that.data.count / that.data.limit) {
  105. return true;
  106. }
  107. that.setData({
  108. page: that.data.page + 1
  109. });
  110. this.getCouponList();
  111. },
  112. prevPage: function (event) {
  113. if (this.data.page <= 1) {
  114. return false;
  115. }
  116. var that = this;
  117. that.setData({
  118. page: that.data.page - 1
  119. });
  120. this.getCouponList();
  121. }
  122. })