couponReceive.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. options: '',
  10. couponList: [],
  11. scrollTop: 0,
  12. typeList: [{
  13. name: "全部",
  14. type: 0
  15. },
  16. {
  17. name: "立减券",
  18. type: 1
  19. },
  20. {
  21. name: "折扣券",
  22. type: 2
  23. },
  24. ],
  25. fraId: '',
  26. recCode: '',
  27. addressInfo: ''
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function (options) {
  33. if (this.data.options == '') {
  34. this.setData({
  35. options: options
  36. });
  37. }
  38. console.info(options);
  39. },
  40. /**
  41. * 生命周期函数--监听页面显示
  42. */
  43. onShow: function () {
  44. if (app.globalData.hasLogin == false) {
  45. wx.navigateTo({
  46. url: "/pages/auth/login/login"
  47. });
  48. return;
  49. }
  50. setTimeout(() => {
  51. this.getLocation();
  52. }, 500);
  53. },
  54. getCouponList() {
  55. let that = this;
  56. let options = that.data.options;
  57. that.setData({
  58. options: ''
  59. })
  60. let scene = decodeURIComponent(options.scene);
  61. var fraId, recCode;
  62. //两种格式 一种加盟商推荐fraId: 一种服务者推荐:recCode:
  63. if (scene && scene.indexOf("fraId:") >= 0){
  64. fraId = scene.split(":")[1];
  65. wx.setStorageSync('fraId',fraId);
  66. }
  67. if (scene && scene.indexOf("recCode:") >= 0)
  68. recCode = scene.split(":")[1];
  69. that.setData({
  70. fraId: fraId,
  71. recCode: recCode
  72. });
  73. util.request(api.CouponReceiveList, {
  74. city: that.data.addressInfo.city
  75. }).then(function (res) {
  76. if (res.errno === 0) {
  77. that.setData({
  78. couponList: res.data,
  79. });
  80. console.info(that.data.couponList)
  81. }
  82. });
  83. },
  84. goReceive(e) { //点击领取按钮
  85. let that = this;
  86. let id = e.currentTarget.dataset.id;
  87. let coupon = null;
  88. let coupons = this.data.couponList;
  89. coupons.forEach(item => {
  90. if (item.id == id)
  91. coupon = item;
  92. })
  93. let goodsvalue = coupon.goodsValue;
  94. util.request(api.CouponReceive, {
  95. couponId: id,
  96. fraId: that.data.fraId,
  97. recCode: that.data.recCode
  98. }, "POST").then(res => {
  99. if (res.errno === 0) {
  100. that.goBuy(goodsvalue,id);
  101. } else {
  102. util.showErrorToast(res.errmsg);
  103. }
  104. }).catch(res => {
  105. util.showErrorToast(res.errmsg)
  106. })
  107. },
  108. goBuy(goodsvalue,couponId) {
  109. // if (goodsvalue.length == 1) {
  110. // wx.navigateTo({
  111. // url: '/pages/goods/goods?id=' + goodsvalue[0],
  112. // })
  113. // } else
  114. if (goodsvalue.length >= 1) {
  115. wx.navigateTo({
  116. url: '/pages/ucenter/couponGoods/couponGoods?id=' + couponId
  117. })
  118. } else {
  119. wx.switchTab({
  120. url: '/pages/tabBar/catalog/catalog',
  121. })
  122. }
  123. },
  124. getLocation() {
  125. let that = this
  126. //获取地理位置 缓存中是否有地址
  127. /**
  128. * 分2重判断
  129. * 1:缓存中是否有地址
  130. * 2:未登录 取当前地理位置判断
  131. */
  132. var addressInfo = wx.getStorageSync("addressInfo");
  133. console.info(addressInfo);
  134. if (addressInfo) {
  135. that.setData({
  136. addressInfo: addressInfo
  137. })
  138. that.getCouponList();
  139. } else {
  140. map.getCity().then(res => {
  141. that.setData({
  142. addressInfo: res
  143. })
  144. that.getCouponList();
  145. }).catch(res => { //reject内容 1:未授权 2:不在服务范围 3:异常解析
  146. wx.showModal({
  147. title: '提醒',
  148. showCancel: false,
  149. content: '您未授权或非业务城市,无法获取优惠券信息'
  150. })
  151. });
  152. }
  153. },
  154. /**
  155. * 生命周期函数--监听页面初次渲染完成
  156. */
  157. onReady: function () {
  158. },
  159. /**
  160. * 生命周期函数--监听页面隐藏
  161. */
  162. onHide: function () {
  163. },
  164. /**
  165. * 生命周期函数--监听页面卸载
  166. */
  167. onUnload: function () {
  168. },
  169. /**
  170. * 页面相关事件处理函数--监听用户下拉动作
  171. */
  172. onPullDownRefresh: function () {
  173. },
  174. /**
  175. * 页面上拉触底事件的处理函数
  176. */
  177. onReachBottom: function () {
  178. },
  179. /**
  180. * 用户点击右上角分享
  181. */
  182. onShareAppMessage: function () {
  183. }
  184. })