index.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!-- @format -->
  2. <script lang="ts" setup>
  3. import { setGoodsFavoriteApi } from '~/api/model/goods'
  4. defineProps({
  5. data: { type: Object, default: () => {} },
  6. svgSize: {
  7. type: String,
  8. default: '24px',
  9. },
  10. w: {
  11. type: String,
  12. default: '50px',
  13. },
  14. })
  15. const emit = defineEmits(['update:favorite'])
  16. const { openLoginModal } = useLoginModal()
  17. const hoverStatus = ref<any>()
  18. async function handleFavorite(item: any) {
  19. try {
  20. const { status } = await openLoginModal()
  21. if (status) {
  22. const params = { mid: item.id, type: item.isFavorite ? 2 : 1 }
  23. await setGoodsFavoriteApi(params)
  24. item.isFavorite = !item.isFavorite
  25. ElMessage({
  26. message: `${item.isFavorite ? 'Add' : 'Remove'} to My Favourites Successfully`,
  27. type: 'success',
  28. plain: true,
  29. })
  30. emit('update:favorite', item)
  31. }
  32. }
  33. catch (error) {
  34. console.log(error)
  35. }
  36. }
  37. </script>
  38. <template>
  39. <div
  40. class="b-rd-50% b-1px b-solid b-#FAE7CE flex cursor-pointer justify-center items-center bg-#F9F6F2 hover:bg-#CC9879"
  41. :class="[{ '!bg-#CC9879': data.isFavorite }]"
  42. :style="{ width: w, height: w }"
  43. @mouseover="hoverStatus = '!fill-#fff'"
  44. @mouseleave="hoverStatus = '!fill-#CC9879'"
  45. @click.stop="handleFavorite(data)"
  46. >
  47. <svgo-like
  48. :class="(data.isFavorite && '!fill-#fff') || hoverStatus"
  49. class="!fill-#CC9879 "
  50. :style="{ fontSize: svgSize }"
  51. :filled="true"
  52. />
  53. </div>
  54. </template>
  55. <style lang="less" scoped></style>