index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <div>
  3. <div class="mt20 ml20">
  4. <Input class="perW35" v-model="videoLink" placeholder="请输入视频链接" />
  5. <input type="file" ref="refid" style="display: none" @change="zh_uploadFile_change" />
  6. <Button
  7. v-if="upload_type !== '1' || videoLink"
  8. type="primary"
  9. icon="ios-cloud-upload-outline"
  10. class="ml10"
  11. @click="zh_uploadFile"
  12. >{{ videoLink ? '确认添加' : '上传视频' }}</Button
  13. >
  14. <Upload
  15. v-if="upload_type === '1' && !videoLink"
  16. :show-upload-list="false"
  17. :action="fileUrl"
  18. class="ml10"
  19. :before-upload="videoSaveToUrl"
  20. :data="uploadData"
  21. :headers="header"
  22. :multiple="true"
  23. style="display: inline-block"
  24. >
  25. <Button type="primary" icon="ios-cloud-upload-outline">上传视频</Button>
  26. </Upload>
  27. <Progress :percent="progress" :stroke-width="5" v-if="upload.videoIng" />
  28. <div class="iview-video-style" v-if="formValidate.video_link">
  29. <video
  30. style="width: 100%; height: 100% !important; border-radius: 10px"
  31. :src="formValidate.video_link"
  32. controls="controls"
  33. >
  34. 您的浏览器不支持 video 标签。
  35. </video>
  36. <div class="mark"></div>
  37. <Icon type="ios-trash-outline" class="iconv" @click="delVideo" />
  38. </div>
  39. </div>
  40. <div class="mt50 ml20">
  41. <Button type="primary" @click="uploads">确认</Button>
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. import { uploadByPieces } from '@/utils/upload'; //引入uploadByPieces方法
  47. import { productGetTempKeysApi, uploadType } from '@/api/product';
  48. import Setting from '@/setting';
  49. import { getCookies } from '@/libs/util';
  50. // import "../../../public/UEditor/dialogs/internal";
  51. export default {
  52. name: 'vide11o',
  53. data() {
  54. return {
  55. fileUrl: Setting.apiBaseURL + '/file/upload',
  56. upload: {
  57. videoIng: false, // 是否显示进度条;
  58. },
  59. progress: 0, // 进度条默认0
  60. videoLink: '',
  61. formValidate: {
  62. video_link: '',
  63. },
  64. upload_type: '',
  65. uploadData: {},
  66. header: {},
  67. };
  68. },
  69. created() {
  70. this.uploadType();
  71. this.getToken();
  72. },
  73. methods: {
  74. videoSaveToUrl(file) {
  75. uploadByPieces({
  76. file: file, // 视频实体
  77. pieceSize: 3, // 分片大小
  78. success: (data) => {
  79. this.formValidate.video_link = data.file_path;
  80. this.progress = 100;
  81. },
  82. error: (e) => {
  83. this.$Message.error(e.msg);
  84. },
  85. uploading: (chunk, allChunk) => {
  86. this.videoIng = true;
  87. let st = Math.floor((chunk / allChunk) * 100);
  88. this.progress = st;
  89. },
  90. });
  91. return false;
  92. },
  93. // 删除视频;
  94. delVideo() {
  95. let that = this;
  96. that.$set(that.formValidate, 'video_link', '');
  97. },
  98. //获取视频上传类型
  99. uploadType() {
  100. uploadType().then((res) => {
  101. this.upload_type = res.data.upload_type;
  102. });
  103. },
  104. // 上传成功
  105. handleSuccess(res, file, fileList) {
  106. if (res.status === 200) {
  107. this.formValidate.video_link = res.data.src;
  108. this.$Message.success(res.msg);
  109. } else {
  110. this.$Message.error(res.msg);
  111. }
  112. },
  113. getToken() {
  114. this.header['Authori-zation'] = 'Bearer ' + getCookies('token');
  115. },
  116. beforeUpload() {
  117. this.uploadData = {};
  118. let promise = new Promise((resolve) => {
  119. this.$nextTick(function () {
  120. resolve(true);
  121. });
  122. });
  123. return promise;
  124. },
  125. zh_uploadFile() {
  126. if (this.videoLink) {
  127. this.formValidate.video_link = this.videoLink;
  128. } else {
  129. this.$refs.refid.click();
  130. }
  131. },
  132. zh_uploadFile_change(evfile) {
  133. let that = this;
  134. if (evfile.target.files[0].type !== 'video/mp4') {
  135. return that.$Message.error('只能上传mp4文件');
  136. }
  137. let types = {
  138. key: evfile.target.files[0].name,
  139. contentType: evfile.target.files[0].type,
  140. };
  141. productGetTempKeysApi(types).then((res) => {
  142. that.$videoCloud
  143. .videoUpload({
  144. type: res.data.type,
  145. evfile: evfile,
  146. res: res,
  147. uploading(status, progress) {
  148. that.upload.videoIng = status;
  149. },
  150. })
  151. .then((res) => {
  152. that.formValidate.video_link = res.url;
  153. that.$Message.success('视频上传成功');
  154. })
  155. .catch((res) => {
  156. that.$Message.error(res);
  157. });
  158. });
  159. },
  160. uploads() {
  161. this.$emit('getVideo', this.formValidate.video_link);
  162. },
  163. },
  164. };
  165. </script>
  166. <style scoped>
  167. .iview-video-style {
  168. width: 40%;
  169. height: 180px;
  170. border-radius: 10px;
  171. background-color: #707070;
  172. margin-top: 10px;
  173. position: relative;
  174. overflow: hidden;
  175. }
  176. .iview-video-style .iconv {
  177. color: #fff;
  178. line-height: 180px;
  179. width: 50px;
  180. height: 50px;
  181. display: inherit;
  182. font-size: 26px;
  183. position: absolute;
  184. top: -74px;
  185. left: 50%;
  186. margin-left: -25px;
  187. }
  188. .iview-video-style .mark {
  189. position: absolute;
  190. width: 100%;
  191. height: 30px;
  192. top: 0;
  193. background-color: rgba(0, 0, 0, 0.5);
  194. text-align: center;
  195. }
  196. </style>