itemmyLikePublisher.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. getPublisherInfo: function () {
  14. wx.navigateTo({
  15. url: "/pages/publisher/publisher?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: "publisher",
  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: "publisher",
  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. })