seckillActivity.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // subPackagesF/pages/seckillActivity/seckillActivity.js
  2. import {
  3. get,
  4. } from '../../../utils/http'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. store_id: '',
  11. startTime: '', //开始时间
  12. time: 60 * 60 * 1000, //秒杀开始时间
  13. timeData: {}, //秒杀倒计时
  14. activeClass: '', //动态类名是否超出时间或者没到时间
  15. seckillList: [], //秒杀列表
  16. end: false, //时间结束
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad(options) {
  22. this.setData({
  23. store_id: wx.getStorageSync('store_id')
  24. })
  25. // 获取秒杀列表
  26. this.seckillList()
  27. },
  28. /**
  29. * 生命周期函数--监听页面初次渲染完成
  30. */
  31. onReady() {
  32. },
  33. /**
  34. * 生命周期函数--监听页面显示
  35. */
  36. onShow() {
  37. },
  38. // 获取秒杀列表
  39. seckillList() {
  40. get('v2/api/spike/list', {
  41. store_id: this.data.store_id || wx.getStorageSync('store_id')
  42. }, (res => {
  43. if (res.code == 200) {
  44. // res.data.end_time= "01-10 16:45"
  45. res.data.start_time = (new Date()).getFullYear() + '-' + res.data.start_time
  46. res.data.end_time = (new Date()).getFullYear() + '-' + res.data.end_time
  47. let startDate = res.data.start_time.replace(new RegExp("-", "gm"), "/");
  48. let endDate = res.data.end_time.replace(new RegExp("-", "gm"), "/");
  49. let startDateM = (new Date(startDate)).getTime(); //开始时间毫秒数
  50. let endDateM = (new Date(endDate)).getTime(); //结束时间毫秒数
  51. let nowTime = Date.now(); //当前时间毫秒数
  52. let time = Number(startDateM) - Number(nowTime) //大于0活动未开始
  53. // 如果当前时间比结束时间大(活动结束)
  54. if (nowTime > endDateM) {
  55. this.setData({
  56. activeClass: 'activeClass',
  57. end: true
  58. })
  59. time = 0
  60. } else {
  61. // 否则就判断时间是否开始
  62. if (Number(time) > 0) {
  63. this.setData({
  64. activeClass: 'activeClass'
  65. })
  66. } else {
  67. this.setData({
  68. activeClass: ''
  69. })
  70. time = 0
  71. }
  72. }
  73. this.setData({
  74. seckillList: res.data,
  75. startTime: res.data.start_time,
  76. time
  77. })
  78. }
  79. }))
  80. },
  81. // 倒计时改变
  82. onChange(e) {
  83. let time = e.detail
  84. if (time.days > 0) {
  85. e.detail.hours = Number(time.days) * 24 + Number(time.hours)
  86. }
  87. time.hours1 = 0
  88. time.minutes1 = 0
  89. time.seconds1 = 0
  90. if (time.seconds >= 10) {
  91. time.seconds1 = time.seconds.toString().charAt(0)
  92. time.seconds = time.seconds.toString().charAt(1)
  93. }
  94. if (time.hours >= 10) {
  95. if (time.hours >= 100) {
  96. time.hours2 = time.hours.toString().charAt(2)
  97. }
  98. time.hours1 = time.hours.toString().charAt(0)
  99. time.hours = time.hours.toString().charAt(1)
  100. }
  101. if (time.minutes >= 10) {
  102. time.minutes1 = time.minutes.toString().charAt(0)
  103. time.minutes = time.minutes.toString().charAt(1)
  104. }
  105. this.setData({
  106. timeData: time,
  107. });
  108. },
  109. // 倒计时结束
  110. finishTime() {
  111. // 活动时间结束不触发
  112. if (!this.data.end) {
  113. this.setData({
  114. activeClass: '',
  115. })
  116. }
  117. if(this.data.activeClass==''){
  118. let endDate = this.data.seckillList.end_time.replace(new RegExp("-", "gm"), "/");
  119. let endDateM = (new Date(endDate)).getTime(); //结束时间毫秒数
  120. let nowTime = Date.now(); //当前时间毫秒数
  121. let time = endDateM-nowTime
  122. if(time>0){
  123. this.setData({
  124. time
  125. })
  126. }else{
  127. this.setData({
  128. time:0,
  129. end:true,
  130. activeClass: 'activeClass',
  131. })
  132. }
  133. }
  134. },
  135. // 跳转到秒杀详情
  136. goSeckillDetail(e) {
  137. console.log(e);
  138. let id = e.currentTarget.dataset.id
  139. let num = e.currentTarget.dataset.num
  140. let seckillId = e.currentTarget.dataset.seckillid
  141. if (num <= 0) {
  142. wx.showToast({
  143. title: '抱歉,没有库存了',
  144. icon: 'none'
  145. })
  146. return
  147. }
  148. wx.navigateTo({
  149. url: `/subPackagesF/pages/seckillDetail/seckillDetail?goods_id=${id}&seckill_id=${seckillId}`,
  150. })
  151. },
  152. /**
  153. * 生命周期函数--监听页面隐藏
  154. */
  155. onHide() {
  156. },
  157. /**
  158. * 生命周期函数--监听页面卸载
  159. */
  160. onUnload() {
  161. },
  162. /**
  163. * 页面相关事件处理函数--监听用户下拉动作
  164. */
  165. onPullDownRefresh() {
  166. },
  167. /**
  168. * 页面上拉触底事件的处理函数
  169. */
  170. onReachBottom() {
  171. },
  172. /**
  173. * 用户点击右上角分享
  174. */
  175. onShareAppMessage() {
  176. }
  177. })