index.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. exist?: boolean; // 是否已经选
  22. };
  23. export default defineComponent({
  24. name: 'card-type',
  25. props: {
  26. // 是否是选中状态
  27. isActive: {
  28. type: Boolean,
  29. default: false
  30. },
  31. // 是否可以收藏
  32. isCollect: {
  33. type: Boolean,
  34. default: true
  35. },
  36. // 是否显示收藏
  37. isShowCollect: {
  38. type: Boolean,
  39. default: true
  40. },
  41. // 是否显示添加按钮
  42. isShowAdd: {
  43. type: Boolean,
  44. default: false
  45. },
  46. // 鼠标移动上面的时候是否自动播放,或者可以点击
  47. disabledMouseHover: {
  48. type: Boolean,
  49. default: true
  50. },
  51. // 是否预览
  52. isPreview: {
  53. type: Boolean,
  54. default: true
  55. },
  56. item: {
  57. type: Object as PropType<itemType>,
  58. default: () => ({})
  59. },
  60. /** 是否下架 */
  61. offShelf: {
  62. type: Boolean,
  63. default: false
  64. }
  65. },
  66. /**
  67. * @type {string} click 点击事件
  68. * @type {string} collect 收藏
  69. * @type {string} add 添加
  70. * @type {string} offShelf 下架
  71. */
  72. emits: ['click', 'collect', 'add', 'offShelf'],
  73. setup(props, { emit }) {
  74. const isAnimation = ref(false);
  75. const formatType = (type: string) => {
  76. let typeImg = iconImage;
  77. switch (type) {
  78. case 'IMG':
  79. typeImg = iconImage;
  80. break;
  81. case 'VIDEO':
  82. typeImg = iconVideo;
  83. break;
  84. case 'SONG':
  85. typeImg = iconAudio;
  86. break;
  87. case 'MUSIC':
  88. typeImg = iconMusic;
  89. break;
  90. }
  91. return typeImg;
  92. };
  93. return () => (
  94. <div
  95. onClick={() => emit('click', props.item)}
  96. class={[styles['card-section']]}
  97. onMouseenter={() => {
  98. isAnimation.value = true;
  99. }}
  100. onMouseleave={() => {
  101. isAnimation.value = false;
  102. }}>
  103. {/* 判断是否下架 */}
  104. {props.offShelf && (
  105. <div class={styles.offShelfBg}>
  106. <p class={styles.offShelfTips}>该资源已被平台下架</p>
  107. <NButton
  108. type="primary"
  109. class={styles.offShelfBtn}
  110. onClick={() => emit('offShelf')}>
  111. 确认
  112. </NButton>
  113. </div>
  114. )}
  115. <NCard
  116. class={[
  117. styles['card-section'],
  118. props.isShowAdd ? '' : styles.course,
  119. props.isActive ? styles.isActive : '',
  120. props.item.exist ? styles.showAddBtn : '' // 是否已添加
  121. ]}>
  122. {{
  123. cover: () => (
  124. <>
  125. {/* 图片 */}
  126. {props.item.type === 'IMG' && (
  127. <NImage
  128. class={[styles.cover, styles.image]}
  129. lazy
  130. previewDisabled={props.disabledMouseHover}
  131. objectFit="cover"
  132. src={props.item.coverImg}
  133. />
  134. )}
  135. {/* 乐谱 */}
  136. {props.item.type === 'MUSIC' && (
  137. <NImage
  138. class={[styles.cover, styles.image]}
  139. lazy
  140. previewDisabled={true}
  141. objectFit="contain"
  142. src={props.item.coverImg}
  143. />
  144. )}
  145. {/* 音频 */}
  146. {props.item.type === 'SONG' && (
  147. <AudioPlayer
  148. content={props.item.content}
  149. previewDisabled={props.disabledMouseHover}
  150. />
  151. )}
  152. {/* 视频 */}
  153. {props.item.type === 'VIDEO' && (
  154. <VideoPlayer
  155. cover={props.item.coverImg}
  156. content={props.item.content}
  157. previewDisabled={props.disabledMouseHover}
  158. />
  159. )}
  160. </>
  161. ),
  162. footer: () => (
  163. <div class={styles.footer}>
  164. <div class={styles.title}>
  165. <NImage
  166. class={[styles.titleType]}
  167. src={formatType(props.item.type)}
  168. objectFit="cover"
  169. />
  170. <span class={styles.titleContent}>
  171. <TheNoticeBar
  172. isAnimation={isAnimation.value}
  173. text={props.item.title}
  174. />
  175. </span>
  176. </div>
  177. {/* 收藏 */}
  178. {props.isShowCollect && (
  179. <div
  180. class={[styles.iconCollect, styles.iconDiv]}
  181. onClick={(e: MouseEvent) => {
  182. e.stopPropagation();
  183. e.preventDefault();
  184. // 判断是否可以收藏
  185. if (props.isCollect) {
  186. emit('collect', props.item);
  187. }
  188. }}>
  189. <Transition name="favitor" mode="out-in">
  190. {props.item.isCollect ? (
  191. <img
  192. src={iconCollectActive}
  193. key="1"
  194. class={[
  195. styles.iconCollect,
  196. props.isCollect ? styles.isCollect : ''
  197. ]}
  198. />
  199. ) : (
  200. <img
  201. src={iconCollectDefault}
  202. key="2"
  203. class={[
  204. styles.iconCollect,
  205. props.isCollect ? styles.isCollect : ''
  206. ]}
  207. />
  208. )}
  209. </Transition>
  210. </div>
  211. )}
  212. {/* 精选 */}
  213. {props.item.isSelected && (
  214. <span class={styles.iconSelected}></span>
  215. )}
  216. {/* 添加按钮 */}
  217. {props.isShowAdd && (
  218. <NButton
  219. type="primary"
  220. class={[
  221. styles.addBtn,
  222. props.item.exist ? styles.addBtnDisabled : ''
  223. ]}
  224. disabled={props.item.exist}
  225. onClick={(e: MouseEvent) => {
  226. e.stopPropagation();
  227. e.preventDefault();
  228. emit('add', props.item);
  229. }}>
  230. {props.item.exist ? '已添加' : '添加'}
  231. </NButton>
  232. )}
  233. </div>
  234. )
  235. }}
  236. </NCard>
  237. </div>
  238. );
  239. }
  240. });