activityInfo.js 4.8 KB

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