activity.js 6.2 KB

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