activityInfo.js 6.8 KB

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