index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <div class="simple-oss-upload">
  3. <template v-if="fileList.length < limit">
  4. <div class="button" @click="onChooseFile">本地上传</div>
  5. <input
  6. v-show="false"
  7. id="file"
  8. ref="ossFileInput"
  9. type="file"
  10. :accept="accept"
  11. multiple
  12. @change="onFileChange"
  13. />
  14. </template>
  15. <div class="file-list">
  16. <template v-for="(file, index) in fileList">
  17. <div class="file" :key="file.uuid">
  18. <div class="name">{{ file.name }}</div>
  19. <div class="control">
  20. <span class="play" @click="onPlay(file)">播放</span>
  21. <span class="remove" @click="onAbortUpload(file, index)">删除</span>
  22. </div>
  23. <div class="progress">
  24. <el-progress
  25. :percentage="file.percentage"
  26. :show-text="false"
  27. color="#F3920D"
  28. ></el-progress>
  29. <div class="status">
  30. <span>{{ file.percentage }}%</span>
  31. <span>上传中</span>
  32. </div>
  33. </div>
  34. </div>
  35. </template>
  36. </div>
  37. <!-- <template v-if="videoDialog">
  38. <div class="video-dialog">
  39. <video :src="currentVideoUrl"></video>
  40. </div>
  41. <div class="mask"></div>
  42. </template> -->
  43. <SimpleVideoPlayer
  44. :videoSrc="currentVideoUrl"
  45. ref="videoPlayer"
  46. ></SimpleVideoPlayer>
  47. </div>
  48. </template>
  49. <script>
  50. import OssUploadUtils from './upload'
  51. export default {
  52. props: {
  53. accept: {
  54. type: String,
  55. default: '.mp4',
  56. },
  57. limit: {
  58. type: Number,
  59. default: 9,
  60. },
  61. },
  62. data() {
  63. return {
  64. videoDialog: true,
  65. currentVideoUrl: '',
  66. ossUpload: null,
  67. fileList: [{ percentage: 90, name: 'a8b25605e0db0c9be8362c38d4.mp4' }],
  68. }
  69. },
  70. methods: {
  71. // 播放视频
  72. onPlay(rawFile) {
  73. this.currentVideoUrl = URL.createObjectURL(rawFile.file)
  74. this.$refs.videoPlayer.open()
  75. },
  76. // 选择文件
  77. onChooseFile() {
  78. this.$nextTick(() => {
  79. this.$refs.ossFileInput.click()
  80. })
  81. },
  82. // 文件选中
  83. onFileChange(e) {
  84. this.ossUpload = new OssUploadUtils(this, {
  85. success: this.onSuccess,
  86. progress: this.onProgress,
  87. faild: this.onFaild,
  88. })
  89. this.ossUpload.upload(e.target.files[0])
  90. },
  91. // 取消上传
  92. async onAbortUpload(file, index) {
  93. try {
  94. await this.$confirm('确定删除该视频?', {
  95. confirmButtonText: '确定',
  96. cancelButtonText: '取消',
  97. })
  98. this.fileList.splice(index, 1)
  99. const { checkpoint } = file
  100. this.ossUpload.abortMultipartUpload(checkpoint)
  101. } catch (error) {
  102. console.log(error)
  103. }
  104. },
  105. // 上传进度
  106. onProgress(file, fileList) {
  107. this.fileList = fileList
  108. this.$emit('progress', { file, fileList })
  109. },
  110. // 上传成功
  111. onSuccess(file, fileList) {
  112. console.log(file)
  113. this.fileList = fileList
  114. this.$emit('success', { file, fileList })
  115. },
  116. // 上传失败
  117. onFaild(file, fileList) {
  118. this.fileList = fileList
  119. this.$emit('faild', { file, fileList })
  120. },
  121. },
  122. }
  123. </script>
  124. <style lang="scss" scoped>
  125. @mixin ellipsis($line: 1) {
  126. overflow: hidden;
  127. text-overflow: ellipsis;
  128. display: -webkit-box;
  129. -webkit-line-clamp: $line;
  130. -webkit-box-orient: vertical;
  131. }
  132. @media screen and (min-width: 768px) {
  133. .simple-oss-upload {
  134. .button {
  135. width: 104px;
  136. height: 36px;
  137. text-align: center;
  138. line-height: 36px;
  139. background: #f3920d;
  140. color: #fff;
  141. border-radius: 4px;
  142. cursor: pointer;
  143. }
  144. .video-dialog {
  145. position: fixed;
  146. top: 50%;
  147. left: 50%;
  148. transform: translate(-50%, -50%);
  149. width: 800px;
  150. height: 600px;
  151. z-index: 11;
  152. background: #fff;
  153. border-radius: 4px;
  154. video {
  155. display: block;
  156. width: 100%;
  157. height: 100%;
  158. }
  159. }
  160. .mask {
  161. position: fixed;
  162. left: 0;
  163. top: 0;
  164. width: 100vw;
  165. height: 100vh;
  166. background: #000;
  167. opacity: 0.5;
  168. z-index: 10;
  169. }
  170. .file-list {
  171. line-height: initial;
  172. margin-top: 16px;
  173. .file {
  174. padding: 8px 0;
  175. position: relative;
  176. .name {
  177. font-size: 16px;
  178. margin-bottom: 18px;
  179. margin-right: 80px;
  180. color: #666;
  181. }
  182. .control {
  183. position: absolute;
  184. top: 8px;
  185. right: 0;
  186. span {
  187. font-size: 16px;
  188. cursor: pointer;
  189. &.play {
  190. color: #1890ff;
  191. margin-right: 12px;
  192. }
  193. &.remove {
  194. color: #f3920d;
  195. }
  196. }
  197. }
  198. .progress {
  199. padding-right: 120px;
  200. position: relative;
  201. .status {
  202. position: absolute;
  203. right: 0;
  204. top: 50%;
  205. transform: translateY(-53%);
  206. font-size: 16px;
  207. color: #f3920d;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. </style>