index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { PropType, defineComponent, ref } from 'vue';
  2. import styles from './index.module.less';
  3. import { NButton, NCard, NImage, NModal } 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. // 是否预览
  41. isPreview: {
  42. type: Boolean,
  43. default: true
  44. },
  45. item: {
  46. type: Object as PropType<itemType>,
  47. default: () => ({})
  48. }
  49. },
  50. /**
  51. * @type {string} click 点击事件
  52. * @type {string} collect 收藏
  53. * @type {string} add 添加
  54. */
  55. emits: ['click', 'collect', 'add'],
  56. setup(props, { emit }) {
  57. const isAnimation = ref(false);
  58. const formatType = (type: string) => {
  59. let typeImg = iconImage;
  60. switch (type) {
  61. case 'IMG':
  62. typeImg = iconImage;
  63. break;
  64. case 'VIDEO':
  65. typeImg = iconVideo;
  66. break;
  67. case 'AUDIO':
  68. typeImg = iconAudio;
  69. break;
  70. case 'SONG':
  71. typeImg = iconMusic;
  72. break;
  73. }
  74. return typeImg;
  75. };
  76. return () => (
  77. <div
  78. onClick={() => emit('click', props.item)}
  79. class={[styles['card-section']]}
  80. onMouseenter={() => {
  81. isAnimation.value = true;
  82. }}
  83. onMouseleave={() => {
  84. isAnimation.value = false;
  85. }}>
  86. <NCard
  87. class={[
  88. styles['card-section'],
  89. props.isShowAdd ? '' : styles.course,
  90. props.isActive ? styles.isActive : ''
  91. ]}>
  92. {{
  93. cover: () => (
  94. <>
  95. {props.item.type === 'IMG' && (
  96. <NImage
  97. class={[styles.cover, styles.image]}
  98. lazy
  99. previewDisabled={!props.isPreview}
  100. objectFit="cover"
  101. src={props.item.url}
  102. />
  103. )}
  104. {['VIDEO', 'SONG', 'AUDIO'].includes(props.item.type) && (
  105. <NImage
  106. class={[styles.cover, styles.image]}
  107. lazy
  108. previewDisabled
  109. objectFit="cover"
  110. src={props.item.url}
  111. />
  112. )}
  113. </>
  114. ),
  115. footer: () => (
  116. <div class={styles.footer}>
  117. <div class={styles.title}>
  118. <NImage
  119. class={[styles.titleType]}
  120. src={formatType(props.item.type)}
  121. objectFit="cover"
  122. />
  123. <span class={styles.titleContent}>
  124. <TheNoticeBar
  125. isAnimation={isAnimation.value}
  126. text={props.item.title}
  127. />
  128. </span>
  129. </div>
  130. {/* 收藏 */}
  131. {props.isShowCollect && (
  132. <i
  133. onClick={(e: MouseEvent) => {
  134. e.stopPropagation();
  135. e.preventDefault();
  136. // 判断是否可以收藏
  137. if (props.isCollect) {
  138. console.log('Collect');
  139. emit('collect');
  140. }
  141. }}
  142. class={[
  143. styles.iconCollect,
  144. props.isCollect ? styles.isCollect : '',
  145. props.item.isCollect ? styles.isActive : ''
  146. ]}></i>
  147. )}
  148. {/* 精选 */}
  149. {props.item.isSelected && (
  150. <span class={styles.iconSelected}></span>
  151. )}
  152. {/* 添加按钮 */}
  153. {props.isShowAdd && (
  154. <NButton
  155. type="primary"
  156. class={styles.addBtn}
  157. onClick={(e: MouseEvent) => {
  158. e.stopPropagation();
  159. e.preventDefault();
  160. emit('add');
  161. console.log('add');
  162. }}>
  163. 添加
  164. </NButton>
  165. )}
  166. </div>
  167. )
  168. }}
  169. </NCard>
  170. </div>
  171. );
  172. }
  173. });