publisher.js 3.3 KB

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