publisher.js 4.6 KB

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