index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 = ""
  87. }
  88. if (v.timeType == 1 || endTime == null) {
  89. endTime = ""
  90. }
  91. beginTime && (v._beginTime = parseTime(beginTime, "{y}.{m}.{d} {h}:{i}:{s}"))
  92. endTime && (v._endTime = parseTime(endTime, "{y}.{m}.{d} {h}:{i}:{s}"))
  93. if (v.activityTitle && v.activityTitle.length > 20) {
  94. v.activityTitle = v.activityTitle.substr(0, 20) + "..."
  95. }
  96. })
  97. this.data.activityList = this.data.activityList.concat(...data)
  98. this.setData({
  99. activityList: this.data.activityList
  100. })
  101. this.calcActivityCountdown()
  102. },
  103. /**
  104. * 页面上拉触底事件的处理函数
  105. */
  106. onReachBottom: function () {
  107. if (this.data.lock || this.data.noMore) {
  108. return
  109. }
  110. this.data.lock = true
  111. this.data.page++
  112. this.getActivityList()
  113. },
  114. toDetail(e) {
  115. const url = "detail?id=" + e.currentTarget.dataset.id
  116. wx.redirectTo({
  117. url
  118. })
  119. },
  120. startCountdownTimer() {
  121. setInterval(this.calcActivityCountdown, 1000)
  122. },
  123. calcActivityCountdown() {
  124. // 当前时间毫秒数
  125. if (this.data.activityList == null || this.data.activityList.length == 0) {
  126. return
  127. }
  128. const now = new Date();
  129. this.data.activityList.forEach(v => {
  130. if (v.status == 1 || v.status == 4 || v.status == 5) {
  131. this.setActivityCountdownTime(now, v)
  132. }
  133. })
  134. this.setData({
  135. activityList: this.data.activityList
  136. })
  137. }
  138. })