activityInfo.js 5.4 KB

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