receiveCoupon.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. const { default: activity } = require("../../api/activity");
  2. const util = require('../../utils/util.js')
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. activityId:'', //活动id
  10. userMobile:'', //电话
  11. codeId:'',//code唯一码
  12. memberMobile:'',//领取人手机号
  13. isEffective:false, //是否过期,默认过期
  14. activityInfo:[],
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. let _self = this;
  21. if(options.q) {
  22. let url = decodeURIComponent(options.q);
  23. let id = util.getQueryVariable('id',url);
  24. if(id) {
  25. _self.setData({
  26. codeId:id,
  27. })
  28. //根据codeId查出该活动是否过期
  29. _self.getShareActivityCode(id);
  30. }
  31. }
  32. },
  33. //根据code-id获取券详情信息 判断该codeId是否过期
  34. async getShareActivityCode(codeId) {
  35. let _self = this;
  36. try {
  37. let result = await activity.getShareActivityCode(codeId);
  38. _self.setData({
  39. // activityInfo:result.activityInfo,
  40. activityId:result.activityInfo.activityId
  41. })
  42. _self.activityInfo(result.activityInfo.activityId);
  43. let expireTime = result.expireTime;
  44. //失效的时间
  45. let expireTimeTime = util.getUnixTime(expireTime);
  46. //获取当前的时间戳
  47. var nowTime = Date.parse(new Date())/1000;
  48. if(expireTimeTime-nowTime>0) {
  49. _self.setData({
  50. isEffective:true
  51. })
  52. }
  53. }catch(error) {
  54. _self.setData({
  55. isEffective:false
  56. })
  57. }
  58. },
  59. /**
  60. *
  61. */
  62. getNowCoupon() {
  63. let _self = this;
  64. //判断是否过期
  65. if(_self.isEffective==false) {
  66. wx.showToast('券码包已过期')
  67. return;
  68. }
  69. _self.createOrder();
  70. },
  71. /**
  72. * 创建订单
  73. */
  74. async createOrder() {
  75. let _self = this;
  76. try {
  77. let order = await activity.createOrder(_self.data.activityId,_self.data.memberMobile);
  78. let orderSn = order.orderSn;
  79. _self.setData({
  80. orderSn:orderSn
  81. })
  82. let codeId = _self.data.codeId;
  83. try {
  84. //插入 订单 和 codeId bind-share-code
  85. let res = await activity.bindShareCode(orderSn,codeId);
  86. }catch(e) {
  87. console.log(e)
  88. }
  89. if (_self.data.activityInfo.isPay == 1 && orderSn) {
  90. // console.log(22222)
  91. //TODO::暂时屏蔽
  92. // _self.goPay();
  93. app.showToast('领取成功','success',1000,function(){
  94. wx.redirectTo({
  95. url: '/pages/myCoupons/myCoupons?mobile='+_self.data.memberMobile,
  96. })
  97. })
  98. } else {
  99. //领取成功
  100. app.showToast('领取成功','success',1000,function(){
  101. wx.redirectTo({
  102. url: '/pages/myCoupons/myCoupons?mobile='+_self.data.memberMobile,
  103. })
  104. })
  105. }
  106. }catch(e) {
  107. }
  108. },
  109. /**
  110. * 获取订单参数
  111. */
  112. async goPay() {
  113. var _self = this;
  114. try {
  115. let res = await activity.getOrderParams(_self.data.orderSn,_self.activityInfo.name);
  116. console.log(res);
  117. res = res.wxPrePayVo
  118. wx.requestPayment({
  119. timeStamp: res.timeStamp,
  120. package: res.pack,
  121. nonceStr: res.nonceStr,
  122. signType: res.signType,
  123. paySign: res.paySign,
  124. success: function (res) {
  125. //领取成功
  126. app.showToast('领取成功','success',1000,function(){
  127. wx.redirectTo({
  128. url: '/pages/myCoupons/myCoupons?mobile='+_self.data.memberMobile,
  129. })
  130. })
  131. },
  132. fail: function (res) {
  133. }
  134. })
  135. } catch (err) {
  136. console.log(err)
  137. }
  138. },
  139. //获取手机号
  140. getPhoneNumber(e) {
  141. let _self = this;
  142. var encryptedData = e.detail.encryptedData;
  143. console.log(encryptedData);
  144. var iv = e.detail.iv;
  145. if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
  146. return;
  147. }
  148. //获取手机号
  149. getApp().doDecodePhone(encryptedData, iv, function () {
  150. let userMobile = activity.getMobileCache();
  151. if (userMobile.length !== 0) {
  152. _self.setData({
  153. memberMobile: userMobile,
  154. })
  155. _self.createOrder();
  156. }
  157. });
  158. },
  159. /**
  160. * 生命周期函数--监听页面初次渲染完成
  161. */
  162. onReady: function () {
  163. },
  164. /**
  165. * 生命周期函数--监听页面显示
  166. */
  167. onShow: function () {
  168. },
  169. /**
  170. * 生命周期函数--监听页面隐藏
  171. */
  172. onHide: function () {
  173. },
  174. /**
  175. * 生命周期函数--监听页面卸载
  176. */
  177. onUnload: function () {
  178. },
  179. /**
  180. * 页面相关事件处理函数--监听用户下拉动作
  181. */
  182. onPullDownRefresh: function () {
  183. },
  184. /**
  185. * 页面上拉触底事件的处理函数
  186. */
  187. onReachBottom: function () {
  188. },
  189. /**
  190. * 用户点击右上角分享
  191. */
  192. onShareAppMessage: function () {
  193. },
  194. /**
  195. * 获取活动详细信息
  196. * @param {*} activityId
  197. */
  198. async activityInfo(activityId){
  199. let result = await activity.getActivityDetail(activityId);
  200. // console.log('获取活动详细信息');
  201. // console.log(result)
  202. var objSwitch = getApp().globalData.typeSwitchObj
  203. var couponTypeAndNumObj = {};
  204. var couponList = result.activityCouponList;
  205. for (let j = 0; j < couponList.length; j++) {
  206. var couponNum = couponList[j]['num'] ? couponList[j]['num'] : 1;
  207. var value = couponList[j]['coupon']['type'];
  208. var showText = objSwitch[couponList[j]['coupon']['type']];
  209. var nowNume = 0;
  210. if (couponTypeAndNumObj[value]) {
  211. nowNume = couponTypeAndNumObj[value]['num'];
  212. console.log(nowNume)
  213. }
  214. couponTypeAndNumObj[value] = {
  215. "typeText": showText,
  216. "num": couponNum + nowNume
  217. }
  218. }
  219. result.couponTypeAndNumObj = couponTypeAndNumObj
  220. this.setData({
  221. activityInfo:result
  222. })
  223. },
  224. })