activity.js 5.6 KB

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