index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { PropType, Transition, 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 iconCollectDefault from '@common/images/icon-collect-default.png';
  9. import iconCollectActive from '@common/images/icon-collect-active.png';
  10. import TheNoticeBar from '../TheNoticeBar';
  11. import AudioPlayer from './audio-player';
  12. import VideoPlayer from './video-player';
  13. type itemType = {
  14. id: string | number;
  15. type: 'IMG' | 'VIDEO' | 'SONG' | 'MUSIC';
  16. coverImg: string;
  17. content?: string;
  18. title: string;
  19. isCollect: boolean;
  20. isSelected: boolean; // 精选
  21. };
  22. export default defineComponent({
  23. name: 'card-type',
  24. props: {
  25. // 是否是选中状态
  26. isActive: {
  27. type: Boolean,
  28. default: false
  29. },
  30. // 是否可以收藏
  31. isCollect: {
  32. type: Boolean,
  33. default: true
  34. },
  35. // 是否显示收藏
  36. isShowCollect: {
  37. type: Boolean,
  38. default: true
  39. },
  40. // 是否显示添加按钮
  41. isShowAdd: {
  42. type: Boolean,
  43. default: false
  44. },
  45. // 鼠标移动上面的时候是否自动播放,或者可以点击
  46. disabledMouseHover: {
  47. type: Boolean,
  48. default: true
  49. },
  50. // 是否预览
  51. isPreview: {
  52. type: Boolean,
  53. default: true
  54. },
  55. item: {
  56. type: Object as PropType<itemType>,
  57. default: () => ({})
  58. }
  59. },
  60. /**
  61. * @type {string} click 点击事件
  62. * @type {string} collect 收藏
  63. * @type {string} add 添加
  64. */
  65. emits: ['click', 'collect', 'add'],
  66. setup(props, { emit }) {
  67. const isAnimation = ref(false);
  68. const formatType = (type: string) => {
  69. let typeImg = iconImage;
  70. switch (type) {
  71. case 'IMG':
  72. typeImg = iconImage;
  73. break;
  74. case 'VIDEO':
  75. typeImg = iconVideo;
  76. break;
  77. case 'SONG':
  78. typeImg = iconAudio;
  79. break;
  80. case 'MUSIC':
  81. typeImg = iconMusic;
  82. break;
  83. }
  84. return typeImg;
  85. };
  86. return () => (
  87. <div
  88. onClick={() => emit('click', props.item)}
  89. class={[styles['card-section']]}
  90. onMouseenter={() => {
  91. isAnimation.value = true;
  92. }}
  93. onMouseleave={() => {
  94. isAnimation.value = false;
  95. }}>
  96. <NCard
  97. class={[
  98. styles['card-section'],
  99. props.isShowAdd ? '' : styles.course,
  100. props.isActive ? styles.isActive : ''
  101. ]}>
  102. {{
  103. cover: () => (
  104. <>
  105. {['IMG', 'MUSIC'].includes(props.item.type) && (
  106. <NImage
  107. class={[styles.cover, styles.image]}
  108. lazy
  109. previewDisabled={props.disabledMouseHover}
  110. objectFit="cover"
  111. src={props.item.coverImg}
  112. />
  113. )}
  114. {/* 音频 */}
  115. {props.item.type === 'SONG' && (
  116. <AudioPlayer
  117. content={props.item.content}
  118. previewDisabled={props.disabledMouseHover}
  119. />
  120. )}
  121. {/* 视频 */}
  122. {props.item.type === 'VIDEO' && (
  123. <VideoPlayer
  124. cover={props.item.coverImg}
  125. content={props.item.content}
  126. previewDisabled={props.disabledMouseHover}
  127. />
  128. )}
  129. </>
  130. ),
  131. footer: () => (
  132. <div class={styles.footer}>
  133. <div class={styles.title}>
  134. <NImage
  135. class={[styles.titleType]}
  136. src={formatType(props.item.type)}
  137. objectFit="cover"
  138. />
  139. <span class={styles.titleContent}>
  140. <TheNoticeBar
  141. isAnimation={isAnimation.value}
  142. text={props.item.title}
  143. />
  144. </span>
  145. </div>
  146. {/* 收藏 */}
  147. {props.isShowCollect && (
  148. <div
  149. class={[styles.iconCollect, styles.iconDiv]}
  150. onClick={(e: MouseEvent) => {
  151. e.stopPropagation();
  152. e.preventDefault();
  153. // 判断是否可以收藏
  154. if (props.isCollect) {
  155. emit('collect', props.item);
  156. }
  157. }}>
  158. <Transition name="favitor" mode="out-in">
  159. {props.item.isCollect ? (
  160. <img
  161. src={iconCollectActive}
  162. key="1"
  163. class={[
  164. styles.iconCollect,
  165. props.isCollect ? styles.isCollect : ''
  166. ]}
  167. />
  168. ) : (
  169. <img
  170. src={iconCollectDefault}
  171. key="2"
  172. class={[
  173. styles.iconCollect,
  174. props.isCollect ? styles.isCollect : ''
  175. ]}
  176. />
  177. )}
  178. </Transition>
  179. </div>
  180. )}
  181. {/* 精选 */}
  182. {props.item.isSelected && (
  183. <span class={styles.iconSelected}></span>
  184. )}
  185. {/* 添加按钮 */}
  186. {props.isShowAdd && (
  187. <NButton
  188. type="primary"
  189. class={styles.addBtn}
  190. onClick={(e: MouseEvent) => {
  191. e.stopPropagation();
  192. e.preventDefault();
  193. emit('add');
  194. console.log('add');
  195. }}>
  196. 添加
  197. </NButton>
  198. )}
  199. </div>
  200. )
  201. }}
  202. </NCard>
  203. </div>
  204. );
  205. }
  206. });