index.vue 8.5 KB

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