activity.js 5.5 KB

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