LikeFooter.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <FlexCol position="fixed" :bottom="0" :left="0" :right="0" backgroundColor="#f5ebe0" :padding="[20,20,0,20]">
  3. <FlexRow justify="space-between">
  4. <FlexRow align="center">
  5. </FlexRow>
  6. <FlexRow align="center">
  7. <Touchable direction="row" :gap="10" :padding="[0,10]" @click="doLike">
  8. <Icon icon="good" :color="content.isLike ? 'primary' : 'text.content'" />
  9. <Text :text="likes" :color="content.isLike ? 'primary' : 'text.content'" />
  10. </Touchable>
  11. <Touchable direction="row" :gap="10" :padding="[0,10]" @click="doCollect">
  12. <Icon icon="favorite" :color="content.isCollect ? 'warning' : 'text.content'" />
  13. <Text text="收藏" :color="content.isCollect ? 'warning' : 'text.content'" />
  14. </Touchable>
  15. <button class="remove-button-style" direction="row" open-type="share">
  16. <FlexRow :gap="10" align="center" :padding="[0,10]" >
  17. <Icon icon="share" color="text.content" />
  18. <Text text="分享" />
  19. </FlexRow>
  20. </button>
  21. </FlexRow>
  22. </FlexRow>
  23. <XBarSpace />
  24. </FlexCol>
  25. </template>
  26. <script setup lang="ts">
  27. import type { GetContentDetailItem } from '@/api/CommonContent';
  28. import { computed, type PropType } from 'vue';
  29. import { toast } from "@/components/utils/DialogAction";
  30. import UserApi from "@/api/user/UserApi";
  31. import Icon from "@/components/basic/Icon.vue";
  32. import FlexRow from "@/components/layout/FlexRow.vue";
  33. import XBarSpace from "@/components/layout/space/XBarSpace.vue";
  34. import FlexCol from "@/components/layout/FlexCol.vue";
  35. import Touchable from "@/components/feedback/Touchable.vue";
  36. import Text from '@/components/basic/Text.vue';
  37. const props = defineProps({
  38. content: {
  39. type: Object as PropType<GetContentDetailItem>,
  40. default: () => ({
  41. isLike: false,
  42. likes: 0,
  43. }),
  44. }
  45. })
  46. const likes = computed(() => formatNumber(props.content.likes));
  47. async function doLike() {
  48. const content = props.content;
  49. if (!content)
  50. return;
  51. try {
  52. if (content.isLike) {
  53. await UserApi.unlike(content.id);
  54. toast('取消点赞');
  55. content.isLike = false;
  56. content.likes--;
  57. } else {
  58. await UserApi.like(content.id);
  59. content.isLike = true;
  60. content.likes++;
  61. toast('感谢点赞');
  62. }
  63. } catch (error) {
  64. toast('操作失败' + error);
  65. }
  66. }
  67. async function doCollect() {
  68. const content = props.content;
  69. if (!content)
  70. return;
  71. console.log(content);
  72. try {
  73. if (content.isCollect) {
  74. await UserApi.uncollect(content.id);
  75. toast('取消收藏');
  76. content.isCollect = false;
  77. } else {
  78. await UserApi.collect(content.id);
  79. content.isCollect = true;
  80. toast('收藏成功');
  81. }
  82. } catch (error) {
  83. toast('操作失败' + error);
  84. }
  85. }
  86. function formatNumber(num: number) {
  87. num = Math.max(num, 0);
  88. if (num >= 10000)
  89. return (num / 10000).toFixed(2) + '万';
  90. return num.toString();
  91. }
  92. </script>