receiveCoupon.js 6.2 KB

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