snapUpList.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // pages/snapUpList/snapUpList.js
  2. const app = getApp()
  3. var http = require('../../utils/http.js');
  4. var config = require('../../utils/config.js');
  5. var util = require('../../utils/util.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. seckillList: [], // 秒杀商品列表
  12. countDownList: [], // 秒杀倒计时列表
  13. countDownEndTimeList:[],
  14. timer: '',
  15. current: 1,
  16. },
  17. /**
  18. * 获取秒杀商品列表
  19. */
  20. getsklist() {
  21. http.request({
  22. url: "/seckill/pageProd",
  23. method: "GET",
  24. data: {
  25. current: this.data.current,
  26. size: 5,
  27. lat: wx.getStorageSync('LATITUDE'),
  28. lon: wx.getStorageSync('LONGITUDE'),
  29. distance: wx.getStorageSync('DISTANCE')||0
  30. },
  31. callBack: (res) => {
  32. let result = []
  33. result = res.records.filter(item => util.dateToTimestamp(item.endTime) > new Date().getTime())
  34. this.setData({
  35. seckillList: res.current == 1 ? result : this.data.seckillList.concat(result),
  36. pages: res.pages
  37. })
  38. let endTimeList = []; // 倒计时数组
  39. for (let i = 0; i < this.data.seckillList.length; i++) {
  40. var objs = {}
  41. objs.eTime = this.data.seckillList[i].endTime
  42. objs.sTime = this.data.seckillList[i].startTime
  43. objs.countType = null //1表示秒杀活动正在进行,0表示秒杀活动未开始
  44. endTimeList.push(objs)
  45. }
  46. this.setData({
  47. countDownEndTimeList: endTimeList
  48. })
  49. this.countdown()
  50. }
  51. })
  52. },
  53. countdown() {
  54. // 获取当前时间,同时得到活动结束时间数组
  55. let newTime = new Date().getTime();
  56. let endTimeList = this.data.countDownEndTimeList;
  57. let countDownArr = [];
  58. // 对结束时间进行处理渲染到页面
  59. endTimeList.forEach(o => {
  60. if (newTime - util.dateToTimestamp(o.sTime) > 0) {
  61. let endTime = util.dateToTimestamp(o.eTime);
  62. let obj = null;
  63. // 如果活动未结束,对时间进行处理
  64. if (endTime - newTime > 0) {
  65. let time = (endTime - newTime) / 1000;
  66. // 获取天、时、分、秒
  67. let day = parseInt(time / (60 * 60 * 24));
  68. let hou = parseInt(time % (60 * 60 * 24) / 3600);
  69. let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
  70. let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
  71. obj = {
  72. day: this.timeFormat(day),
  73. hou: `${this.timeFormat(hou)}`,
  74. min: `${this.timeFormat(min)}`,
  75. sec: `${this.timeFormat(sec)}`,
  76. type: 1 // 表示秒杀正在进行
  77. }
  78. }
  79. // 活动已结束
  80. else {
  81. obj = {
  82. day: '00',
  83. hou: '00',
  84. min: '00',
  85. sec: '00'
  86. }
  87. }
  88. countDownArr.push(obj);
  89. }
  90. // 活动未开始
  91. else {
  92. let startTime = util.dateToTimestamp(o.sTime);
  93. let time = (startTime - newTime) / 1000;
  94. let obj = null;
  95. // 获取天、时、分、秒
  96. let day = parseInt(time / (60 * 60 * 24));
  97. let hou = parseInt(time % (60 * 60 * 24) / 3600);
  98. let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
  99. let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
  100. obj = {
  101. day: this.timeFormat(day),
  102. hou: `${this.timeFormat(hou)}`,
  103. min: `${this.timeFormat(min)}`,
  104. sec: `${this.timeFormat(sec)}`,
  105. type: 0 // 表示秒杀还没开始
  106. }
  107. countDownArr.push(obj);
  108. }
  109. })
  110. // 渲染,然后每隔一秒执行一次倒计时函数
  111. this.setData({
  112. countDownList: countDownArr,
  113. timer: setTimeout(this.countdown, 1000)
  114. })
  115. },
  116. //小于10的格式化函数
  117. timeFormat(times) {
  118. return Number(times) < 10 ? '0' + times : times;
  119. },
  120. //跳转秒杀商品详情页
  121. toSeckillDetaile: function(e) {
  122. var seckillId = e.currentTarget.dataset.seckillid;
  123. wx.navigateTo({
  124. url: "../snapUpDetail/snapUpDetail?seckillid=" + seckillId,
  125. })
  126. },
  127. /**
  128. * 生命周期函数--监听页面加载
  129. */
  130. onLoad: function(options) {
  131. this.getsklist(); //秒杀商品列表
  132. },
  133. /**
  134. * 生命周期函数--监听页面初次渲染完成
  135. */
  136. onReady: function() {
  137. },
  138. /**
  139. * 生命周期函数--监听页面显示
  140. */
  141. onShow: function() {
  142. },
  143. /**
  144. * 生命周期函数--监听页面隐藏
  145. */
  146. onHide: function() {
  147. },
  148. /**
  149. * 生命周期函数--监听页面卸载
  150. */
  151. onUnload: function() {
  152. clearTimeout(this.data.timer)
  153. },
  154. /**
  155. * 页面相关事件处理函数--监听用户下拉动作
  156. */
  157. onPullDownRefresh: function() {
  158. },
  159. /**
  160. * 页面上拉触底事件的处理函数
  161. */
  162. onReachBottom: function() {
  163. if(this.data.current < this.data.pages) {
  164. this.setData({
  165. current: this.data.current + 1
  166. })
  167. this.getsklist()
  168. }
  169. },
  170. /**
  171. * 用户点击右上角分享
  172. */
  173. onShareAppMessage: function() {
  174. }
  175. })