activityInfo.js 4.1 KB

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