activity.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.pub_id
  17. })
  18. },
  19. editActivity: function () {
  20. wx.navigateTo({
  21. url: '/pages/activityPublish/activityPublish?id=' + this.data.activityInfo.pub_id +
  22. '&msg_id=' + this.data.activityInfo._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. wx.showLoading({
  84. title: '发送中'
  85. })
  86. wx.cloud.callFunction({
  87. name: 'createQuestion',
  88. data: {
  89. msg_id: this.data.activityInfo._id,
  90. question: this.data.commentText
  91. }
  92. }).then(res => {
  93. wx.hideLoading()
  94. wx.showToast({
  95. title: '发送成功,请等待发布者回复',
  96. icon: 'none'
  97. })
  98. this.setData({
  99. commentText: ''
  100. })
  101. })
  102. // const db = wx.cloud.database()
  103. // db.collection('qaData').where({
  104. // _openid: app.globalData.openId,
  105. // activityId: this.data.activityInfo._id
  106. // }).get({
  107. // success: function (res) {
  108. // console.log(res)
  109. // if (res.data.length >= 10) {
  110. // wx.showToast({
  111. // title: '为防止刷屏,每人每消息至多提问10条',
  112. // icon: 'none'
  113. // })
  114. // } else {
  115. // db.collection('qaData').add({
  116. // data: {
  117. // activityId: this.data.activityInfo._id,
  118. // publisherId: this.data.activityInfo.publisherId,
  119. // answer: '',
  120. // answerTime: '',
  121. // question: this.data.commentText,
  122. // questionTime: new Date(),
  123. // rank: ''
  124. // },
  125. // success: function () {
  126. // this.setData({
  127. // commentText: ''
  128. // })
  129. // wx.showToast({
  130. // title: '提问成功'
  131. // })
  132. // }.bind(this)
  133. // })
  134. // }
  135. // }.bind(this)
  136. // })
  137. }
  138. },
  139. /**
  140. * 生命周期函数--监听页面加载
  141. */
  142. onLoad: function (options) {
  143. wx.showLoading({
  144. title: '加载中'
  145. })
  146. wx.cloud.callFunction({
  147. name: 'getMessage',
  148. data: {
  149. msg_id: options.id
  150. }
  151. }).then(res => {
  152. wx.hideLoading()
  153. res.result.list[0] = util.dbToMsg(res.result.list[0])
  154. this.setData({
  155. activityInfo: res.result.list[0]
  156. })
  157. })
  158. // const db = wx.cloud.database()
  159. // const _ = db.command
  160. // db.collection('mainData').doc(options.id).get({
  161. // success: function (res) {
  162. // res.data.time = util.handleDate(res.data.time)
  163. // this.setData({
  164. // activityInfo: res.data
  165. // })
  166. // wx.hideLoading()
  167. // }.bind(this)
  168. // })
  169. // db.collection('qaData').where({
  170. // activityId: options.id,
  171. // answer: _.neq('')
  172. // }).get({
  173. // success: function (res) {
  174. // for (let i = 0; i < res.data.length; i++) {
  175. // if (res.data[i].answerTime !== '') {
  176. // res.data[i].time = util.handleDate(res.data[i].answerTime)
  177. // } else {
  178. // res.data[i].time = ''
  179. // }
  180. // }
  181. // this.setData({
  182. // activityComment: res.data
  183. // })
  184. // }.bind(this)
  185. // })
  186. // db.collection('likeData').where({
  187. // type: 'message',
  188. // id: options.id
  189. // }).get({
  190. // success: function (res) {
  191. // this.setData({
  192. // like: res.data.length
  193. // })
  194. // }.bind(this)
  195. // })
  196. },
  197. /**
  198. * 生命周期函数--监听页面初次渲染完成
  199. */
  200. onReady: function () {
  201. },
  202. /**
  203. * 生命周期函数--监听页面显示
  204. */
  205. onShow: function () {
  206. },
  207. /**
  208. * 生命周期函数--监听页面隐藏
  209. */
  210. onHide: function () {
  211. },
  212. /**
  213. * 生命周期函数--监听页面卸载
  214. */
  215. onUnload: function () {
  216. },
  217. /**
  218. * 页面相关事件处理函数--监听用户下拉动作
  219. */
  220. onPullDownRefresh: function () {
  221. },
  222. /**
  223. * 页面上拉触底事件的处理函数
  224. */
  225. onReachBottom: function () {
  226. },
  227. /**
  228. * 用户点击右上角分享
  229. */
  230. onShareAppMessage: function () {
  231. }
  232. })