publisher.js 4.0 KB

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