receiveCoupon.js 5.7 KB

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