activity.js 5.2 KB

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