activityInfo.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. this.setData({
  82. detail:res.data,
  83. statusShowText:statusShowText,
  84. buttonText:buttonText
  85. })
  86. },
  87. /**
  88. * 获取手机号
  89. * @param {*} e
  90. */
  91. getPhoneNumber(e) {
  92. // this.setData({
  93. // hideWindowValue:true
  94. // })
  95. // return;
  96. let _self = this;
  97. var encryptedData = e.detail.encryptedData;
  98. var iv = e.detail.iv;
  99. if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
  100. return;
  101. }
  102. //获取手机号
  103. app.doDecodePhone(encryptedData, iv, function () {
  104. let userMobile = Activity.getMobileCache();
  105. if (userMobile.length !== 0) {
  106. _self.setData({
  107. userMobile: userMobile,
  108. })
  109. _self.nowBuy();
  110. }
  111. });
  112. },
  113. /**
  114. * 下单
  115. */
  116. async nowBuy() {
  117. let _self = this;
  118. if(!this.data.preventDup){
  119. return;
  120. }
  121. this.setData({
  122. preventDup:false
  123. })
  124. try {
  125. let res = await WelfareMall.createOrder(_self.data.userMobile, _self.data.detail.activityId,1);
  126. _self.setData({
  127. orderInfo: res.data,
  128. })
  129. if (this.data.detail.isPay == 1 && _self.data.orderInfo.orderSn) {
  130. _self.goPay();
  131. } else {
  132. _self.setData({
  133. hideWindowValue:true,
  134. preventDup:true
  135. })
  136. }
  137. } catch (err) {
  138. this.setData({
  139. preventDup:true
  140. })
  141. app.showToast(err.msg);
  142. }
  143. },
  144. /**
  145. * 获取订单参数
  146. */
  147. async goPay() {
  148. // console.log(wx.getStorageSync('loginInfo').openId)
  149. var _self = this;
  150. let result = await WelfareMall.getOrderParams(_self.data.orderInfo.orderSn,wx.getStorageSync('loginInfo').openId);
  151. // console.log(result);
  152. let res = result.data
  153. wx.requestPayment({
  154. timeStamp: res.wxPrePayVo.timeStamp,
  155. package: res.wxPrePayVo.pack,
  156. nonceStr: res.wxPrePayVo.nonceStr,
  157. signType: res.wxPrePayVo.signType,
  158. paySign: res.wxPrePayVo.paySign,
  159. success: function (res) {
  160. //支付成功,跳转领取成功页面
  161. _self.setData({
  162. hideWindowValue:true,
  163. preventDup:true
  164. })
  165. },
  166. fail: function (res) {
  167. _self.setData({
  168. preventDup:true
  169. })
  170. _self.goToOrderList()
  171. }
  172. })
  173. },
  174. goToOrderList(){
  175. wx.redirectTo({
  176. url: '/pages/welfareMall/historical/historical'
  177. })
  178. },
  179. hideWindow(){
  180. this.setData({
  181. hideWindowValue:false
  182. })
  183. }
  184. })