activity.js 6.4 KB

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