attentionBtn.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const net = require("../../utils/net.js");
  2. const {
  3. api_user_attention,
  4. api_topic_attention,
  5. } = require("../../utils/api.js");
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. isAttention: {
  12. type: Boolean,
  13. value: false,
  14. observer: function (newVal, oldVal) {
  15. // 属性值变化时执行
  16. this.setData({
  17. isLike: newVal,
  18. });
  19. },
  20. },
  21. type: {
  22. type: Number,
  23. value: 1, // 1 是用户,2 是话题
  24. },
  25. attentionId: {
  26. type: Number,
  27. value: 0,
  28. },
  29. index: {
  30. type: Number,
  31. value: 0,
  32. },
  33. },
  34. /**
  35. * 组件的初始数据
  36. */
  37. data: {
  38. isLike: false,
  39. },
  40. /**
  41. * 组件的方法列表
  42. */
  43. methods: {
  44. tapAttentionAction(e) {
  45. const { type, attentionId, index } = this.properties;
  46. const userInfo = wx.getStorageSync("USERINFO");
  47. if (userInfo) {
  48. let params = {};
  49. if (parseInt(type) == 1) {
  50. params.url = api_user_attention;
  51. params.data = {
  52. targetUserId: attentionId,
  53. };
  54. } else {
  55. params.url = api_topic_attention;
  56. params.data = {
  57. id: attentionId,
  58. };
  59. }
  60. net.req(params, true).then(
  61. (data) => {
  62. const { isAttention, attentionNum } = data;
  63. this.setData({
  64. isLike: !!isAttention,
  65. });
  66. this.triggerEvent("attention", {
  67. index: index,
  68. isAttention: isAttention,
  69. attentionNum: attentionNum,
  70. });
  71. },
  72. (e) => {
  73. wx.showToast({
  74. title: e.msg,
  75. });
  76. }
  77. );
  78. } else {
  79. wx.navigateTo({ url: `/pages/login/login` });
  80. }
  81. },
  82. },
  83. });