activity.js 6.6 KB

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