publisher.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const util = require('../../utils/util.js')
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. publisherInfo: [],
  8. mainDatas: [],
  9. like: -1,
  10. likeEnable: true
  11. },
  12. processData: function (start, arr) {
  13. for (let i = start; i < arr.length; i++) {
  14. arr[i].time = util.handleDate(arr[i].time)
  15. }
  16. this.setData({
  17. mainDatas: arr
  18. })
  19. wx.hideLoading()
  20. },
  21. toggleLike: function () {
  22. if (!this.data.likeEnable) return
  23. this.setData({
  24. likeEnable: false
  25. })
  26. const db = wx.cloud.database()
  27. if (this.data.like == 0) {
  28. db.collection("likeData").add({
  29. data: {
  30. type: "publisher",
  31. id: this.data.publisherInfo._id
  32. },
  33. success: function () {
  34. this.setData({
  35. like: 1,
  36. likeEnable: true
  37. })
  38. wx.showToast({
  39. title: "已关注",
  40. })
  41. }.bind(this),
  42. fail: function () {
  43. wx.showToast({
  44. title: "网络错误",
  45. icon: "none"
  46. })
  47. }
  48. })
  49. } else {
  50. db.collection("likeData").where({
  51. type: "publisher",
  52. id: this.data.publisherInfo._id
  53. }).remove({
  54. success: function () {
  55. this.setData({
  56. like: 0,
  57. likeEnable: true
  58. })
  59. wx.showToast({
  60. title: "已取消关注",
  61. })
  62. }.bind(this),
  63. fail: function () {
  64. wx.showToast({
  65. title: "网络错误",
  66. icon: "none"
  67. })
  68. }
  69. })
  70. }
  71. },
  72. /**
  73. * 生命周期函数--监听页面加载
  74. */
  75. onLoad: function (options) {
  76. wx.showLoading({
  77. title: "加载中"
  78. })
  79. const db = wx.cloud.database()
  80. db.collection("publisherInfoData").doc(options.id).get({
  81. success: function (res) {
  82. this.setData({
  83. publisherInfo: res.data
  84. })
  85. db.collection("mainData").where({
  86. publisherName: this.data.publisherInfo.publisherName
  87. }).orderBy("time", "desc").limit(20).get({
  88. success: function (res) {
  89. this.processData(0, res.data)
  90. }.bind(this)
  91. })
  92. }.bind(this)
  93. })
  94. db.collection("likeData").where({
  95. type: "publisher",
  96. id: options.id
  97. }).get({
  98. success: function (res) {
  99. this.setData({
  100. like: res.data.length
  101. })
  102. }.bind(this)
  103. })
  104. },
  105. /**
  106. * 生命周期函数--监听页面初次渲染完成
  107. */
  108. onReady: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面显示
  112. */
  113. onShow: function () {
  114. },
  115. /**
  116. * 生命周期函数--监听页面隐藏
  117. */
  118. onHide: function () {
  119. },
  120. /**
  121. * 生命周期函数--监听页面卸载
  122. */
  123. onUnload: function () {
  124. },
  125. /**
  126. * 页面相关事件处理函数--监听用户下拉动作
  127. */
  128. onPullDownRefresh: function () {
  129. },
  130. /**
  131. * 页面上拉触底事件的处理函数
  132. */
  133. onReachBottom: function () {
  134. },
  135. /**
  136. * 用户点击右上角分享
  137. */
  138. onShareAppMessage: function () {
  139. }
  140. })