publisher.js 4.1 KB

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