couponCenter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // pages/couponCenter/couponCenter.js
  2. var http = require("../../utils/http.js");
  3. var config = require("../../utils/config.js");
  4. var util = require('../../utils/util.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. couponList: [], // 通用券列表
  11. prodCouponList: [], // 商品券列表
  12. selectedCouponId:0, // 要领取的优惠券id
  13. current: 1, // 当前页数
  14. pages: 1, // 总页数
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function(options) {
  20. //获取通用列表
  21. },
  22. /**
  23. * 生命周期函数--监听页面初次渲染完成
  24. */
  25. onReady: function() {
  26. },
  27. /**
  28. * 生命周期函数--监听页面显示
  29. */
  30. onShow: function() {
  31. this.getCouponList();
  32. this.getProdCouponList(1);
  33. },
  34. /**
  35. * 生命周期函数--监听页面隐藏
  36. */
  37. onHide: function() {
  38. },
  39. /**
  40. * 生命周期函数--监听页面卸载
  41. */
  42. onUnload: function() {
  43. },
  44. /**
  45. * 页面相关事件处理函数--监听用户下拉动作
  46. */
  47. onPullDownRefresh: function() {
  48. },
  49. /**
  50. * 页面上拉触底事件的处理函数
  51. */
  52. onReachBottom: function() {
  53. },
  54. /**
  55. * 用户点击右上角分享
  56. */
  57. onShareAppMessage: function() {
  58. },
  59. /**
  60. * 获取通用列表
  61. */
  62. getCouponList() {
  63. var params = {
  64. url: "/p/myCoupon/generalCouponList",
  65. method: "GET",
  66. callBack: (res) => {
  67. this.initCouponCanGoUseFlag(res)
  68. this.setData({
  69. couponList: res
  70. });
  71. }
  72. };
  73. http.request(params);
  74. },
  75. /**
  76. * 获取指定商品券
  77. */
  78. getProdCouponList(cur) {
  79. var params = {
  80. url: "/p/myCoupon/getCouponPage",
  81. method: "GET",
  82. data: {
  83. current: cur,
  84. size: 20,
  85. },
  86. callBack: (res) => {
  87. this.initCouponCanGoUseFlag(res.records)
  88. let list = []
  89. if (res.current == 1) {
  90. list = res.records
  91. } else {
  92. list = this.data.prodCouponList
  93. list = list.concat(res.records)
  94. }
  95. this.setData({
  96. prodCouponList: list,
  97. pages: res.pages,
  98. current: res.current
  99. });
  100. }
  101. };
  102. http.request(params);
  103. },
  104. /**
  105. * 初始化优惠券去可以使用的标记
  106. */
  107. initCouponCanGoUseFlag(couponList){
  108. couponList.forEach(coupon => {
  109. coupon.canGoUse = (coupon.curUserReceiveCount >= coupon.limitNum)
  110. });
  111. },
  112. /**
  113. * 设置优惠券去可以使用的标记
  114. */
  115. setCouponCanGoUseFlag(index, couponType) {
  116. if (couponType == 1) {
  117. var tempCouponList = this.data.couponList
  118. tempCouponList[index].canGoUse = true
  119. tempCouponList[index].stocks -= 1
  120. this.setData({
  121. couponList: tempCouponList
  122. })
  123. } else if (couponType == 2) {
  124. var tempCouponList = this.data.prodCouponList
  125. tempCouponList[index].canGoUse = true
  126. tempCouponList[index].stocks -= 1
  127. this.setData({
  128. prodCouponList: tempCouponList
  129. })
  130. }
  131. },
  132. /**
  133. * 立即领取
  134. */
  135. receiveCoupon(e) {
  136. this.setData({
  137. selectedCouponId: e.currentTarget.dataset.couponid
  138. })
  139. util.checkAuthInfo(()=>{
  140. var ths = this
  141. if (ths.data.selectedCouponId) {
  142. wx.showLoading();
  143. http.request({
  144. url: "/p/myCoupon/receive",
  145. method: "POST",
  146. data: ths.data.selectedCouponId,
  147. callBack: (data) => {
  148. wx.hideLoading();
  149. wx.showToast({
  150. title: '领券成功',
  151. icon: 'success',
  152. duration: 2000
  153. })
  154. this.setCouponCanGoUseFlag(e.currentTarget.dataset.couponindex,e.currentTarget.dataset.coupontype)
  155. }
  156. })
  157. }
  158. })
  159. },
  160. /**
  161. * 立即使用
  162. */
  163. useCoupon(e) {
  164. let url = '/pages/prod-classify/prod-classify?sts=4';
  165. let couponId = e.currentTarget.dataset.couponid;
  166. var title = "优惠券活动商品";
  167. var prodList = e.currentTarget.dataset.prodlist
  168. if (prodList && prodList.length == 1) {
  169. wx.navigateTo({
  170. url: '/pages/prod/prod?prodid=' + prodList[0].prodId
  171. })
  172. }else {
  173. if (couponId) {
  174. url += "&tagid=" + couponId + "&title=" + title;
  175. }
  176. wx.navigateTo({
  177. url: url
  178. })
  179. }
  180. },
  181. /**
  182. * 页面上拉触底事件的处理函数
  183. */
  184. onReachBottom: function () {
  185. if (this.data.current < this.data.pages) {
  186. this.getProdCouponList(this.data.current + 1);
  187. }
  188. },
  189. })