likeComponent.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <view>
  3. <view
  4. class=""
  5. style="display: flex; align-items: center; justify-content: space-evenly; height: 40rpx; width: 100rpx; border-radius: 30rpx; background-color: rgb(253 0 0 / 60%)"
  6. @click="likeBtn"
  7. >
  8. <uni-icons v-if="list.is_like > 0" type="heart-filled" color="red" size="20"></uni-icons>
  9. <uni-icons v-else type="heart-filled" color="#f8f8f8" size="20"></uni-icons>
  10. <text style="font-size: 28rpx">{{ list.likes }}</text>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import { like, unLike } from '@/config/api.js';
  16. let that;
  17. export default {
  18. name: 'likeComponent',
  19. props: {
  20. list: {
  21. type: [Object, Array],
  22. default: null
  23. }
  24. },
  25. data() {
  26. return {
  27. main_body_id: ''
  28. };
  29. },
  30. created() {
  31. that = this;
  32. this.main_body_id = this.$db.get('main_body_id');
  33. },
  34. onLoad(e) {},
  35. methods: {
  36. // 点赞
  37. async likeBtn(i) {
  38. if (this.list.is_like == 0) {
  39. await like(
  40. {
  41. main_body_id: this.main_body_id,
  42. content_id: this.list.id
  43. },
  44. function (res) {
  45. that.$nextTick(() => {
  46. that.list.is_like = 1;
  47. that.list.likes++;
  48. });
  49. that.$common.successToShow('成功点赞');
  50. }
  51. );
  52. } else {
  53. await unLike(
  54. {
  55. main_body_id: that.main_body_id,
  56. content_ids: that.list.id
  57. },
  58. function (res) {
  59. that.$nextTick(() => {
  60. that.list.is_like = 0;
  61. that.list.likes--;
  62. });
  63. that.$common.successToShow('取消点赞');
  64. }
  65. );
  66. }
  67. this.$emit('price', this.list);
  68. }
  69. }
  70. };
  71. </script>
  72. <style></style>