index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <template>
  2. <div>
  3. <el-form-item label="视频上传">
  4. <div class="upload_video" id="upload_video">
  5. <el-upload
  6. class="upload-demo"
  7. ref="upload"
  8. action="#"
  9. :http-request="uploadVideoToTxPcdn"
  10. accept=".mp4"
  11. :limit="1"
  12. :on-remove="handleRemove"
  13. :on-change="handleChange"
  14. :auto-upload="false"
  15. :key="uploadKey"
  16. >
  17. <el-button slot="trigger" size="small" type="primary" >选取视频</el-button>
  18. <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">点击上传</el-button>
  19. <!-- 仅当showControl为true时显示视频库选取按钮 -->
  20. <el-button v-if="showControl" style="margin-left: 10px;" size="small" type="success" @click="openVideoLibrary">视频库选取</el-button>
  21. <!-- 线路一 -->
  22. <div class="progress-container">
  23. <span class="progress-label">线路一</span>
  24. <el-progress
  25. style="margin-top: 5px;"
  26. class="progress"
  27. :text-inside="true"
  28. :stroke-width="18"
  29. :percentage="txProgress"
  30. status="success">
  31. </el-progress>
  32. </div>
  33. <!-- 线路二 -->
  34. <div class="progress-container">
  35. <span class="progress-label">线路二</span>
  36. <el-progress
  37. style="margin-top: 5px;"
  38. class="progress"
  39. :text-inside="true"
  40. :stroke-width="18"
  41. :percentage="hwProgress"
  42. status="success">
  43. </el-progress>
  44. </div>
  45. <div slot="tip" class="el-upload__tip">只能上传mp4文件,且不超过500M</div>
  46. </el-upload>
  47. </div>
  48. </el-form-item>
  49. <el-form-item label="视频播放">
  50. <video v-if="videoUrl" ref="myvideo" :src="videoUrl" id="video" width="100%" height="300px" controls></video>
  51. <div v-if="fileName">视频文件名: {{ fileName }}</div>
  52. <div v-if="fileKey">文件Key: {{ fileKey }}</div>
  53. <div v-if="fileSize">文件大小(MB): {{ (fileSize / (1024 * 1024)).toFixed(2) }} MB</div>
  54. </el-form-item>
  55. <!-- 仅当showControl为true时显示播放线路选择器 -->
  56. <el-form-item v-if="showControl" label="播放线路">
  57. <el-radio-group v-model="localUploadType">
  58. <el-radio :label="1" >线路一</el-radio>
  59. <el-radio :label="2" >线路二</el-radio>
  60. <!-- <el-radio :label="3" >线路三</el-radio>-->
  61. </el-radio-group>
  62. </el-form-item>
  63. <!-- 视频库选择对话框 -->
  64. <el-dialog title="视频库选择" :visible.sync="libraryOpen" width="900px" append-to-body>
  65. <!-- 搜索条件 -->
  66. <el-form :inline="true" :model="libraryQueryParams" class="library-search">
  67. <el-form-item label="素材名称">
  68. <el-input
  69. v-model="libraryQueryParams.resourceName"
  70. placeholder="请输入素材名称"
  71. clearable
  72. size="small"
  73. @keyup.enter.native="handleLibraryQuery"
  74. />
  75. </el-form-item>
  76. <el-form-item>
  77. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleLibraryQuery">搜索</el-button>
  78. <el-button icon="el-icon-refresh" size="mini" @click="resetLibraryQuery">重置</el-button>
  79. </el-form-item>
  80. </el-form>
  81. <!-- 视频列表 -->
  82. <el-table v-loading="libraryLoading" :data="libraryList" @row-click="handleLibrarySelect" highlight-current-row>
  83. <el-table-column label="素材名称" align="center" prop="resourceName" />
  84. <el-table-column label="文件名称" align="center" prop="fileName" />
  85. <el-table-column label="缩略图" align="center">
  86. <template slot-scope="scope">
  87. <el-popover
  88. placement="right"
  89. title=""
  90. trigger="hover"
  91. >
  92. <img alt="" slot="reference" :src="scope.row.thumbnail" style="width: 80px; height: 50px" />
  93. <img alt="" :src="scope.row.thumbnail" style="max-width: 150px;" />
  94. </el-popover>
  95. </template>
  96. </el-table-column>
  97. <el-table-column label="视频时长" align="center">
  98. <template slot-scope="scope">
  99. <span>{{ formatDuration(scope.row.duration) }}</span>
  100. </template>
  101. </el-table-column>
  102. </el-table>
  103. <!-- 分页 -->
  104. <pagination
  105. v-show="libraryTotal>0"
  106. :total="libraryTotal"
  107. :page.sync="libraryQueryParams.pageNum"
  108. :limit.sync="libraryQueryParams.pageSize"
  109. @pagination="getLibraryList"
  110. />
  111. <div slot="footer" class="dialog-footer">
  112. <el-button type="primary" @click="confirmVideoSelection">确 定</el-button>
  113. <el-button @click="cancelVideoSelection">取 消</el-button>
  114. </div>
  115. </el-dialog>
  116. </div>
  117. </template>
  118. <script>
  119. import {getSignature, uploadHuaWeiObs, uploadHuaWeiVod} from "@/api/common";
  120. import {getThumbnail} from "@/api/course/userVideo";
  121. import TcVod from "vod-js-sdk-v6";
  122. import { uploadObject } from "@/utils/cos.js";
  123. import { uploadToOBS } from "@/utils/obs.js";
  124. import Pagination from "@/components/Pagination";
  125. import { listVideoResource } from '@/api/course/videoResource';
  126. export default {
  127. components: {
  128. Pagination
  129. },
  130. props: {
  131. videoUrl: {
  132. type: String,
  133. default: "",
  134. },
  135. fileKey: {
  136. type: String,
  137. default: "",
  138. },
  139. fileSize: {
  140. type: Number,
  141. default: null,
  142. },
  143. fileName: {
  144. type: String,
  145. default: "",
  146. },
  147. line_1: {
  148. type: String,
  149. default: "",
  150. },
  151. line_2: {
  152. type: String,
  153. default: "",
  154. },
  155. line_3: {
  156. type: String,
  157. default: "",
  158. },
  159. thumbnail: {
  160. type: String,
  161. default: "",
  162. },
  163. uploadType: {
  164. type: Number,
  165. default: null,
  166. },
  167. type: {
  168. type: Number,
  169. default: null,
  170. },
  171. isPrivate: {
  172. type: Number,
  173. default: 0,
  174. },
  175. // 使用一个变量控制显示,默认为true显示所有控制项
  176. showControl: {
  177. type: Boolean,
  178. default: true,
  179. }
  180. },
  181. data() {
  182. return {
  183. videoName:'',
  184. isHidden : false,
  185. localUploadType: this.uploadType,
  186. duration: 0,
  187. fileId: "",
  188. uploadTypeOptions: [
  189. { dictLabel: "线路一", dictValue: 1 }, // 腾讯pcdn
  190. { dictLabel: "线路二", dictValue: 2 }, // 华为云obs
  191. // { dictLabel: "华为云VOD", dictValue: 4 },
  192. // { dictLabel: "腾讯云VOD", dictValue: 5 },
  193. ],
  194. fileList: [],
  195. txProgress: 0,
  196. hwProgress: 0,
  197. uploadLoading: false,
  198. uploadKey: 0,
  199. // 视频库选择相关数据
  200. libraryOpen: false,
  201. libraryLoading: false,
  202. libraryTotal: 0,
  203. libraryList: [],
  204. selectedVideo: null,
  205. libraryQueryParams: {
  206. pageNum: 1,
  207. pageSize: 10,
  208. resourceName: null
  209. }
  210. };
  211. },
  212. watch: {
  213. localUploadType(newType) {
  214. this.$emit("update:uploadType", newType);
  215. this.$emit("change");
  216. },
  217. uploadType(newType) {
  218. this.localUploadType = newType;
  219. },
  220. },
  221. methods: {
  222. // 打开视频库对话框
  223. openVideoLibrary() {
  224. this.libraryOpen = true;
  225. this.selectedVideo = null;
  226. this.getLibraryList();
  227. },
  228. handleChange(file, fileList) {
  229. this.fileList = fileList;
  230. this.getVideoDuration(file.raw);
  231. },
  232. getVideoDuration(file) {
  233. const video = document.createElement("video");
  234. video.preload = "metadata";
  235. video.onloadedmetadata = () => {
  236. window.URL.revokeObjectURL(video.src);
  237. this.duration = parseInt(video.duration.toFixed(2));
  238. console.log("视频时长=========>",this.duration);
  239. this.$emit("video-duration", this.duration);
  240. console.log("文件大小=====>",file.size);
  241. this.$emit("update:fileSize", file.size);
  242. };
  243. video.src = URL.createObjectURL(file);
  244. },
  245. async submitUpload() {
  246. if (this.fileList.length < 1) {
  247. return this.$message.error("请先选取视频,再进行上传");
  248. }
  249. await this.getFirstThumbnail();
  250. //同时上传个线路
  251. await this.uploadVideoToTxPcdn();
  252. await this.uploadVideoToHwObs();
  253. this.$emit("update:fileName", this.fileList[0].name);
  254. },
  255. //获取第一帧封面
  256. async getFirstThumbnail(){
  257. const file = this.fileList[0].raw;
  258. getThumbnail(file).then(response => {
  259. console.log("获取到第一帧为封面======>",response.url)
  260. this.$emit("update:thumbnail", response.url);
  261. })
  262. },
  263. //更新华为线路进度条
  264. updateHwProgress(progress) {
  265. this.hwProgress = progress;
  266. },
  267. //更新腾讯线路进度条
  268. updateTxProgress(progressData) {
  269. this.txProgress = Math.round(progressData.percent * 100);
  270. },
  271. //上传腾讯云Pcdn
  272. async uploadVideoToTxPcdn() {
  273. try {
  274. const file = this.fileList[0].raw;
  275. const data = await uploadObject(file, this.updateTxProgress,this.type);
  276. console.log("腾讯COS返回========>",data);
  277. console.log("isPrivate=======>",this.isPrivate)
  278. let line_1='' ;
  279. if (this.isPrivate===0){
  280. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  281. }else {
  282. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  283. }
  284. let urlPathWithoutFirstSlash = data.urlPath.substring(1);
  285. this.$emit("update:fileKey", urlPathWithoutFirstSlash);
  286. console.log("文件key",urlPathWithoutFirstSlash);
  287. console.log("组装URL========>",line_1);
  288. this.$emit("update:videoUrl", line_1);
  289. this.$emit("update:line_1", line_1);
  290. // this.$emit("update:line_2", line_2);
  291. this.$message.success("线路一上传成功");
  292. } catch (error) {
  293. this.$message.error("线路一上传失败");
  294. }
  295. },
  296. //上传华为云Obs
  297. async uploadVideoToHwObs() {
  298. try {
  299. const file = this.fileList[0].raw;
  300. const data = await uploadToOBS(file, this.updateHwProgress,this.type);
  301. console.log("华为OBS返回========>",data);
  302. let line_2='' ;
  303. if (this.isPrivate===0){
  304. line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  305. }else {
  306. line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  307. }
  308. // this.$emit("update:videoUrl", data);
  309. this.$emit("update:line_2", line_2);
  310. this.$message.success("线路二上传成功");
  311. } catch (error) {
  312. this.$message.error("线路二上传失败");
  313. }
  314. },
  315. handleRemove(file, fileList) {
  316. console.log(file, fileList.length);
  317. },
  318. resetUpload() {
  319. // 重置内部状态
  320. this.txProgress = 0;
  321. this.hwProgress = 0;
  322. this.fileList = [];
  323. this.uploadKey++;
  324. },
  325. /** 查询视频库列表 */
  326. getLibraryList() {
  327. this.libraryLoading = true;
  328. listVideoResource(this.libraryQueryParams).then(response => {
  329. this.libraryList = response.rows;
  330. this.libraryTotal = response.total;
  331. this.libraryLoading = false;
  332. });
  333. },
  334. /** 搜索视频库按钮操作 */
  335. handleLibraryQuery() {
  336. this.libraryQueryParams.pageNum = 1;
  337. this.getLibraryList();
  338. },
  339. /** 重置视频库查询按钮操作 */
  340. resetLibraryQuery() {
  341. this.libraryQueryParams = {
  342. pageNum: 1,
  343. pageSize: 10,
  344. resourceName: null
  345. };
  346. this.handleLibraryQuery();
  347. },
  348. /** 视频库选择行点击 */
  349. handleLibrarySelect(row) {
  350. this.selectedVideo = row;
  351. },
  352. /** 格式化视频时长 */
  353. formatDuration(seconds) {
  354. if (!seconds) return '00:00';
  355. const minutes = Math.floor(seconds / 60);
  356. const remainingSeconds = seconds % 60;
  357. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  358. },
  359. /** 确认选择视频 */
  360. confirmVideoSelection() {
  361. if (!this.selectedVideo) {
  362. this.$message.warning("请选择一个视频");
  363. return;
  364. }
  365. // 更新组件内部数据
  366. this.$emit("update:fileName", this.selectedVideo.fileName);
  367. this.$emit("update:thumbnail", this.selectedVideo.thumbnail);
  368. this.$emit("update:line_1", this.selectedVideo.line1);
  369. this.$emit("update:line_2", this.selectedVideo.line2);
  370. this.$emit("update:line_3", this.selectedVideo.line3);
  371. this.$emit("update:fileSize", this.selectedVideo.fileSize);
  372. this.$emit("update:fileKey", this.selectedVideo.fileKey);
  373. this.$emit("update:uploadType", this.selectedVideo.uploadType);
  374. this.$emit("video-duration", this.selectedVideo.duration);
  375. // 设置预览URL
  376. this.$emit("update:videoUrl", this.selectedVideo.videoUrl);
  377. // 题目
  378. this.$emit("selectProjects", this.selectedVideo.projectIds)
  379. this.libraryOpen = false;
  380. },
  381. /** 取消视频选择 */
  382. cancelVideoSelection() {
  383. this.libraryOpen = false;
  384. this.selectedVideo = null;
  385. }
  386. }
  387. };
  388. </script>
  389. <style>
  390. .progress-container {
  391. margin-bottom: 5px; /* 进度条之间的间距 */
  392. }
  393. .progress-label {
  394. display: block;
  395. font-weight: bold;
  396. font-size: 13px;
  397. color: #303331; /* 标签颜色,可以根据需要调整 */
  398. }
  399. /* 视频库选择对话框样式 */
  400. .library-search {
  401. margin-bottom: 15px;
  402. }
  403. .el-table .el-table__row:hover {
  404. cursor: pointer;
  405. }
  406. </style>