publisher.js 4.3 KB

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