index.vue 5.4 KB

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