discountDetail.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // pages/discountDetail/discountDetail.js
  2. var http = require('../../utils/http.js');
  3. var util = require('../../utils/util.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. discountDetail: {}, //活动详情
  10. discountId: '',
  11. hour: 0, //时
  12. min: 0, //分
  13. second: 0, //秒
  14. discountProdList: [], //限时特惠商品列表
  15. current: 1, // 当前页数
  16. pages: 1, // 总页数
  17. endTime: '', //结束时间
  18. timer: ''
  19. },
  20. // 获取活动详情
  21. getDiscountDet () {
  22. // 请求详情数据
  23. var _this = this;
  24. http.request({
  25. url: "/marking/discount/getDiscountByDiscountId/" + this.data.discountId,
  26. method: "GET",
  27. callBack: (res) => {
  28. _this.setData({
  29. discountDetail: res,
  30. endTime: res.endTime
  31. })
  32. _this.countTime()
  33. }
  34. })
  35. },
  36. // 获取限时特惠商品列表
  37. getDiscountProdList (cur) {
  38. wx.showLoading();
  39. var ths = this;
  40. http.request({
  41. url: "/marking/discount/prodList",
  42. data: {
  43. discountId: this.data.discountId, //活动id
  44. current: cur, //当前页
  45. size: 20
  46. },
  47. method: "GET",
  48. callBack: (res) => {
  49. var list = [];
  50. if (res.current == 1) {
  51. list = res.records;
  52. } else {
  53. list = ths.data.discountProdList;
  54. Array.prototype.push.apply(list, res.records);
  55. }
  56. this.setData({
  57. discountProdList: list,
  58. pages: res.pages,
  59. current: res.current
  60. })
  61. wx.hideLoading()
  62. }
  63. })
  64. },
  65. // 倒计时
  66. countTime () {
  67. // 获取当前时间
  68. let date = new Date()
  69. let now = date.getTime()
  70. // 设置截止时间
  71. let end = util.dateToTimestamp(this.data.endTime)
  72. // 时间差
  73. let leftTime = end - now
  74. // 定义变量 d,h,m,s保存倒计时的时间
  75. if (leftTime >= 0) {
  76. // 时
  77. let h = Math.floor(leftTime / 1000 / 60 / 60)
  78. // 分
  79. let m = Math.floor(leftTime / 1000 / 60 % 60)
  80. // 秒
  81. let s = Math.floor(leftTime / 1000 % 60)
  82. this.setData({
  83. hour: h < 10 ? '0' + h : h,
  84. min: m < 10 ? '0' + m : m,
  85. second: s < 10 ? '0' + s : s
  86. })
  87. } else {
  88. this.setData({
  89. hour: '00',
  90. min: '00',
  91. second: '00'
  92. })
  93. }
  94. // 等于0的时候不调用
  95. if (Number(this.data.hour) === 0 && Number(this.data.min) === 0 && Number(this.data.second) === 0) {
  96. return
  97. } else {
  98. // 递归每秒调用countTime方法,显示动态时间效果,
  99. this.setData({
  100. timer: setTimeout(this.countTime, 1000)
  101. })
  102. }
  103. },
  104. //跳转商品详情
  105. toDiscountProdDet (e) {
  106. var prodid = e.currentTarget.dataset.prodid;
  107. if (prodid) {
  108. wx.navigateTo({
  109. url: '/pages/prod/prod?prodid=' + prodid,
  110. })
  111. }
  112. },
  113. /**
  114. * 生命周期函数--监听页面加载
  115. */
  116. onLoad: function (options) {
  117. const {
  118. discountId
  119. } = options
  120. this.setData({
  121. discountId
  122. })
  123. this.getDiscountDet()
  124. this.getDiscountProdList(1) //默认加载第一页
  125. },
  126. /**
  127. * 生命周期函数--监听页面初次渲染完成
  128. */
  129. onReady: function () {
  130. },
  131. /**
  132. * 生命周期函数--监听页面显示
  133. */
  134. onShow: function () {
  135. },
  136. /**
  137. * 生命周期函数--监听页面隐藏
  138. */
  139. onHide: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面卸载
  143. */
  144. onUnload: function () {
  145. clearTimeout(this.data.timer)
  146. },
  147. /**
  148. * 页面相关事件处理函数--监听用户下拉动作
  149. */
  150. onPullDownRefresh: function () {
  151. },
  152. /**
  153. * 页面上拉触底事件的处理函数
  154. */
  155. onReachBottom: function () {
  156. if (this.data.current < this.data.pages) {
  157. this.getDiscountProdList(this.data.current + 1);
  158. }
  159. },
  160. /**
  161. * 用户点击右上角分享
  162. */
  163. onShareAppMessage: function () {
  164. }
  165. })