couponList.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. code: '',
  8. status: 0,
  9. page: 1,
  10. limit: 10,
  11. count: 0,
  12. scrollTop: 0,
  13. showPage: false
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function(options) {
  19. this.getCouponList();
  20. },
  21. /**
  22. * 生命周期函数--监听页面初次渲染完成
  23. */
  24. onReady: function() {
  25. },
  26. /**
  27. * 生命周期函数--监听页面显示
  28. */
  29. onShow: function() {
  30. },
  31. /**
  32. * 生命周期函数--监听页面隐藏
  33. */
  34. onHide: function() {
  35. },
  36. /**
  37. * 生命周期函数--监听页面卸载
  38. */
  39. onUnload: function() {
  40. },
  41. /**
  42. * 页面相关事件处理函数--监听用户下拉动作
  43. */
  44. onPullDownRefresh() {
  45. wx.showNavigationBarLoading() //在标题栏中显示加载
  46. this.getCouponList();
  47. wx.hideNavigationBarLoading() //完成停止加载
  48. wx.stopPullDownRefresh() //停止下拉刷新
  49. },
  50. /**
  51. * 页面上拉触底事件的处理函数
  52. */
  53. onReachBottom: function() {
  54. },
  55. /**
  56. * 用户点击右上角分享
  57. */
  58. onShareAppMessage: function() {
  59. },
  60. getCouponList: function() {
  61. let that = this;
  62. that.setData({
  63. scrollTop: 0,
  64. showPage: false,
  65. couponList: []
  66. });
  67. util.request(api.CouponMyList, {
  68. status: that.data.status,
  69. page: that.data.page,
  70. limit: that.data.limit
  71. }).then(function(res) {
  72. if (res.errno === 0) {
  73. that.setData({
  74. scrollTop: 0,
  75. couponList: res.data.list,
  76. showPage: res.data.total > that.data.limit,
  77. count: res.data.total
  78. });
  79. }
  80. });
  81. },
  82. bindExchange: function (e) {
  83. this.setData({
  84. code: e.detail.value
  85. });
  86. },
  87. clearExchange: function () {
  88. this.setData({
  89. code: ''
  90. });
  91. },
  92. goExchange: function() {
  93. if (this.data.code.length === 0) {
  94. util.showErrorToast("请输入兑换码");
  95. return;
  96. }
  97. let that = this;
  98. util.request(api.CouponExchange, {
  99. code: that.data.code
  100. }, 'POST').then(function (res) {
  101. if (res.errno === 0) {
  102. that.getCouponList();
  103. that.clearExchange();
  104. wx.showToast({
  105. title: "领取成功",
  106. duration: 2000
  107. })
  108. }
  109. else{
  110. util.showErrorToast(res.errmsg);
  111. }
  112. });
  113. },
  114. nextPage: function(event) {
  115. var that = this;
  116. if (this.data.page > that.data.count / that.data.limit) {
  117. return true;
  118. }
  119. that.setData({
  120. page: that.data.page + 1
  121. });
  122. this.getCouponList();
  123. },
  124. prevPage: function(event) {
  125. if (this.data.page <= 1) {
  126. return false;
  127. }
  128. var that = this;
  129. that.setData({
  130. page: that.data.page - 1
  131. });
  132. this.getCouponList();
  133. },
  134. switchTab: function(e) {
  135. this.setData({
  136. couponList: [],
  137. status: e.currentTarget.dataset.index,
  138. page: 1,
  139. limit: 10,
  140. count: 0,
  141. scrollTop: 0,
  142. showPage: false
  143. });
  144. this.getCouponList();
  145. },
  146. })