MediaAssetBoard.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <div class="media-asset-board-wrapper" :class="{ selected }" :style="{
  3. width: props.width + 'px',
  4. height: props.height + 'px',
  5. position: 'relative',
  6. display: 'flex',
  7. alignItems: 'center',
  8. justifyContent: 'center',
  9. overflow: 'hidden',
  10. flexDirection: 'column',
  11. gap: '8px',
  12. padding: '8px'
  13. }" @click="$emit('click', $event)">
  14. <!-- 预览窗口 -->
  15. <div v-if="mediaItems.length > 0" class="preview-container">
  16. <template v-for="(item, index) in mediaItems" :key="index">
  17. <div v-show="currentMediaIndex === index" class="media-item" :class="{ 'active': currentMediaIndex === index }">
  18. <img v-if="item.type === 1" :src="item.url" :alt="item.name" class="media-content" />
  19. <video v-else-if="item.type === 2" :src="item.url" class="media-content" preload="metadata" muted
  20. @loadeddata="onVideoLoaded"></video>
  21. </div>
  22. </template>
  23. <!-- 轮播组名称显示 -->
  24. <div v-if="carouselGroup.name" class="group-name-overlay">
  25. {{ carouselGroup.name }}
  26. </div>
  27. </div>
  28. <!-- 无媒体时的默认状态 -->
  29. <div v-else class="media-asset-icon">
  30. <svg width="32" height="32" viewBox="0 0 32 32" fill="none">
  31. <rect x="3" y="3" width="26" height="26" rx="6" fill="#f3f6fa" stroke="#409eff" stroke-width="2" />
  32. <path d="M8 22l6-6 4 4 6-6" stroke="#409eff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
  33. <circle cx="11" cy="12" r="2" fill="#409eff" />
  34. </svg>
  35. <div v-if="!props.readonly" class="select-hint">
  36. {{ carouselGroup.name ? `轮播组:${carouselGroup.name}` : '请选择轮播组' }}
  37. </div>
  38. </div>
  39. <!-- Resize -->
  40. <template v-if="selected && !props.readonly">
  41. <div v-for="dir in ['nw', 'ne', 'sw', 'se']" :key="dir" class="resize-handle" :class="'resize-' + dir"
  42. @mousedown.stop="onResizeMouseDown(dir, $event)" />
  43. </template>
  44. </div>
  45. </template>
  46. <script setup lang="ts">
  47. import { ref, computed, watch, onUnmounted, defineProps, defineEmits } from 'vue';
  48. import { getItem, getItemFileRelList } from '@/api/smsb/source/item';
  49. import { getMinioData } from '@/api/smsb/source/minioData';
  50. const emit = defineEmits(['update:mediaId', 'resize', 'click']);
  51. const props = withDefaults(
  52. defineProps<{
  53. width: number;
  54. height: number;
  55. mediaId?: string;
  56. selected?: boolean;
  57. readonly?: boolean;
  58. modelValue?: any;
  59. }>(),
  60. {
  61. selected: false,
  62. readonly: false,
  63. modelValue: null
  64. }
  65. );
  66. interface MediaItem {
  67. id: string | number;
  68. type: number; // 1-图片 2-视频
  69. url: string;
  70. duration: number; // 秒
  71. name?: string;
  72. }
  73. const mediaItems = ref<MediaItem[]>([]);
  74. const currentMediaIndex = ref(0);
  75. let slideInterval: number | null = null;
  76. // 解析轮播组数据
  77. const carouselGroup = computed(() => {
  78. try {
  79. if (props.mediaId) {
  80. return JSON.parse(props.mediaId);
  81. }
  82. } catch (e) {
  83. console.error('Failed to parse mediaId:', e);
  84. }
  85. return {};
  86. });
  87. const selected = computed(() => !!props.selected);
  88. // 清理轮播
  89. const clearSlideShow = () => {
  90. if (slideInterval !== null) {
  91. clearInterval(slideInterval);
  92. slideInterval = null;
  93. }
  94. };
  95. // 开始轮播
  96. const startSlideShow = () => {
  97. clearSlideShow();
  98. if (mediaItems.value.length <= 1) return;
  99. const playNext = () => {
  100. const currentItem = mediaItems.value[currentMediaIndex.value];
  101. const duration = (currentItem?.duration || 5) * 1000; // 转为毫秒,默认5秒
  102. // console.log(`播放第 ${currentMediaIndex.value + 1}/${mediaItems.value.length} 项,类型: ${currentItem.type},时长: ${duration}ms`);
  103. slideInterval = window.setTimeout(() => {
  104. currentMediaIndex.value = (currentMediaIndex.value + 1) % mediaItems.value.length;
  105. playNext();
  106. }, duration);
  107. };
  108. // 开始第一项播放
  109. playNext();
  110. };
  111. // 获取文件类型(1-图片,2-视频)
  112. const getFileType = (type: number): number => {
  113. // 1-图片,2-视频,3-音频
  114. return type === 2 ? 2 : 1; // 视频为2,其他都视为图片
  115. };
  116. // 加载轮播组媒体项
  117. const loadCarouselGroupMedia = async () => {
  118. // console.log('开始加载轮播组媒体项', carouselGroup.value);
  119. try {
  120. if (!carouselGroup.value || !carouselGroup.value.id) {
  121. // console.log('轮播组ID不存在');
  122. mediaItems.value = [];
  123. return;
  124. }
  125. // 1. 获取轮播组关联的文件关系列表
  126. const relRes = await getItemFileRelList(carouselGroup.value.id);
  127. // console.log('获取到轮播组关联文件关系:', relRes.data);
  128. if (!Array.isArray(relRes.data) || relRes.data.length === 0) {
  129. // console.log('轮播组没有关联的文件');
  130. mediaItems.value = [];
  131. return;
  132. }
  133. // 2. 处理每个关联文件
  134. const items: MediaItem[] = [];
  135. for (const rel of relRes.data) {
  136. try {
  137. // 获取文件详情
  138. // console.log(`加载文件详情: ${rel.fileId}`);
  139. const fileRes = await getMinioData(rel.fileId);
  140. const fileInfo = fileRes.data;
  141. // console.log('文件详情:', fileInfo);
  142. if (fileInfo) {
  143. // 使用 fileUrl 如果存在,否则回退到构建的 URL
  144. const fileUrl = fileInfo.fileUrl || `/api/source/file/${rel.fileId}`;
  145. // console.log(`文件 ${fileInfo.originalName} 的 URL:`, fileUrl);
  146. items.push({
  147. id: rel.fileId,
  148. type: getFileType(fileInfo.type),
  149. url: fileUrl,
  150. duration: rel.duration || 5, // 使用关联关系中的 duration
  151. name: fileInfo.originalName
  152. });
  153. }
  154. } catch (fileError) {
  155. console.error(`加载文件 ${rel.fileId} 详情失败:`, fileError);
  156. }
  157. }
  158. // 3. 按sort排序
  159. items.sort((a, b) => (a as any).sort - (b as any).sort);
  160. mediaItems.value = items;
  161. // console.log('最终媒体项列表:', mediaItems.value);
  162. // 4. 开始轮播
  163. currentMediaIndex.value = 0;
  164. startSlideShow();
  165. } catch (error) {
  166. console.error('加载轮播组媒体项失败:', error);
  167. mediaItems.value = [];
  168. }
  169. };
  170. // 监听mediaId变化
  171. watch(
  172. () => props.mediaId,
  173. (newVal) => {
  174. if (newVal) {
  175. loadCarouselGroupMedia();
  176. } else {
  177. mediaItems.value = [];
  178. clearSlideShow();
  179. }
  180. },
  181. { immediate: true }
  182. );
  183. const onVideoLoaded = (event: Event) => {
  184. const video = event.target as HTMLVideoElement;
  185. video.pause();
  186. video.currentTime = 0;
  187. };
  188. onUnmounted(() => {
  189. clearSlideShow();
  190. });
  191. // 暴露方法,供父组件调用
  192. const getCarouselGroupId = () => {
  193. return props.mediaId;
  194. };
  195. defineExpose({
  196. getCarouselGroupId
  197. });
  198. let startX = 0,
  199. startY = 0,
  200. startW = 0,
  201. startH = 0;
  202. function onResizeMouseDown(dir: string, e: MouseEvent) {
  203. e.stopPropagation();
  204. startX = e.clientX;
  205. startY = e.clientY;
  206. startW = props.width;
  207. startH = props.height;
  208. function onMouseMove(ev: MouseEvent) {
  209. let dx = ev.clientX - startX;
  210. let dy = ev.clientY - startY;
  211. let newW = startW;
  212. let newH = startH;
  213. if (dir.includes('e')) newW = Math.max(40, startW + dx);
  214. if (dir.includes('s')) newH = Math.max(40, startH + dy);
  215. if (dir.includes('w')) newW = Math.max(40, startW - dx);
  216. if (dir.includes('n')) newH = Math.max(40, startH - dy);
  217. emit('resize', { width: newW, height: newH });
  218. }
  219. function onMouseUp() {
  220. window.removeEventListener('mousemove', onMouseMove);
  221. window.removeEventListener('mouseup', onMouseUp);
  222. }
  223. window.addEventListener('mousemove', onMouseMove);
  224. window.addEventListener('mouseup', onMouseUp);
  225. }
  226. </script>
  227. <style scoped>
  228. .media-asset-board-wrapper {
  229. background: #f9fbfd;
  230. border: 2px dashed #b3c7e6;
  231. border-radius: 10px;
  232. transition: border-color 0.2s;
  233. position: relative;
  234. overflow: hidden;
  235. box-sizing: border-box;
  236. }
  237. .select-hint {
  238. margin-top: 8px;
  239. font-size: 12px;
  240. color: #909399;
  241. }
  242. .media-asset-board-wrapper.selected {
  243. border-color: #409eff;
  244. }
  245. .preview-container {
  246. width: 100%;
  247. height: 100%;
  248. position: relative;
  249. display: flex;
  250. align-items: center;
  251. justify-content: center;
  252. }
  253. .media-item {
  254. position: absolute;
  255. width: 100%;
  256. height: 100%;
  257. display: flex;
  258. align-items: center;
  259. justify-content: center;
  260. opacity: 0;
  261. transition: opacity 0.5s ease-in-out;
  262. }
  263. .media-item.active {
  264. opacity: 1;
  265. }
  266. .media-content {
  267. max-width: 100%;
  268. max-height: 100%;
  269. object-fit: contain;
  270. }
  271. .media-asset-icon {
  272. display: flex;
  273. flex-direction: column;
  274. align-items: center;
  275. justify-content: center;
  276. width: 100%;
  277. height: 100%;
  278. background: #f5f7fa;
  279. }
  280. .group-name-overlay {
  281. position: absolute;
  282. bottom: 0;
  283. left: 0;
  284. right: 0;
  285. background-color: rgba(0, 0, 0, 0.5);
  286. color: white;
  287. padding: 4px 8px;
  288. font-size: 12px;
  289. text-align: center;
  290. white-space: nowrap;
  291. overflow: hidden;
  292. text-overflow: ellipsis;
  293. }
  294. .resize-handle {
  295. position: absolute;
  296. width: 10px;
  297. height: 10px;
  298. background: #fff;
  299. border: 2px solid #409eff;
  300. border-radius: 50%;
  301. z-index: 10;
  302. cursor: pointer;
  303. }
  304. .resize-nw {
  305. left: -6px;
  306. top: -6px;
  307. cursor: nwse-resize;
  308. }
  309. .resize-ne {
  310. right: -6px;
  311. top: -6px;
  312. cursor: nesw-resize;
  313. }
  314. .resize-sw {
  315. left: -6px;
  316. bottom: -6px;
  317. cursor: nesw-resize;
  318. }
  319. .resize-se {
  320. right: -6px;
  321. bottom: -6px;
  322. cursor: nwse-resize;
  323. }
  324. </style>