publisher.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. loading: 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.loading) {
  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. this.setData({
  62. loading: true
  63. })
  64. wx.showNavigationBarLoading()
  65. const arr = []
  66. arr.push(wx.cloud.callFunction({
  67. name: 'listMessages',
  68. data: {
  69. pub_id: this.data.publisherId,
  70. page_token: this.data.pageToken,
  71. page_size: 20
  72. }
  73. }))
  74. arr.push(wx.cloud.callFunction({
  75. name: 'getFollow',
  76. data: {
  77. pub_id: this.data.publisherId
  78. }
  79. }))
  80. Promise.all(arr).then(res => {
  81. wx.hideNavigationBarLoading()
  82. if (res[0].result.status !== 'OK' || res[1].result.status !== 'OK') {
  83. wx.showToast({
  84. title: res[0].result.errMsg || res[1].result.errMsg,
  85. icon: 'none'
  86. })
  87. return
  88. }
  89. for (let i = 0; i < res[0].result.list.length; i++) {
  90. res[0].result.list[i] = util.dbToMsg(res[0].result.list[i])
  91. res[0].result.list[i].publisher = this.data.publisherInfo
  92. }
  93. this.setData({
  94. mainDatas: this.data.mainDatas.concat(res[0].result.list),
  95. pageToken: res[0].result.next_page_token,
  96. like: res[1].result.total === 1,
  97. loading: false
  98. })
  99. })
  100. },
  101. /**
  102. * 生命周期函数--监听页面加载
  103. */
  104. onLoad: function (options) {
  105. this.setData({
  106. hasUserInfo: app.globalData.hasUserInfo
  107. })
  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.once('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. if (this.data.loading) {
  175. return
  176. }
  177. this.loadExtraData()
  178. },
  179. /**
  180. * 用户点击右上角分享
  181. */
  182. onShareAppMessage: function () {
  183. }
  184. })