preview.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <view class="page">
  3. <view class="nav-bar">
  4. <view class="nav-content">
  5. <view class="nav-left" @click="goBack"><text class="back-icon">←</text></view>
  6. <text class="page-title">视频预览</text>
  7. <view class="nav-right"></view>
  8. </view>
  9. </view>
  10. <view class="video-container" v-if="project?.outputUrl">
  11. <video
  12. :src="baseUrl + project.outputUrl"
  13. class="video-player"
  14. controls
  15. autoplay
  16. show-fullscreen-btn
  17. ></video>
  18. </view>
  19. <view class="empty-state" v-else>
  20. <text class="empty-icon">🎥</text>
  21. <text class="empty-text">暂无视频</text>
  22. </view>
  23. <view class="action-buttons">
  24. <button class="action-btn download" @click="downloadVideo" v-if="project?.outputUrl">
  25. 📥 下载视频
  26. </button>
  27. <button class="action-btn share" @click="shareVideo" v-if="project?.outputUrl">
  28. 📤 分享视频
  29. </button>
  30. <button class="action-btn publish" @click="goToPublish" v-if="project?.outputUrl">
  31. 🚀 发布到平台
  32. </button>
  33. <button class="action-btn regenerate" @click="regenerateVideo">
  34. 🔄 重新生成
  35. </button>
  36. </view>
  37. <view class="video-info" v-if="project">
  38. <text class="info-title">{{ project.title }}</text>
  39. <view class="info-meta">
  40. <text class="meta-item">时长: {{ formatDuration(project.duration) }}</text>
  41. <text class="meta-item">大小: {{ formatFileSize(project.fileSize) }}</text>
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script setup lang="ts">
  47. import { ref, onMounted } from 'vue';
  48. import { onLoad, onShow } from '@dcloudio/uni-app';
  49. import { getVideoProject, generateVideo, getGenerateProgress, type VideoProjectResponse } from '@/utils/video-generator-api';
  50. const baseUrl = ''; // 使用相对路径,依赖 Nginx 代理
  51. const project = ref<VideoProjectResponse | null>(null);
  52. function goBack() {
  53. const pages = getCurrentPages();
  54. if (pages.length > 1) {
  55. uni.navigateBack();
  56. } else {
  57. uni.switchTab({ url: '/pages/index/index' });
  58. }
  59. }
  60. function formatDuration(seconds: number | null): string {
  61. if (!seconds) return '--:--';
  62. const mins = Math.floor(seconds / 60);
  63. const secs = seconds % 60;
  64. return `${mins}:${secs.toString().padStart(2, '0')}`;
  65. }
  66. function formatFileSize(bytes: number | null): string {
  67. if (!bytes) return '--';
  68. if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB';
  69. return (bytes / (1024 * 1024)).toFixed(1) + 'MB';
  70. }
  71. async function loadProject(id: number) {
  72. try {
  73. project.value = await getVideoProject(id);
  74. } catch (error) {
  75. console.error('加载项目失败:', error);
  76. uni.showToast({ title: '加载失败', icon: 'none' });
  77. }
  78. }
  79. async function downloadVideo() {
  80. if (!project.value?.outputUrl) return;
  81. const url = baseUrl + project.value.outputUrl;
  82. // #ifdef H5
  83. // H5 平台:打开新窗口下载
  84. window.open(url, '_blank');
  85. // #endif
  86. // #ifndef H5
  87. // App 平台:下载并保存到相册
  88. uni.downloadFile({
  89. url: url,
  90. success: (res) => {
  91. if (res.statusCode === 200) {
  92. uni.saveVideoToPhotosAlbum({
  93. filePath: res.tempFilePath,
  94. success: () => {
  95. uni.showToast({ title: '保存成功', icon: 'success' });
  96. },
  97. fail: (err) => {
  98. console.error('保存失败:', err);
  99. uni.showToast({ title: '保存失败', icon: 'none' });
  100. }
  101. });
  102. } else {
  103. uni.showToast({ title: '下载失败', icon: 'none' });
  104. }
  105. },
  106. fail: (err) => {
  107. console.error('下载失败:', err);
  108. uni.showToast({ title: '下载失败', icon: 'none' });
  109. }
  110. });
  111. // #endif
  112. }
  113. async function shareVideo() {
  114. if (!project.value?.outputUrl) return;
  115. const url = baseUrl + project.value.outputUrl;
  116. uni.share({
  117. provider: 'weixin',
  118. scene: 'WXSession',
  119. title: project.value.title,
  120. type: 5,
  121. videoUrl: url,
  122. });
  123. }
  124. function goToPublish() {
  125. if (!project.value?.id) return;
  126. uni.navigateTo({ url: `/pages/publish/index?projectId=${project.value.id}` });
  127. }
  128. async function regenerateVideo() {
  129. if (!project.value?.id) return;
  130. try {
  131. uni.showLoading({ title: '正在重新生成...' });
  132. await generateVideo(project.value.id);
  133. const pollInterval = setInterval(async () => {
  134. const data = await getGenerateProgress(project.value!.id);
  135. if (data.status === 'completed') {
  136. clearInterval(pollInterval);
  137. uni.hideLoading();
  138. uni.showToast({ title: '重新生成成功', icon: 'success' });
  139. loadProject(project.value!.id);
  140. } else if (data.status === 'failed') {
  141. clearInterval(pollInterval);
  142. uni.hideLoading();
  143. uni.showToast({ title: '生成失败', icon: 'none' });
  144. }
  145. }, 2000);
  146. } catch (error) {
  147. uni.hideLoading();
  148. console.error('重新生成失败:', error);
  149. uni.showToast({ title: '重新生成失败', icon: 'none' });
  150. }
  151. }
  152. let projectIdLoaded = 0;
  153. onLoad((query: any) => {
  154. if (query?.id) {
  155. projectIdLoaded = Number(query.id);
  156. loadProject(projectIdLoaded);
  157. }
  158. });
  159. onShow(() => {
  160. // H5 环境下 onLoad 可能不会再次触发,需要在 onShow 中检查并刷新
  161. if (projectIdLoaded && project.value?.id !== projectIdLoaded) {
  162. loadProject(projectIdLoaded);
  163. }
  164. });
  165. </script>
  166. <style scoped>
  167. .page { min-height: 100vh; background: #f5f5f5; }
  168. .nav-bar { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 20px 15px; }
  169. .nav-content { display: flex; align-items: center; justify-content: space-between; }
  170. .nav-left { width: 40px; }
  171. .back-icon { font-size: 24px; color: white; }
  172. .page-title { font-size: 18px; font-weight: 600; color: white; }
  173. .video-container { padding: 15px; }
  174. .video-player { width: 100%; border-radius: 12px; }
  175. .empty-state { padding: 60px 20px; text-align: center; }
  176. .empty-icon { font-size: 64px; display: block; margin-bottom: 15px; }
  177. .empty-text { font-size: 18px; color: #666; }
  178. .action-buttons { padding: 15px; display: flex; flex-direction: column; gap: 10px; }
  179. .action-btn { border: none; padding: 15px; border-radius: 8px; font-size: 16px; color: white; }
  180. .action-btn.download { background: #67c23a; }
  181. .action-btn.share { background: #409eff; }
  182. .action-btn.regenerate { background: #909399; }
  183. .action-btn.publish { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
  184. .video-info { padding: 20px 15px; }
  185. .info-title { font-size: 18px; font-weight: 600; color: #333; display: block; margin-bottom: 10px; }
  186. .info-meta { display: flex; gap: 20px; }
  187. .meta-item { font-size: 14px; color: #666; }
  188. </style>