activity.js 6.9 KB

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