activityInfo.js 5.3 KB

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