publisher.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. const util = require('../../utils/util.js')
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. publisherId: '',
  8. publisherInfo: {},
  9. pageToken: 0,
  10. mainDatas: [],
  11. like: -1,
  12. likeEnable: true
  13. },
  14. detail: function () {
  15. wx.navigateTo({
  16. url: '/pages/publisherDetail/publisherDetail?id=' + this.data.publisherId,
  17. })
  18. },
  19. loadMessageData: function () {
  20. wx.showNavigationBarLoading()
  21. wx.cloud.callFunction({
  22. name: 'listMessages',
  23. data: {
  24. pub_id: this.data.publisherId,
  25. page_token: this.data.pageToken,
  26. page_size: 25
  27. }
  28. }).then(res => {
  29. wx.hideNavigationBarLoading()
  30. for (let i = 0; i < res.result.list.length; i++) {
  31. res.result.list[i] = util.dbToMsg(res.result.list[i])
  32. res.result.list[i].publisher = [this.data.publisherInfo]
  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. this.setData({
  96. publisherId: options.id
  97. })
  98. wx.showLoading({
  99. title: "加载中"
  100. })
  101. wx.cloud.callFunction({
  102. name: 'getPublisher',
  103. data: {
  104. pub_id: this.data.publisherId
  105. }
  106. }).then(res => {
  107. wx.hideLoading()
  108. this.setData({
  109. publisherInfo: res.result.data
  110. })
  111. this.loadMessageData()
  112. })
  113. // const db = wx.cloud.database()
  114. // db.collection("publisherInfoData").doc(options.id).get({
  115. // success: function (res) {
  116. // this.setData({
  117. // publisherInfo: res.data
  118. // })
  119. // db.collection("mainData").where({
  120. // publisherName: this.data.publisherInfo.publisherName
  121. // }).orderBy("time", "desc").limit(20).get({
  122. // success: function (res) {
  123. // this.processData(0, res.data)
  124. // }.bind(this)
  125. // })
  126. // }.bind(this)
  127. // })
  128. // db.collection("likeData").where({
  129. // type: "publisher",
  130. // id: options.id
  131. // }).get({
  132. // success: function (res) {
  133. // this.setData({
  134. // like: res.data.length
  135. // })
  136. // }.bind(this)
  137. // })
  138. },
  139. /**
  140. * 生命周期函数--监听页面初次渲染完成
  141. */
  142. onReady: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面显示
  146. */
  147. onShow: function () {
  148. },
  149. /**
  150. * 生命周期函数--监听页面隐藏
  151. */
  152. onHide: function () {
  153. },
  154. /**
  155. * 生命周期函数--监听页面卸载
  156. */
  157. onUnload: function () {
  158. },
  159. /**
  160. * 页面相关事件处理函数--监听用户下拉动作
  161. */
  162. onPullDownRefresh: function () {
  163. },
  164. /**
  165. * 页面上拉触底事件的处理函数
  166. */
  167. onReachBottom: function () {
  168. this.loadMessageData()
  169. },
  170. /**
  171. * 用户点击右上角分享
  172. */
  173. onShareAppMessage: function () {
  174. }
  175. })