publisher.js 4.3 KB

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