index.vue 16 KB

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