activityInfo.js 6.5 KB

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