couponList.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. if (options.scene) { // id,12;type,0
  20. var paras = decodeURIComponent(options.scene).split(";");
  21. console.info(paras)
  22. var id = paras[0].replace("id,", "");
  23. console.info(id)
  24. //开始领取优惠券
  25. util.request(api.CouponReceive, {
  26. couponId: id
  27. }, "POST").then(res => {
  28. if (res.errno == 0) {
  29. wx.showToast({
  30. title: '领取成功',
  31. icon: 'none'
  32. })
  33. } else util.showErrorToast(res.errmsg);
  34. this.getCouponList();
  35. })
  36. } else
  37. this.getCouponList();
  38. },
  39. /**
  40. * 生命周期函数--监听页面初次渲染完成
  41. */
  42. onReady: function () {
  43. },
  44. /**
  45. * 生命周期函数--监听页面显示
  46. */
  47. onShow: function () {
  48. },
  49. /**
  50. * 生命周期函数--监听页面隐藏
  51. */
  52. onHide: function () {
  53. },
  54. /**
  55. * 生命周期函数--监听页面卸载
  56. */
  57. onUnload: function () {
  58. },
  59. goService(e) {
  60. let goodsvalue = e.currentTarget.dataset.goodsvalue;
  61. let id = e.currentTarget.dataset.id;
  62. console.log(goodsvalue);
  63. if (goodsvalue.length == 1) {
  64. wx.navigateTo({
  65. url: '/pages/goods/goods?id=' + goodsvalue[0],
  66. })
  67. }
  68. else if (goodsvalue.length > 1) {
  69. wx.navigateTo({
  70. url: '/pages/ucenter/couponGoods/couponGoods?id='+id
  71. })
  72. }
  73. else {
  74. wx.switchTab({
  75. url: '/pages/tabBar/catalog/catalog',
  76. })
  77. }
  78. },
  79. /**
  80. * 页面相关事件处理函数--监听用户下拉动作
  81. */
  82. onPullDownRefresh() {
  83. wx.showNavigationBarLoading() //在标题栏中显示加载
  84. this.getCouponList();
  85. wx.hideNavigationBarLoading() //完成停止加载
  86. wx.stopPullDownRefresh() //停止下拉刷新
  87. },
  88. /**
  89. * 页面上拉触底事件的处理函数
  90. */
  91. onReachBottom: function () {
  92. },
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function () {
  97. },
  98. getCouponList: function () {
  99. let that = this;
  100. that.setData({
  101. scrollTop: 0,
  102. showPage: false,
  103. couponList: []
  104. });
  105. util.request(api.CouponMyList, {
  106. status: that.data.status,
  107. page: that.data.page,
  108. limit: that.data.limit
  109. }).then(function (res) {
  110. if (res.errno === 0) {
  111. that.setData({
  112. scrollTop: 0,
  113. couponList: res.data.list,
  114. showPage: res.data.total > that.data.limit,
  115. count: res.data.total
  116. });
  117. }
  118. });
  119. },
  120. bindExchange: function (e) {
  121. this.setData({
  122. code: e.detail.value
  123. });
  124. },
  125. clearExchange: function () {
  126. this.setData({
  127. code: ''
  128. });
  129. },
  130. goExchange: function () {
  131. if (this.data.code.length === 0) {
  132. util.showErrorToast("请输入兑换码");
  133. return;
  134. }
  135. let that = this;
  136. wx.showModal({
  137. title: '提示',
  138. content: '确定要兑换么?兑换后将无法撤回',
  139. success: (res) => {
  140. if (res.confirm) {
  141. util.request(api.CouponExchange, {
  142. code: that.data.code
  143. }, 'POST').then(function (res) {
  144. if (res.errno === 0) {
  145. that.getCouponList();
  146. that.clearExchange();
  147. wx.showToast({
  148. title: "领取成功",
  149. duration: 2000
  150. })
  151. } else {
  152. util.showErrorToast(res.errmsg);
  153. }
  154. });
  155. }
  156. }
  157. });
  158. },
  159. nextPage: function (event) {
  160. var that = this;
  161. if (this.data.page > that.data.count / that.data.limit) {
  162. return true;
  163. }
  164. that.setData({
  165. page: that.data.page + 1
  166. });
  167. this.getCouponList();
  168. },
  169. prevPage: function (event) {
  170. if (this.data.page <= 1) {
  171. return false;
  172. }
  173. var that = this;
  174. that.setData({
  175. page: that.data.page - 1
  176. });
  177. this.getCouponList();
  178. },
  179. switchTab: function (e) {
  180. this.setData({
  181. couponList: [],
  182. status: e.currentTarget.dataset.index,
  183. page: 1,
  184. limit: 10,
  185. count: 0,
  186. scrollTop: 0,
  187. showPage: false
  188. });
  189. this.getCouponList();
  190. },
  191. onTabChange(e) {
  192. let status = e.detail.name;
  193. this.setData({
  194. couponList: [],
  195. status: status,
  196. page: 1,
  197. limit: 10,
  198. count: 0,
  199. scrollTop: 0,
  200. showPage: false
  201. });
  202. this.getCouponList();
  203. },
  204. })