couponSelect.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. cartId: 0,
  8. couponId: 0,
  9. userCouponId: 0,
  10. grouponLinkId: 0,
  11. scrollTop: 0
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. },
  18. /**
  19. * 生命周期函数--监听页面初次渲染完成
  20. */
  21. onReady: function () {
  22. },
  23. /**
  24. * 生命周期函数--监听页面显示
  25. */
  26. onShow: function () {
  27. // 页面显示
  28. wx.showLoading({
  29. title: '加载中...',
  30. });
  31. try {
  32. var cartId = wx.getStorageSync('cartId');
  33. if (!cartId) {
  34. cartId = 0;
  35. }
  36. var couponId = wx.getStorageSync('couponId');
  37. if (!couponId) {
  38. couponId = 0;
  39. }
  40. var userCouponId = wx.getStorageSync('userCouponId');
  41. if (!userCouponId) {
  42. userCouponId = 0;
  43. }
  44. var grouponRulesId = wx.getStorageSync('grouponRulesId');
  45. if (!grouponRulesId) {
  46. grouponRulesId = 0;
  47. }
  48. this.setData({
  49. cartId: cartId,
  50. couponId: couponId,
  51. userCouponId: userCouponId,
  52. grouponRulesId: grouponRulesId
  53. });
  54. } catch (e) {
  55. // Do something when catch error
  56. console.log(e);
  57. }
  58. this.getCouponList();
  59. },
  60. /**
  61. * 生命周期函数--监听页面隐藏
  62. */
  63. onHide: function () {
  64. },
  65. /**
  66. * 生命周期函数--监听页面卸载
  67. */
  68. onUnload: function () {
  69. },
  70. /**
  71. * 页面相关事件处理函数--监听用户下拉动作
  72. */
  73. onPullDownRefresh: function () {
  74. },
  75. /**
  76. * 页面上拉触底事件的处理函数
  77. */
  78. onReachBottom() {
  79. },
  80. /**
  81. * 用户点击右上角分享
  82. */
  83. onShareAppMessage: function () {
  84. },
  85. getCouponList: function () {
  86. let that = this;
  87. that.setData({
  88. couponList: []
  89. });
  90. // 页面渲染完成
  91. wx.showToast({
  92. title: '加载中...',
  93. icon: 'loading',
  94. duration: 2000
  95. });
  96. util.request(api.CouponSelectList, {
  97. cartId: that.data.cartId,
  98. grouponRulesId: that.data.grouponRulesId,
  99. }).then(function (res) {
  100. if (res.errno === 0) {
  101. let list = [];
  102. for (var i = 0; i < res.data.list.length; i++) {
  103. if (res.data.list[i].available) {
  104. list.push(res.data.list[i]);
  105. }
  106. }
  107. that.setData({
  108. couponList: list
  109. });
  110. }
  111. wx.hideToast();
  112. });
  113. },
  114. selectCoupon: function (e) {
  115. try {
  116. wx.setStorageSync('couponId', e.currentTarget.dataset.cid);
  117. wx.setStorageSync('userCouponId', e.currentTarget.dataset.id);
  118. } catch (error) {
  119. }
  120. wx.navigateBack();
  121. },
  122. unselectCoupon: function() {
  123. // 如果优惠券ID设置-1,则表示订单不使用优惠券
  124. try {
  125. wx.setStorageSync('couponId', -1);
  126. wx.setStorageSync('userCouponId', -1);
  127. } catch (error) {
  128. }
  129. wx.navigateBack();
  130. }
  131. })