activityInfo.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. channelId: '',
  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. channelId: options.channelId,
  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. department: options.channelId
  55. })
  56. },
  57. /**
  58. * 生命周期函数--监听页面初次渲染完成
  59. */
  60. onReady: function () {
  61. },
  62. /**
  63. * 生命周期函数--监听页面显示
  64. */
  65. onShow: function () {
  66. },
  67. /**
  68. * 生命周期函数--监听页面隐藏
  69. */
  70. onHide: function () {
  71. },
  72. /**
  73. * 生命周期函数--监听页面卸载
  74. */
  75. onUnload: function () {
  76. },
  77. /**
  78. * 页面相关事件处理函数--监听用户下拉动作
  79. */
  80. onPullDownRefresh: function () {
  81. },
  82. /**
  83. * 页面上拉触底事件的处理函数
  84. */
  85. onReachBottom: function () {
  86. },
  87. /**
  88. * 用户点击右上角分享
  89. */
  90. onShareAppMessage: function () {
  91. },
  92. getActivityDetail: async function(activityId) {
  93. let res = await WelfareMall.getActivityDetail(activityId)
  94. let statusShowText = '';
  95. if(res.data.stock <= 0){
  96. statusShowText = '已售罄'
  97. }
  98. if(res.data.status ==3){
  99. statusShowText = '已结束'
  100. }
  101. if(res.data.status ==1){
  102. statusShowText = '待开始'
  103. }
  104. console.log(res.data.stock)
  105. let buttonText = res.data.isPay == 1 ? '立即抢购' : '立即领取';
  106. // 构建券包传参参数
  107. const couponList = []
  108. if (res.data.activityCouponList && Array.isArray(res.data.activityCouponList)) {
  109. res.data.activityCouponList.forEach(v => {
  110. const coupon = v.coupon
  111. if (coupon) {
  112. coupon.num = v.num
  113. couponList.push(coupon)
  114. }
  115. })
  116. }
  117. this.setData({
  118. detail:res.data,
  119. statusShowText:statusShowText,
  120. buttonText:buttonText,
  121. couponList,
  122. })
  123. },
  124. /**
  125. * 获取手机号
  126. * @param {*} e
  127. */
  128. getPhoneNumber(e) {
  129. let _self = this;
  130. getPhoneNumberSync(e, async () => {
  131. let userMobile = Activity.getMobileCache();
  132. if (userMobile.length !== 0) {
  133. _self.setData({
  134. userMobile: userMobile,
  135. })
  136. _self.nowBuy();
  137. }
  138. })
  139. // this.setData({
  140. // hideWindowValue:true
  141. // })
  142. // return;
  143. // let _self = this;
  144. // var encryptedData = e.detail.encryptedData;
  145. // var iv = e.detail.iv;
  146. // if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
  147. // return;
  148. // }
  149. //获取手机号
  150. // app.doDecodePhone(encryptedData, iv, function () {
  151. // let userMobile = Activity.getMobileCache();
  152. // if (userMobile.length !== 0) {
  153. // _self.setData({
  154. // userMobile: userMobile,
  155. // })
  156. // _self.nowBuy();
  157. // }
  158. // });
  159. },
  160. /**
  161. * 下单
  162. */
  163. async nowBuy() {
  164. let _self = this;
  165. if(!this.data.preventDup){
  166. return;
  167. }
  168. this.setData({
  169. preventDup:false
  170. })
  171. try {
  172. let res = await WelfareMall.createOrder(_self.data.userMobile, _self.data.detail.activityId,1, "", this.refStatisticsPostParam);
  173. _self.setData({
  174. orderInfo: res.data,
  175. })
  176. if (this.data.detail.isPay == 1 && _self.data.orderInfo.orderSn) {
  177. _self.goPay();
  178. } else {
  179. _self.setData({
  180. hideWindowValue:true,
  181. preventDup:true
  182. })
  183. }
  184. } catch (err) {
  185. this.setData({
  186. preventDup:true
  187. })
  188. app.showToast(err.msg);
  189. }
  190. Statistics.done({
  191. module: 'activity:detail',
  192. action: 'click',
  193. businessId: this.data.activityId,
  194. // department: options.department
  195. department: this.data.channelId
  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. let res = await WelfareMall.getPayStatus(this.data.orderSn);
  239. console.log(res);
  240. wx.redirectTo({
  241. url: '/pages/welfareMall/order/orderCompletion?ordersn='+this.data.orderSn
  242. })
  243. },
  244. hideWindow(){
  245. this.setData({
  246. hideWindowValue:false
  247. })
  248. }
  249. })