specialDiscount.js 3.4 KB

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