publisher.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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: false,
  12. likeEnable: true
  13. },
  14. detail: function () {
  15. wx.navigateTo({
  16. url: '/pages/publisherDetail/publisherDetail?id=' + this.data.publisherId,
  17. })
  18. },
  19. loadExtraData: function () {
  20. wx.showLoading({
  21. title: '加载中'
  22. })
  23. wx.cloud.callFunction({
  24. name: 'listMessages',
  25. data: {
  26. pub_id: this.data.publisherId,
  27. page_token: this.data.pageToken,
  28. page_size: 25
  29. }
  30. }).then(res => {
  31. wx.hideLoading()
  32. if (res.result.status !== 'OK') {
  33. wx.showToast({
  34. title: res.result.errMsg,
  35. icon: 'none'
  36. })
  37. return
  38. }
  39. for (let i = 0; i < res.result.list.length; i++) {
  40. res.result.list[i] = util.dbToMsg(res.result.list[i])
  41. res.result.list[i].publisher = this.data.publisherInfo
  42. }
  43. this.setData({
  44. mainDatas: this.data.mainDatas.concat(res.result.list),
  45. pageToken: res.result.next_page_token
  46. })
  47. })
  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. if (options.id) {
  105. this.setData({
  106. publisherId: options.id
  107. })
  108. wx.showLoading({
  109. title: "加载中"
  110. })
  111. wx.cloud.callFunction({
  112. name: 'getPublisher',
  113. data: {
  114. pub_id: this.data.publisherId
  115. }
  116. }).then(res => {
  117. wx.hideLoading()
  118. if (res.result.status !== 'OK') {
  119. wx.showToast({
  120. title: res.result.errMsg,
  121. icon: 'none'
  122. })
  123. return
  124. }
  125. this.setData({
  126. publisherInfo: res.result.data
  127. })
  128. this.loadExtraData()
  129. })
  130. } else {
  131. const eventChannel = this.getOpenerEventChannel()
  132. eventChannel.on('loadCommonData', res => {
  133. this.setData({
  134. publisherId: res.data._id,
  135. publisherInfo: res.data
  136. })
  137. this.loadExtraData()
  138. })
  139. }
  140. },
  141. /**
  142. * 生命周期函数--监听页面初次渲染完成
  143. */
  144. onReady: function () {
  145. },
  146. /**
  147. * 生命周期函数--监听页面显示
  148. */
  149. onShow: function () {
  150. },
  151. /**
  152. * 生命周期函数--监听页面隐藏
  153. */
  154. onHide: function () {
  155. },
  156. /**
  157. * 生命周期函数--监听页面卸载
  158. */
  159. onUnload: function () {
  160. },
  161. /**
  162. * 页面相关事件处理函数--监听用户下拉动作
  163. */
  164. onPullDownRefresh: function () {
  165. },
  166. /**
  167. * 页面上拉触底事件的处理函数
  168. */
  169. onReachBottom: function () {
  170. this.loadMessageData()
  171. },
  172. /**
  173. * 用户点击右上角分享
  174. */
  175. onShareAppMessage: function () {
  176. }
  177. })