activityInfo.js 5.7 KB

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