activityInfo.js 5.1 KB

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