itemmyLikeMessage.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. Component({
  2. options: {
  3. styleIsolation: "apply-shared"
  4. },
  5. properties: {
  6. item: Object
  7. },
  8. data: {
  9. like: 1,
  10. likeEnable: true
  11. },
  12. methods: {
  13. getActivityInfo: function () {
  14. wx.navigateTo({
  15. url: "/pages/activity/activity?id=" + this.properties.item.id
  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.properties.item.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.properties.item.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. }
  70. })