publisher.js 4.2 KB

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