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