index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { PropType, defineComponent } from 'vue';
  2. import styles from './index.module.less';
  3. import { NButton, NCard, NImage } from 'naive-ui';
  4. import iconImage from '@common/images/icon-image.png';
  5. import iconVideo from '@common/images/icon-video.png';
  6. import iconAudio from '@common/images/icon-audio.png';
  7. import iconMusic from '@common/images/icon-music.png';
  8. import TheNoticeBar from '../TheNoticeBar';
  9. type itemType = {
  10. id: string | number;
  11. type: 'IMG' | 'VIDEO' | 'AUDIO' | 'SONG';
  12. url: string;
  13. title: string;
  14. isCollect: boolean;
  15. isSelected: boolean;
  16. };
  17. export default defineComponent({
  18. name: 'card-type',
  19. props: {
  20. // 是否是选中状态
  21. isActive: {
  22. type: Boolean,
  23. default: false
  24. },
  25. // 是否可以收藏
  26. isCollect: {
  27. type: Boolean,
  28. default: true
  29. },
  30. // 是否显示收藏
  31. isShowCollect: {
  32. type: Boolean,
  33. default: true
  34. },
  35. // 是否显示添加按钮
  36. isShowAdd: {
  37. type: Boolean,
  38. default: false
  39. },
  40. item: {
  41. type: Object as PropType<itemType>,
  42. default: () => ({})
  43. }
  44. },
  45. /**
  46. * @type {string} click 点击事件
  47. * @type {string} collect 收藏
  48. * @type {string} add 添加
  49. */
  50. emits: ['click', 'collect', 'add'],
  51. setup(props, { emit }) {
  52. const formatType = (type: string) => {
  53. let typeImg = iconImage;
  54. switch (type) {
  55. case 'IMG':
  56. typeImg = iconImage;
  57. break;
  58. case 'VIDEO':
  59. typeImg = iconVideo;
  60. break;
  61. case 'AUDIO':
  62. typeImg = iconAudio;
  63. break;
  64. case 'SONG':
  65. typeImg = iconMusic;
  66. break;
  67. }
  68. return typeImg;
  69. };
  70. return () => (
  71. <div
  72. onClick={() => emit('click', props.item)}
  73. class={[styles['card-section']]}>
  74. <NCard
  75. class={[
  76. styles['card-section'],
  77. props.isActive ? styles.isActive : ''
  78. ]}>
  79. {{
  80. cover: () =>
  81. ['IMG', 'VIDEO', 'SONG', 'AUDIO'].includes(props.item.type) && (
  82. <NImage
  83. class={[styles.cover, styles.image]}
  84. lazy
  85. previewDisabled
  86. objectFit="cover"
  87. src={props.item.url}
  88. />
  89. ),
  90. footer: () => (
  91. <div class={styles.footer}>
  92. <div class={styles.title}>
  93. <NImage
  94. class={[styles.titleType]}
  95. src={formatType(props.item.type)}
  96. objectFit="cover"
  97. />
  98. <span class={styles.titleContent}>
  99. <TheNoticeBar text={props.item.title} />
  100. </span>
  101. </div>
  102. {/* 收藏 */}
  103. {props.isShowCollect && (
  104. <i
  105. onClick={(e: MouseEvent) => {
  106. e.stopPropagation();
  107. e.preventDefault();
  108. // 判断是否可以收藏
  109. if (props.isCollect) {
  110. console.log('Collect');
  111. emit('collect');
  112. }
  113. }}
  114. class={[
  115. styles.iconCollect,
  116. props.isCollect ? styles.isCollect : '',
  117. props.item.isCollect ? styles.isActive : ''
  118. ]}></i>
  119. )}
  120. {/* 精选 */}
  121. {props.item.isSelected && (
  122. <span class={styles.iconSelected}></span>
  123. )}
  124. {/* 添加按钮 */}
  125. {props.isShowAdd && (
  126. <NButton
  127. type="primary"
  128. class={styles.addBtn}
  129. onClick={(e: MouseEvent) => {
  130. e.stopPropagation();
  131. e.preventDefault();
  132. emit('add');
  133. console.log('add');
  134. }}>
  135. 添加
  136. </NButton>
  137. )}
  138. </div>
  139. )
  140. }}
  141. </NCard>
  142. </div>
  143. );
  144. }
  145. });