activityInfo.js 5.4 KB

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