index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. @mixin ellipsis($line: 1) {
  118. overflow: hidden;
  119. text-overflow: ellipsis;
  120. display: -webkit-box;
  121. -webkit-line-clamp: $line;
  122. -webkit-box-orient: vertical;
  123. }
  124. @media screen and (min-width: 768px) {
  125. .simple-oss-upload {
  126. .button {
  127. width: 104px;
  128. height: 36px;
  129. text-align: center;
  130. line-height: 36px;
  131. background: #f3920d;
  132. color: #fff;
  133. border-radius: 4px;
  134. cursor: pointer;
  135. }
  136. .file-list {
  137. line-height: initial;
  138. margin-top: 16px;
  139. .file {
  140. padding: 8px 0;
  141. position: relative;
  142. .name {
  143. font-size: 16px;
  144. margin-bottom: 18px;
  145. margin-right: 80px;
  146. color: #666;
  147. }
  148. .control {
  149. position: absolute;
  150. top: 8px;
  151. right: 0;
  152. span {
  153. font-size: 16px;
  154. cursor: pointer;
  155. &.play {
  156. color: #1890ff;
  157. margin-right: 12px;
  158. }
  159. &.remove {
  160. color: #f3920d;
  161. }
  162. }
  163. }
  164. .progress {
  165. padding-right: 120px;
  166. position: relative;
  167. .status {
  168. position: absolute;
  169. right: 0;
  170. top: 50%;
  171. transform: translateY(-53%);
  172. font-size: 16px;
  173. color: #f3920d;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. @media screen and (max-width: 768px) {
  181. .simple-oss-upload {
  182. .button {
  183. width: 24vw;
  184. height: 8.8vw;
  185. text-align: center;
  186. line-height: 8.8vw;
  187. background: #f3920d;
  188. color: #fff;
  189. border-radius: 0.4vw;
  190. font-size: 3.2vw;
  191. cursor: pointer;
  192. }
  193. .file-list {
  194. line-height: initial;
  195. margin-top: 3.2vw;
  196. .file {
  197. position: relative;
  198. margin-bottom: 7.2vw;
  199. &:last-child {
  200. margin-bottom: 0;
  201. }
  202. .name {
  203. font-size: 3.2vw;
  204. margin-bottom: 3.2vw;
  205. margin-right: 22vw;
  206. color: #666;
  207. }
  208. .control {
  209. position: absolute;
  210. top: 0.2vw;
  211. right: 0;
  212. span {
  213. font-size: 3.2vw;
  214. cursor: pointer;
  215. &.play {
  216. color: #1890ff;
  217. margin-right: 2.8vw;
  218. }
  219. &.remove {
  220. color: #f3920d;
  221. }
  222. }
  223. }
  224. .progress {
  225. padding-right: 22vw;
  226. position: relative;
  227. .status {
  228. position: absolute;
  229. right: 0;
  230. top: 50%;
  231. transform: translateY(-53%);
  232. font-size: 3.2vw;
  233. color: #f3920d;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. </style>