index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // pages/luckDraw/index.js
  2. import LuckDraw from '../../api/luck-draw'
  3. import { parseTime } from '../../utils/util';
  4. import Common from './common'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. page: 1,
  11. pageSize: 10,
  12. lock: false,
  13. noResult: false,
  14. noMore: false,
  15. activityList: []
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. Object.assign(this, Common)
  22. this.getActivityList();
  23. this.startCountdownTimer()
  24. },
  25. /**
  26. * 生命周期函数--监听页面初次渲染完成
  27. */
  28. onReady: function () {
  29. },
  30. /**
  31. * 生命周期函数--监听页面显示
  32. */
  33. onShow: function () {
  34. },
  35. /**
  36. * 生命周期函数--监听页面隐藏
  37. */
  38. onHide: function () {
  39. },
  40. /**
  41. * 生命周期函数--监听页面卸载
  42. */
  43. onUnload: function () {
  44. },
  45. /**
  46. * 页面相关事件处理函数--监听用户下拉动作
  47. */
  48. onPullDownRefresh: function () {
  49. },
  50. /**
  51. * 用户点击右上角分享
  52. */
  53. onShareAppMessage: function () {
  54. },
  55. getActivityList: function() {
  56. LuckDraw.getActivityList({
  57. page: this.data.page,
  58. pageSize: this.data.pageSize
  59. }).then(res => {
  60. if (res.code == 200) {
  61. this.drawListView(res.data)
  62. }
  63. this.data.lock = false
  64. }).catch(_ => {
  65. this.data.lock = false
  66. })
  67. },
  68. drawListView: function(data) {
  69. if (!Array.isArray(data) || data.length == 0) {
  70. console.log("没有获取到数据");
  71. if (this.data.page == 1) {
  72. this.setData({
  73. noResult: true
  74. })
  75. } else {
  76. this.setData({
  77. noMore: true
  78. })
  79. }
  80. return
  81. }
  82. data.forEach(v => {
  83. let beginTime = v.beginTime
  84. let endTime = v.endTime
  85. if (v.timeType == 1 || beginTime == null) {
  86. beginTime = "2021.11.01"
  87. }
  88. if (v.timeType == 1 || endTime == null) {
  89. endTime = "2031.11.01"
  90. }
  91. v._beginTime = parseTime(beginTime, "{y}.{m}.{d}")
  92. v._endTime = parseTime(endTime, "{y}.{m}.{d}")
  93. })
  94. this.data.activityList = this.data.activityList.concat(...data)
  95. this.setData({
  96. activityList: this.data.activityList
  97. })
  98. this.calcActivityCountdown()
  99. },
  100. /**
  101. * 页面上拉触底事件的处理函数
  102. */
  103. onReachBottom: function () {
  104. if (this.data.lock || this.data.noMore) {
  105. return
  106. }
  107. this.data.lock = true
  108. this.data.page++
  109. this.getActivityList()
  110. },
  111. toDetail(e) {
  112. const url = "detail?id=" + e.currentTarget.dataset.id
  113. wx.redirectTo({
  114. url
  115. })
  116. },
  117. startCountdownTimer() {
  118. setInterval(this.calcActivityCountdown, 1000)
  119. },
  120. calcActivityCountdown() {
  121. // 当前时间毫秒数
  122. if (this.data.activityList == null || this.data.activityList.length == 0) {
  123. return
  124. }
  125. const now = new Date();
  126. this.data.activityList.forEach(v => {
  127. if (v.status == 1 || v.status == 4 || v.status == 5) {
  128. this.setActivityCountdownTime(now, v)
  129. }
  130. })
  131. this.setData({
  132. activityList: this.data.activityList
  133. })
  134. }
  135. })