index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. mounted() {
  141. this.reset()
  142. },
  143. watch: {
  144. localUploadType(newType) {
  145. this.$emit("update:uploadType", newType);
  146. },
  147. uploadType(newType) {
  148. this.localUploadType = newType;
  149. },
  150. },
  151. methods: {
  152. // 提供重置方法供父组件调用
  153. reset() {
  154. this.txProgress = 0;
  155. this.fileList = [];
  156. this.$refs.upload.clearFiles();
  157. // 重置其他状态...
  158. },
  159. handleChange(file, fileList) {
  160. this.fileList = fileList;
  161. this.getVideoDuration(file.raw);
  162. },
  163. getVideoDuration(file) {
  164. const video = document.createElement("video");
  165. video.preload = "metadata";
  166. video.onloadedmetadata = () => {
  167. window.URL.revokeObjectURL(video.src);
  168. this.duration = parseInt(video.duration.toFixed(2));
  169. console.log("视频时长=========>",this.duration);
  170. this.$emit("video-duration", this.duration);
  171. console.log("文件大小=====>",file.size);
  172. this.$emit("update:fileSize", file.size);
  173. };
  174. video.src = URL.createObjectURL(file);
  175. },
  176. async submitUpload() {
  177. if (this.fileList.length < 1) {
  178. return this.$message.error("请先选取视频,再进行上传");
  179. }
  180. //同时上传个线路
  181. await this.uploadVideoToTxPcdn();
  182. // await this.uploadVideoToHwObs();
  183. this.$emit("update:fileName", this.fileList[0].name);
  184. },
  185. //更新华为线路进度条
  186. updateHwProgress(progress) {
  187. this.hwProgress = progress;
  188. },
  189. //更新腾讯线路进度条
  190. updateTxProgress(progressData) {
  191. this.txProgress = Math.round(progressData.percent * 100);
  192. },
  193. //上传腾讯云Pcdn
  194. async uploadVideoToTxPcdn() {
  195. try {
  196. const file = this.fileList[0].raw;
  197. const data = await uploadObject(file, this.updateTxProgress,this.type);
  198. console.log("腾讯COS返回========>",data);
  199. console.log("isPrivate=======>",this.isPrivate)
  200. let line_1='' ;
  201. if (this.isPrivate===0){
  202. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  203. }else {
  204. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  205. }
  206. line_1 = line_1.replace(/\.mp4$/, '.m3u8');
  207. let urlPathWithoutFirstSlash = data.urlPath.substring(1);
  208. this.$emit("update:fileKey", urlPathWithoutFirstSlash);
  209. console.log("文件key",urlPathWithoutFirstSlash);
  210. console.log("组装URL========>",line_1);
  211. this.$emit("update:videoUrl", line_1);
  212. this.$emit("update:line_1", line_1);
  213. this.$emit("change", line_1,line_1);
  214. // this.$emit("update:line_2", line_2);
  215. this.$message.success("线路一上传成功");
  216. } catch (error) {
  217. this.$message.error("线路一上传失败");
  218. }
  219. },
  220. // //上传华为云Obs
  221. // async uploadVideoToHwObs() {
  222. // try {
  223. // const file = this.fileList[0].raw;
  224. // const data = await uploadToOBS(file, this.updateHwProgress,this.type);
  225. // console.log("华为OBS返回========>",data);
  226. // let line_2='' ;
  227. // if (this.isPrivate===0){
  228. // line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  229. // }else {
  230. // line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  231. // }
  232. // this.$emit("update:videoUrl", line_2);
  233. // this.$emit("update:line_2", line_2);
  234. // this.$message.success("线路二上传成功");
  235. // } catch (error) {
  236. // this.$message.error("线路二上传失败");
  237. // }
  238. // },
  239. handleRemove(file, fileList) {
  240. console.log(file, fileList.length);
  241. },
  242. }
  243. };
  244. </script>
  245. <style>
  246. .progress-container {
  247. margin-bottom: 5px; /* 进度条之间的间距 */
  248. }
  249. .progress-label {
  250. display: block;
  251. font-weight: bold;
  252. font-size: 13px;
  253. color: #303331; /* 标签颜色,可以根据需要调整 */
  254. }
  255. /* 视频库选择对话框样式 */
  256. .library-search {
  257. margin-bottom: 15px;
  258. }
  259. .el-table .el-table__row:hover {
  260. cursor: pointer;
  261. }
  262. </style>