index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. <SimpleVideoPlayer
  38. :videoSrc="currentVideoUrl"
  39. ref="videoPlayer"
  40. ></SimpleVideoPlayer>
  41. </div>
  42. </template>
  43. <script>
  44. import OssUploadUtils from './upload'
  45. export default {
  46. props: {
  47. accept: {
  48. type: String,
  49. default: '.mp4',
  50. },
  51. limit: {
  52. type: Number,
  53. default: 9,
  54. },
  55. },
  56. data() {
  57. return {
  58. videoDialog: true,
  59. currentVideoUrl: '',
  60. ossUpload: null,
  61. fileList: [],
  62. }
  63. },
  64. methods: {
  65. // 播放视频
  66. onPlay(rawFile) {
  67. this.currentVideoUrl = URL.createObjectURL(rawFile.file)
  68. this.$refs.videoPlayer.open()
  69. },
  70. // 选择文件
  71. onChooseFile() {
  72. this.$nextTick(() => {
  73. this.$refs.ossFileInput.click()
  74. })
  75. },
  76. // 文件选中
  77. onFileChange(e) {
  78. this.ossUpload = new OssUploadUtils(this, {
  79. success: this.onSuccess,
  80. progress: this.onProgress,
  81. faild: this.onFaild,
  82. })
  83. this.ossUpload.upload(e.target.files[0])
  84. },
  85. // 取消上传
  86. async onAbortUpload(file, index) {
  87. try {
  88. await this.$confirm('确定删除该视频?', {
  89. confirmButtonText: '确定',
  90. cancelButtonText: '取消',
  91. })
  92. this.fileList.splice(index, 1)
  93. const { checkpoint } = file
  94. this.ossUpload.abortMultipartUpload(checkpoint)
  95. } catch (error) {
  96. console.log(error)
  97. }
  98. },
  99. // 上传进度
  100. onProgress(file, fileList) {
  101. this.fileList = fileList
  102. this.$emit('progress', { file, fileList })
  103. },
  104. // 上传成功
  105. onSuccess(file, fileList) {
  106. console.log(file)
  107. this.fileList = fileList
  108. this.$emit('success', { file, fileList })
  109. },
  110. // 上传失败
  111. onFaild(file, fileList) {
  112. this.fileList = fileList
  113. this.$emit('faild', { file, fileList })
  114. },
  115. },
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. @mixin ellipsis($line: 1) {
  120. overflow: hidden;
  121. text-overflow: ellipsis;
  122. display: -webkit-box;
  123. -webkit-line-clamp: $line;
  124. -webkit-box-orient: vertical;
  125. }
  126. @media screen and (min-width: 768px) {
  127. .simple-oss-upload {
  128. .button {
  129. width: 104px;
  130. height: 36px;
  131. text-align: center;
  132. line-height: 36px;
  133. background: #f3920d;
  134. color: #fff;
  135. border-radius: 4px;
  136. cursor: pointer;
  137. }
  138. .file-list {
  139. line-height: initial;
  140. margin-top: 16px;
  141. .file {
  142. padding: 8px 0;
  143. position: relative;
  144. .name {
  145. font-size: 16px;
  146. margin-bottom: 18px;
  147. margin-right: 80px;
  148. color: #666;
  149. }
  150. .control {
  151. position: absolute;
  152. top: 8px;
  153. right: 0;
  154. span {
  155. font-size: 16px;
  156. cursor: pointer;
  157. &.play {
  158. color: #1890ff;
  159. margin-right: 12px;
  160. }
  161. &.remove {
  162. color: #f3920d;
  163. }
  164. }
  165. }
  166. .progress {
  167. padding-right: 120px;
  168. position: relative;
  169. .status {
  170. position: absolute;
  171. right: 0;
  172. top: 50%;
  173. transform: translateY(-53%);
  174. font-size: 16px;
  175. color: #f3920d;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. @media screen and (max-width: 768px) {
  183. .simple-oss-upload {
  184. .button {
  185. width: 24vw;
  186. height: 8.8vw;
  187. text-align: center;
  188. line-height: 8.8vw;
  189. background: #f3920d;
  190. color: #fff;
  191. border-radius: 0.4vw;
  192. font-size: 3.2vw;
  193. cursor: pointer;
  194. }
  195. .file-list {
  196. line-height: initial;
  197. margin-top: 3.2vw;
  198. .file {
  199. position: relative;
  200. margin-bottom: 7.2vw;
  201. &:last-child {
  202. margin-bottom: 0;
  203. }
  204. .name {
  205. font-size: 3.2vw;
  206. margin-bottom: 3.2vw;
  207. margin-right: 22vw;
  208. color: #666;
  209. }
  210. .control {
  211. position: absolute;
  212. top: 0.2vw;
  213. right: 0;
  214. span {
  215. font-size: 3.2vw;
  216. cursor: pointer;
  217. &.play {
  218. color: #1890ff;
  219. margin-right: 2.8vw;
  220. }
  221. &.remove {
  222. color: #f3920d;
  223. }
  224. }
  225. }
  226. .progress {
  227. padding-right: 22vw;
  228. position: relative;
  229. .status {
  230. position: absolute;
  231. right: 0;
  232. top: 50%;
  233. transform: translateY(-53%);
  234. font-size: 3.2vw;
  235. color: #f3920d;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. </style>