index.js 3.7 KB

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