myCoupon.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // pages/myCoupon/myCoupon.js
  2. var http = require("../../utils/http.js");
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. type: 1,
  9. couponList: [],
  10. unUseCount: 0,
  11. useCount: 0,
  12. expiredCount: 0,
  13. pages: 0,
  14. current: 1,
  15. size: 10
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function(options) {
  21. this.couponCount()
  22. this.loadMyCouponData(1)
  23. },
  24. /**
  25. * 生命周期函数--监听页面初次渲染完成
  26. */
  27. onReady: function() {
  28. },
  29. /**
  30. * 生命周期函数--监听页面显示
  31. */
  32. onShow: function() {
  33. },
  34. /**
  35. * 生命周期函数--监听页面隐藏
  36. */
  37. onHide: function() {
  38. },
  39. /**
  40. * 生命周期函数--监听页面卸载
  41. */
  42. onUnload: function() {
  43. },
  44. /**
  45. * 页面相关事件处理函数--监听用户下拉动作
  46. */
  47. onPullDownRefresh: function() {
  48. if (this.data.current < this.data.pages) {
  49. this.loadOrderData(this.data.sts, this.data.current + 1);
  50. }
  51. },
  52. /**
  53. * 页面上拉触底事件的处理函数
  54. */
  55. onReachBottom: function() {
  56. if (this.data.current < this.data.pages) {
  57. this.setData({
  58. current: this.data.current + 1
  59. })
  60. this.loadMyCouponData(this.data.type)
  61. }
  62. },
  63. /**
  64. * 用户点击右上角分享
  65. */
  66. onShareAppMessage: function() {
  67. },
  68. /**
  69. * 获取我的优惠券列表
  70. */
  71. loadMyCouponData(type) {
  72. // 我的优惠券状态(0:过期 1:可用 2:已用)
  73. let status;
  74. if (type == 1) {
  75. status = 2
  76. } else if (type == 0) {
  77. status = 1
  78. } else if (type == 2) {
  79. status = 0
  80. }
  81. var params = {
  82. url: "/p/myCoupon/getCouponList",
  83. method: "GET",
  84. data: {
  85. status: status,
  86. current: this.data.current,
  87. size: this.data.size
  88. },
  89. callBack: (res) => {
  90. let list = []
  91. if (res.current == 1) {
  92. list = res.records
  93. } else {
  94. list = this.data.couponList
  95. list = list.concat(res.records)
  96. }
  97. this.setData({
  98. couponList: list,
  99. pages: res.pages
  100. })
  101. if (status == 0) {
  102. // 已过期数量
  103. this.setData({
  104. expiredCount: res.total
  105. });
  106. } else if (status == 1) {
  107. // 可用数量
  108. this.setData({
  109. unUseCount: res.total
  110. });
  111. } else if (status == 2) {
  112. // 使用过总数量
  113. this.setData({
  114. useCount: res.total
  115. });
  116. }
  117. }
  118. };
  119. http.request(params);
  120. },
  121. /**
  122. * 标签切换事件
  123. */
  124. changeTab(e) {
  125. this.setData({
  126. current: 1,
  127. type: e.currentTarget.dataset.type,
  128. couponList: []
  129. });
  130. this.loadMyCouponData(this.data.type)
  131. },
  132. /**
  133. * 获取各个状态下优惠券数量
  134. */
  135. couponCount() {
  136. var ths = this
  137. var params = {
  138. url: "/p/myCoupon/getMyCouponsStatusCount",
  139. method: "GET",
  140. data: {},
  141. callBack: function(res) {
  142. ths.setData({
  143. unUseCount: res.unUseCount,
  144. expiredCount: res.expiredCount,
  145. useCount: res.useCount,
  146. })
  147. }
  148. }
  149. http.request(params);
  150. },
  151. /**
  152. * 删除优惠券
  153. */
  154. delCouponHandle(e) {
  155. let couponUserId = e.currentTarget.dataset.couponuserid
  156. let ths = this
  157. wx.showModal({
  158. title: '',
  159. content: '确定要删除此优惠券吗?',
  160. confirmColor: "#eb2444",
  161. success(res) {
  162. if (res.confirm) {
  163. wx.showLoading();
  164. let params = {
  165. url: "/p/myCoupon/delCoupon/" + couponUserId,
  166. method: "DELETE",
  167. data: {},
  168. callBack: function(res) {
  169. wx.hideLoading();
  170. ths.setData({
  171. current: 1,
  172. pages: 0
  173. })
  174. wx.showToast({
  175. title: res,
  176. duration: 1000
  177. })
  178. setTimeout(() => {
  179. ths.loadMyCouponData(ths.data.type)
  180. },1000)
  181. }
  182. }
  183. http.request(params);
  184. } else if (res.cancel) {
  185. console.log('用户点击取消')
  186. }
  187. }
  188. })
  189. }
  190. })