activity.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. const app = getApp()
  2. const util = require('../../utils/util.js')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. activityInfo: {},
  9. activityComment: [],
  10. like: -1,
  11. likeEnable: true,
  12. commentText: ""
  13. },
  14. getPublisherInfo: function () {
  15. wx.navigateTo({
  16. url: "/pages/publisher/publisher?id=" + this.data.activityInfo.publisherId
  17. })
  18. },
  19. toggleLike: function () {
  20. if (!this.data.likeEnable) return
  21. this.setData({
  22. likeEnable: false
  23. })
  24. const db = wx.cloud.database()
  25. if (this.data.like == 0) {
  26. db.collection("likeData").add({
  27. data: {
  28. type: "message",
  29. id: this.data.activityInfo._id
  30. },
  31. success: function () {
  32. this.setData({
  33. like: 1,
  34. likeEnable: true
  35. })
  36. wx.showToast({
  37. title: "已收藏",
  38. })
  39. }.bind(this),
  40. fail: function () {
  41. wx.showToast({
  42. title: "网络错误",
  43. icon: "none"
  44. })
  45. }
  46. })
  47. } else {
  48. db.collection("likeData").where({
  49. type: "message",
  50. id: this.data.activityInfo._id
  51. }).remove({
  52. success: function () {
  53. this.setData({
  54. like: 0,
  55. likeEnable: true
  56. })
  57. wx.showToast({
  58. title: "已取消收藏",
  59. })
  60. }.bind(this),
  61. fail: function () {
  62. wx.showToast({
  63. title: "网络错误",
  64. icon: "none"
  65. })
  66. }
  67. })
  68. }
  69. },
  70. comment: function () {
  71. if (this.data.commentText.length < 5) {
  72. wx.showToast({
  73. title: "提问字数至少为5",
  74. icon: "none"
  75. })
  76. } else {
  77. const db = wx.cloud.database()
  78. db.collection("qaData").where({
  79. _openid: app.globalData.openId,
  80. activityId: this.data.activityInfo._id
  81. }).get({
  82. success: function (res) {
  83. console.log(res)
  84. if (res.data.length >= 10) {
  85. wx.showToast({
  86. title: "为防止刷屏,每人每消息至多提问10条",
  87. icon: "none"
  88. })
  89. } else {
  90. db.collection("qaData").add({
  91. data: {
  92. activityId: this.data.activityInfo._id,
  93. publisherId: this.data.activityInfo.publisherId,
  94. answer: "",
  95. answerTime: "",
  96. question: this.data.commentText,
  97. questionTime: new Date(),
  98. rank: ""
  99. },
  100. success: function () {
  101. this.setData({
  102. commentText: ""
  103. })
  104. wx.showToast({
  105. title: "提问成功"
  106. })
  107. }.bind(this)
  108. })
  109. }
  110. }.bind(this)
  111. })
  112. }
  113. },
  114. /**
  115. * 生命周期函数--监听页面加载
  116. */
  117. onLoad: function (options) {
  118. wx.showLoading({
  119. title: "加载中",
  120. })
  121. const db = wx.cloud.database()
  122. const _ = db.command
  123. db.collection("mainData").doc(options.id).get({
  124. success: function (res) {
  125. this.setData({
  126. activityInfo: res.data
  127. })
  128. wx.hideLoading()
  129. }.bind(this)
  130. })
  131. db.collection("qaData").where({
  132. activityId: options.id,
  133. answer: _.neq("")
  134. }).get({
  135. success: function (res) {
  136. for (let i = 0; i < res.data.length; i++) {
  137. if (res.data[i].answerTime != "") {
  138. res.data[i].time = util.handleDate(res.data[i].answerTime)
  139. } else {
  140. res.data[i].time = ""
  141. }
  142. }
  143. this.setData({
  144. activityComment: res.data
  145. })
  146. }.bind(this)
  147. })
  148. db.collection("likeData").where({
  149. type: "message",
  150. id: options.id
  151. }).get({
  152. success: function (res) {
  153. this.setData({
  154. like: res.data.length
  155. })
  156. }.bind(this)
  157. })
  158. },
  159. /**
  160. * 生命周期函数--监听页面初次渲染完成
  161. */
  162. onReady: function () {
  163. },
  164. /**
  165. * 生命周期函数--监听页面显示
  166. */
  167. onShow: function () {
  168. },
  169. /**
  170. * 生命周期函数--监听页面隐藏
  171. */
  172. onHide: function () {
  173. },
  174. /**
  175. * 生命周期函数--监听页面卸载
  176. */
  177. onUnload: function () {
  178. },
  179. /**
  180. * 页面相关事件处理函数--监听用户下拉动作
  181. */
  182. onPullDownRefresh: function () {
  183. },
  184. /**
  185. * 页面上拉触底事件的处理函数
  186. */
  187. onReachBottom: function () {
  188. },
  189. /**
  190. * 用户点击右上角分享
  191. */
  192. onShareAppMessage: function () {
  193. }
  194. })