activityInfo.js 6.1 KB

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