index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <div>
  3. <el-form-item label="视频上传">
  4. <div class="upload_video" id="upload_video">
  5. <el-upload
  6. ref="upload"
  7. action="#"
  8. :http-request="uploadVideoToTxPcdn"
  9. accept=".mp4"
  10. :limit="1"
  11. :on-remove="handleRemove"
  12. :on-change="handleChange"
  13. :auto-upload="false"
  14. :key="uploadKey"
  15. >
  16. <el-button slot="trigger" size="small" type="primary">选取视频</el-button>
  17. <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">点击上传</el-button>
  18. <!-- 线路一 -->
  19. <div class="progress-container" style="width:100% !important;">
  20. <span class="progress-label">线路一</span>
  21. <el-progress
  22. style="margin-top: 5px;"
  23. class="progress"
  24. :text-inside="true"
  25. :stroke-width="18"
  26. :percentage="txProgress"
  27. status="success">
  28. </el-progress>
  29. </div>
  30. <!-- 线路二 -->
  31. <!-- <div class="progress-container">-->
  32. <!-- <span class="progress-label">线路er</span>-->
  33. <!-- <el-progress-->
  34. <!-- style="margin-top: 5px;"-->
  35. <!-- class="progress"-->
  36. <!-- :text-inside="true"-->
  37. <!-- :stroke-width="18"-->
  38. <!-- :percentage="hwProgress"-->
  39. <!-- status="success">-->
  40. <!-- </el-progress>-->
  41. <!-- </div>-->
  42. <div slot="tip" class="el-upload__tip">只能上传mp4文件,且不超过500M</div>
  43. </el-upload>
  44. </div>
  45. </el-form-item>
  46. <el-form-item label="视频播放">
  47. <video v-if="videoUrl" ref="myvideo" :src="videoUrl" id="video" width="100%" height="300px" controls></video>
  48. <div v-if="fileName">视频文件名: {{ fileName }}</div>
  49. <div v-if="fileKey">文件Key: {{ fileKey }}</div>
  50. <div v-if="fileSize">文件大小(MB): {{ (fileSize / (1024 * 1024)).toFixed(2) }} MB</div>
  51. </el-form-item>
  52. <!-- 仅当showControl为true时显示播放线路选择器 -->
  53. <el-form-item v-if="showControl" label="播放线路">
  54. <el-radio-group v-model="localUploadType">
  55. <el-radio :label="1" >线路一</el-radio>
  56. <!-- <el-radio :label="2" >线路二</el-radio>-->
  57. <!-- <el-radio :label="3" >线路三</el-radio>-->
  58. </el-radio-group>
  59. </el-form-item>
  60. </div>
  61. </template>
  62. <script>
  63. import { uploadObject } from "@/utils/cos.js";
  64. import Pagination from "@/components/Pagination";
  65. export default {
  66. components: {
  67. Pagination
  68. },
  69. props: {
  70. videoUrl: {
  71. type: String,
  72. default: "",
  73. },
  74. fileKey: {
  75. type: String,
  76. default: "",
  77. },
  78. fileSize: {
  79. type: Number,
  80. default: null,
  81. },
  82. fileName: {
  83. type: String,
  84. default: "",
  85. },
  86. line_1: {
  87. type: String,
  88. default: "",
  89. },
  90. line_2: {
  91. type: String,
  92. default: "",
  93. },
  94. line_3: {
  95. type: String,
  96. default: "",
  97. },
  98. thumbnail: {
  99. type: String,
  100. default: "",
  101. },
  102. uploadType: {
  103. type: Number,
  104. default: null,
  105. },
  106. type: {
  107. type: Number,
  108. default: null,
  109. },
  110. isPrivate: {
  111. type: Number,
  112. default: 0,
  113. },
  114. // 使用一个变量控制显示,默认为true显示所有控制项
  115. showControl: {
  116. type: Boolean,
  117. default: false,
  118. }
  119. },
  120. data() {
  121. return {
  122. videoName:'',
  123. isHidden : false,
  124. localUploadType: this.uploadType,
  125. duration: 0,
  126. fileId: "",
  127. uploadTypeOptions: [
  128. { dictLabel: "线路一", dictValue: 1 }, // 腾讯pcdn
  129. // { dictLabel: "线路二", dictValue: 2 }, // 华为云obs
  130. // { dictLabel: "华为云VOD", dictValue: 4 },
  131. // { dictLabel: "腾讯云VOD", dictValue: 5 },
  132. ],
  133. fileList: [],
  134. txProgress: 0,
  135. hwProgress: 0,
  136. uploadLoading: false,
  137. uploadKey: 0,
  138. };
  139. },
  140. watch: {
  141. localUploadType(newType) {
  142. this.reset()
  143. this.$emit("update:uploadType", newType);
  144. },
  145. uploadType(newType) {
  146. this.localUploadType = newType;
  147. },
  148. },
  149. mounted() {
  150. this.reset()
  151. },
  152. methods: {
  153. // 提供重置方法供父组件调用
  154. reset() {
  155. this.txProgress = 0;
  156. this.fileList = [];
  157. this.$refs.upload.clearFiles();
  158. // 重置其他状态...
  159. },
  160. handleChange(file, fileList) {
  161. this.fileList = fileList;
  162. this.getVideoDuration(file.raw);
  163. },
  164. getVideoDuration(file) {
  165. const video = document.createElement("video");
  166. video.preload = "metadata";
  167. video.onloadedmetadata = () => {
  168. window.URL.revokeObjectURL(video.src);
  169. this.duration = parseInt(video.duration.toFixed(2));
  170. console.log("视频时长=========>", this.duration);
  171. this.$emit("video-duration", this.duration);
  172. console.log("文件大小=====>", file.size);
  173. this.$emit("update:fileSize", file.size);
  174. };
  175. video.src = URL.createObjectURL(file);
  176. },
  177. async submitUpload() {
  178. if (this.fileList.length < 1) {
  179. return this.$message.error("请先选取视频,再进行上传");
  180. }
  181. //同时上传个线路
  182. await this.uploadVideoToTxPcdn();
  183. // await this.uploadVideoToHwObs();
  184. this.$emit("update:fileName", this.fileList[0].name);
  185. },
  186. //获取第一帧封面
  187. async getFirstThumbnail() {
  188. const file = this.fileList[0].raw;
  189. getThumbnail(file).then(response => {
  190. console.log("获取到第一帧为封面======>", response.url)
  191. this.$emit("update:thumbnail", response.url);
  192. })
  193. },
  194. //更新华为线路进度条
  195. updateHwProgress(progress) {
  196. this.hwProgress = progress;
  197. },
  198. //更新腾讯线路进度条
  199. updateTxProgress(progressData) {
  200. this.txProgress = Math.round(progressData.percent * 100);
  201. },
  202. //上传腾讯云Pcdn
  203. async uploadVideoToTxPcdn() {
  204. try {
  205. const file = this.fileList[0].raw;
  206. const data = await uploadObject(file, this.updateTxProgress, 3);
  207. console.log("腾讯COS返回========>", data);
  208. console.log("isPrivate=======>", this.isPrivate)
  209. let line_1 = '';
  210. if (this.isPrivate === 0) {
  211. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  212. } else {
  213. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  214. }
  215. let urlPathWithoutFirstSlash = data.urlPath.substring(1);
  216. this.$emit("update:fileKey", urlPathWithoutFirstSlash);
  217. console.log("文件key", urlPathWithoutFirstSlash);
  218. console.log("组装URL========>", line_1);
  219. this.$emit("update:videoUrl", line_1);
  220. this.$emit("update:line_1", line_1);
  221. this.$emit("change", line_1,line_1);
  222. // this.$emit("update:line_2", line_2);
  223. this.$message.success("线路一上传成功");
  224. } catch (error) {
  225. this.$message.error("线路一上传失败");
  226. }
  227. },
  228. // //上传华为云Obs
  229. // async uploadVideoToHwObs() {
  230. // try {
  231. // const file = this.fileList[0].raw;
  232. // const data = await uploadToOBS(file, this.updateHwProgress, this.type);
  233. // console.log("华为OBS返回========>", data);
  234. // let line_2 = '';
  235. // if (this.isPrivate === 0) {
  236. // line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  237. // } else {
  238. // line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  239. // }
  240. // this.$emit("update:videoUrl", line_2);
  241. // this.$emit("update:line_2", line_2);
  242. // this.$message.success("线路二上传成功");
  243. // } catch (error) {
  244. // this.$message.error("线路二上传失败");
  245. // }
  246. // },
  247. handleRemove(file, fileList) {
  248. console.log(file, fileList.length);
  249. },
  250. }
  251. };
  252. </script>
  253. <style>
  254. .progress-container {
  255. margin-bottom: 5px; /* 进度条之间的间距 */
  256. }
  257. .progress-label {
  258. display: block;
  259. font-weight: bold;
  260. font-size: 13px;
  261. color: #303331; /* 标签颜色,可以根据需要调整 */
  262. }
  263. /* 视频库选择对话框样式 */
  264. .library-search {
  265. margin-bottom: 15px;
  266. }
  267. .el-table .el-table__row:hover {
  268. cursor: pointer;
  269. }
  270. </style>