UploadVideo.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div>
  3. <el-dialog
  4. v-loading="loading"
  5. :show-close="false"
  6. :close-on-click-modal="false"
  7. :visible.sync="isShow"
  8. :title="curObj.id ? '上传视频' : '上传视频'"
  9. :fullscreen="false"
  10. width="500px"
  11. custom-class="xl-dialog"
  12. center
  13. >
  14. <div class="tip">上传新的视频:请等待<span class="b">上传成功</span>后再点击顶部的确定保存</div>
  15. <el-upload
  16. class="upload-demo"
  17. ref="upload"
  18. action="https://jsonplaceholder.typicode.com/posts/"
  19. :on-preview="handlePreview"
  20. :on-remove="handleRemove"
  21. :multiple="false"
  22. :limit="1"
  23. :file-list="fileList"
  24. @change="fileChange"
  25. :http-request="submitUpload"
  26. :auto-upload="true">
  27. <el-button v-if="!cObj.video" slot="trigger" size="small" type="primary">选取新视频</el-button>
  28. <el-button v-if="fileNextResObj.total && fileNextResObj.total.percent === 100" size="small" type="success" style="margin-left: 10px;">{{ fileNextResObj.total ? fileNextResObj.total.percent === 100 ? '上传成功' :`上传中${parseInt(fileNextResObj.total.percent)}%` : '上传到服务器'}}</el-button>
  29. <el-progress v-else-if="fileNextResObj.total" class="tip3" :text-inside="true" :stroke-width="20" :percentage="parseInt(fileNextResObj.total.percent)"></el-progress>
  30. <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> -->
  31. <!-- v-else-if="fileNextResObj.total" fileNextResObj.total.percent-->
  32. </el-upload>
  33. <div class="xl-form">
  34. <div class="xl-form-footer">
  35. <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
  36. <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
  37. </div>
  38. </div>
  39. </el-dialog>
  40. </div>
  41. </template>
  42. <script>
  43. import * as qiniu from 'qiniu-js'
  44. import { convertFileName } from '@/utils'
  45. export default {
  46. mixins,
  47. props: {
  48. isShow: Boolean,
  49. curFrom: String,
  50. curObj: Object
  51. },
  52. inject: ['parentData'],
  53. data() {
  54. return {
  55. loading: false,
  56. cObj: {},
  57. fileList: [],
  58. fileConfig: {},
  59. fileNextResObj: {},
  60. fileCompleteResObj: {},
  61. }
  62. },
  63. mixins: [...mixins],
  64. watch: {
  65. isShow: function(val) {
  66. if (val) {
  67. this.fileList= []
  68. this.fileNextResObj = {}
  69. let cObj = this.cObj
  70. cObj.video = ''
  71. this.cObj = {...cObj}
  72. this.$api.base.uploadvideotoken().then(res => {
  73. this.fileConfig = res || {}
  74. })
  75. }
  76. },
  77. },
  78. computed: {
  79. },
  80. mounted() {
  81. },
  82. methods: {
  83. submitUpload() {
  84. console.log('submitUpload')
  85. if (this.fileNextResObj.total) {
  86. this.$msgConfrm('正在上传中,如需重新上传,请保存当前页面重新上传')
  87. return
  88. }
  89. const upload = this.$refs.upload || {}
  90. const curFile = upload.uploadFiles[0] || {}
  91. let config = {
  92. useCdnDomain: true,
  93. region: qiniu.region.z2,
  94. debugLogLevel: 'INFO'
  95. }
  96. let putExtra = {
  97. fname: '',
  98. params: {},
  99. mimeType: null
  100. }
  101. let next = (response) =>{
  102. console.log(response, 11)
  103. this.fileNextResObj = response
  104. }
  105. let complete = (response) =>{
  106. console.log(response, 22)
  107. let cObj = this.cObj
  108. cObj.video = `${this.fileConfig.domain}/${response.key}`
  109. this.cObj = {...cObj}
  110. console.log(cObj, 333)
  111. }
  112. let subscription;
  113. // 调用sdk上传接口获得相应的observable,控制上传和暂停
  114. // let observable = qiniu.upload(curFile.raw, curFile.name, this.fileConfig.token, putExtra, config);
  115. let observable = qiniu.upload(curFile.raw, this.curFrom+convertFileName(curFile.name, 'onlyName'), this.fileConfig.token, putExtra, config);
  116. observable.subscribe(next, null, complete)
  117. // this.$refs.upload.submit();
  118. },
  119. handleRemove(file, fileList) {
  120. this.fileNextResObj = {}
  121. let cObj = this.cObj
  122. cObj.video = ''
  123. this.cObj = {...cObj}
  124. console.log(file, fileList);
  125. },
  126. handlePreview(file) {
  127. console.log(file);
  128. },
  129. fileChange (e) {
  130. console.log(e)
  131. },
  132. close (str) {
  133. if (str === 'confirm') {
  134. if (this.cObj.video) {
  135. this.$emit('close', this.cObj)
  136. } else {
  137. this.$msgConfrm('请上传视频')
  138. }
  139. } else {
  140. this.$emit('close', {})
  141. }
  142. },
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. .lib-edit {
  148. padding-top: 0;
  149. width: 500px;
  150. ::v-deep .el-date-editor.el-input {
  151. width: 100%;
  152. }
  153. }
  154. .tip {
  155. color: #f00;
  156. .b {
  157. font-weight: bold;
  158. display: inline-block;
  159. font-size: 20px;
  160. }
  161. }
  162. .tip2 {
  163. display: inline-block;
  164. width: 300px;
  165. color: #606266;
  166. }
  167. .tip3 {
  168. display: inline-block;
  169. margin-left: 10px;
  170. width: 300px;
  171. }
  172. .i {
  173. border: 0;
  174. border-bottom: 1px solid #dcdcdc;
  175. width: 50px;
  176. text-align: center;
  177. outline: none;
  178. }
  179. .dot {
  180. display: inline-block;
  181. width: 50px;
  182. &.t2 {
  183. text-align: center;
  184. }
  185. }
  186. </style>