publisher.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. publisherInfo: [],
  7. mainDatas: [],
  8. like: -1,
  9. likeEnable: true
  10. },
  11. handleDate: function(date) {
  12. var now = new Date().getTime()
  13. var diffValue = now - date.getTime()
  14. if (diffValue < 0) {
  15. console.log("时间不同步")
  16. return "刚刚"
  17. }
  18. var result = ""
  19. var minute = 1000 * 60
  20. var hour = minute * 60
  21. var day = hour * 24
  22. var minC = diffValue / minute
  23. var hourC = diffValue / hour
  24. var dayC = diffValue / day
  25. if (parseInt(dayC) > 30) {
  26. result += date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate()
  27. } else if (parseInt(dayC) > 1) {
  28. result += parseInt(dayC) + "天前"
  29. } else if (parseInt(dayC) == 1) {
  30. result += "昨天"
  31. } else if (hourC >= 1) {
  32. result += parseInt(hourC) + "小时前"
  33. } else if (minC >= 5) {
  34. result += parseInt(minC) + "分钟前"
  35. } else {
  36. result += "刚刚"
  37. }
  38. return result
  39. },
  40. processData: function (start, arr) {
  41. for (let i = start; i < arr.length; i++) {
  42. arr[i].time = this.handleDate(arr[i].time)
  43. }
  44. this.setData({
  45. mainDatas: arr
  46. })
  47. wx.hideLoading()
  48. },
  49. toggleLike: function () {
  50. if (!this.data.likeEnable) return
  51. this.setData({
  52. likeEnable: false
  53. })
  54. const db = wx.cloud.database()
  55. if (this.data.like == 0) {
  56. db.collection("likeData").add({
  57. data: {
  58. type: "publisher",
  59. id: this.data.publisherInfo._id
  60. },
  61. success: function() {
  62. this.setData({
  63. like: 1,
  64. likeEnable: true
  65. })
  66. wx.showToast({
  67. title: "已关注",
  68. })
  69. }.bind(this),
  70. fail: function() {
  71. wx.showToast({
  72. title: "网络错误",
  73. icon: "none"
  74. })
  75. }
  76. })
  77. } else {
  78. db.collection("likeData").where({
  79. type: "publisher",
  80. id: this.data.publisherInfo._id
  81. }).remove({
  82. success: function() {
  83. this.setData({
  84. like: 0,
  85. likeEnable: true
  86. })
  87. wx.showToast({
  88. title: "已取消关注",
  89. })
  90. }.bind(this),
  91. fail: function() {
  92. wx.showToast({
  93. title: "网络错误",
  94. icon: "none"
  95. })
  96. }
  97. })
  98. }
  99. },
  100. /**
  101. * 生命周期函数--监听页面加载
  102. */
  103. onLoad: function (options) {
  104. wx.showLoading({
  105. title: "加载中"
  106. })
  107. const db = wx.cloud.database()
  108. db.collection("publisherInfoData").doc(options.id).get({
  109. success: function (res) {
  110. this.setData({
  111. publisherInfo: res.data
  112. })
  113. db.collection("mainData").where({
  114. publisherName: this.data.publisherInfo.publisherName
  115. }).orderBy("time", "desc").limit(20).get({
  116. success: function (res) {
  117. this.processData(0, res.data)
  118. }.bind(this)
  119. })
  120. }.bind(this)
  121. })
  122. db.collection("likeData").where({
  123. type: "publisher",
  124. id: options.id
  125. }).get({
  126. success: function(res) {
  127. this.setData({
  128. like: res.data.length
  129. })
  130. }.bind(this)
  131. })
  132. },
  133. /**
  134. * 生命周期函数--监听页面初次渲染完成
  135. */
  136. onReady: function () {
  137. },
  138. /**
  139. * 生命周期函数--监听页面显示
  140. */
  141. onShow: function () {
  142. },
  143. /**
  144. * 生命周期函数--监听页面隐藏
  145. */
  146. onHide: function () {
  147. },
  148. /**
  149. * 生命周期函数--监听页面卸载
  150. */
  151. onUnload: function () {
  152. },
  153. /**
  154. * 页面相关事件处理函数--监听用户下拉动作
  155. */
  156. onPullDownRefresh: function () {
  157. },
  158. /**
  159. * 页面上拉触底事件的处理函数
  160. */
  161. onReachBottom: function () {
  162. },
  163. /**
  164. * 用户点击右上角分享
  165. */
  166. onShareAppMessage: function () {
  167. }
  168. })