activityInfo.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // pages/welfareMall/activityInfo/activityInfo.js
  2. import WelfareMall from '../../../api/welfareMall';
  3. import Activity from '../../../api/activity';
  4. const util = require('../../../utils/util.js');
  5. const app = getApp();
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. detail:[],
  12. activityId:'',
  13. statusShowText:'',
  14. buttonText:'',
  15. userMobile: '', //用户手机号
  16. orderInfo: [], //下单信息
  17. hideWindowValue:false,
  18. preventDup:true
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: async function (options) {
  24. let userMobile = await Activity.getMobileCache();
  25. if (userMobile.length !== 0) {
  26. this.setData({
  27. userMobile: userMobile,
  28. })
  29. }
  30. console.log(userMobile);
  31. if(options.activityId){
  32. this.getActivityDetail(options.activityId)
  33. }
  34. },
  35. /**
  36. * 生命周期函数--监听页面初次渲染完成
  37. */
  38. onReady: function () {
  39. },
  40. /**
  41. * 生命周期函数--监听页面显示
  42. */
  43. onShow: function () {
  44. },
  45. /**
  46. * 生命周期函数--监听页面隐藏
  47. */
  48. onHide: function () {
  49. },
  50. /**
  51. * 生命周期函数--监听页面卸载
  52. */
  53. onUnload: function () {
  54. },
  55. /**
  56. * 页面相关事件处理函数--监听用户下拉动作
  57. */
  58. onPullDownRefresh: function () {
  59. },
  60. /**
  61. * 页面上拉触底事件的处理函数
  62. */
  63. onReachBottom: function () {
  64. },
  65. /**
  66. * 用户点击右上角分享
  67. */
  68. onShareAppMessage: function () {
  69. },
  70. getActivityDetail: async function(activityId) {
  71. let res = await WelfareMall.getActivityDetail(activityId)
  72. let statusShowText = '';
  73. if(res.data.stock <= 0){
  74. statusShowText = '已售罄'
  75. }
  76. if(res.data.status ==3){
  77. statusShowText = '已结束'
  78. }
  79. console.log(res.data.stock)
  80. let buttonText = res.data.isPay == 1 ? '立即抢购' : '立即领取';
  81. // 构建券包传参参数
  82. const couponList = []
  83. if (res.data.activityCouponList && Array.isArray(res.data.activityCouponList)) {
  84. res.data.activityCouponList.forEach(v => {
  85. const coupon = v.coupon
  86. if (coupon) {
  87. coupon.num = v.num
  88. couponList.push(coupon)
  89. }
  90. })
  91. }
  92. this.setData({
  93. detail:res.data,
  94. statusShowText:statusShowText,
  95. buttonText:buttonText,
  96. couponList,
  97. })
  98. },
  99. /**
  100. * 获取手机号
  101. * @param {*} e
  102. */
  103. getPhoneNumber(e) {
  104. // this.setData({
  105. // hideWindowValue:true
  106. // })
  107. // return;
  108. let _self = this;
  109. var encryptedData = e.detail.encryptedData;
  110. var iv = e.detail.iv;
  111. if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
  112. return;
  113. }
  114. //获取手机号
  115. app.doDecodePhone(encryptedData, iv, function () {
  116. let userMobile = Activity.getMobileCache();
  117. if (userMobile.length !== 0) {
  118. _self.setData({
  119. userMobile: userMobile,
  120. })
  121. _self.nowBuy();
  122. }
  123. });
  124. },
  125. /**
  126. * 下单
  127. */
  128. async nowBuy() {
  129. let _self = this;
  130. if(!this.data.preventDup){
  131. return;
  132. }
  133. this.setData({
  134. preventDup:false
  135. })
  136. try {
  137. let res = await WelfareMall.createOrder(_self.data.userMobile, _self.data.detail.activityId,1);
  138. _self.setData({
  139. orderInfo: res.data,
  140. })
  141. if (this.data.detail.isPay == 1 && _self.data.orderInfo.orderSn) {
  142. _self.goPay();
  143. } else {
  144. _self.setData({
  145. hideWindowValue:true,
  146. preventDup:true
  147. })
  148. }
  149. } catch (err) {
  150. this.setData({
  151. preventDup:true
  152. })
  153. app.showToast(err.msg);
  154. }
  155. },
  156. /**
  157. * 获取订单参数
  158. */
  159. async goPay() {
  160. // console.log(wx.getStorageSync('loginInfo').openId)
  161. var _self = this;
  162. let result = await WelfareMall.getOrderParams(_self.data.orderInfo.orderSn,wx.getStorageSync('loginInfo').openId);
  163. // console.log(result);
  164. let res = result.data
  165. wx.requestPayment({
  166. timeStamp: res.wxPrePayVo.timeStamp,
  167. package: res.wxPrePayVo.pack,
  168. nonceStr: res.wxPrePayVo.nonceStr,
  169. signType: res.wxPrePayVo.signType,
  170. paySign: res.wxPrePayVo.paySign,
  171. success: function (res) {
  172. //支付成功,跳转领取成功页面
  173. _self.setData({
  174. hideWindowValue:true,
  175. preventDup:true
  176. })
  177. },
  178. fail: function (res) {
  179. _self.setData({
  180. preventDup:true
  181. })
  182. _self.goToOrderList()
  183. }
  184. })
  185. },
  186. goToOrderList(){
  187. wx.redirectTo({
  188. url: '/pages/welfareMall/historical/historical'
  189. })
  190. },
  191. hideWindow(){
  192. this.setData({
  193. hideWindowValue:false
  194. })
  195. }
  196. })