index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <el-dialog
  3. :before-close="handleClose"
  4. :close-on-click-modal="false"
  5. :title="title"
  6. :visible.sync="dialogFormVisible"
  7. width="909px"
  8. >
  9. <div class="upload">
  10. <el-alert
  11. :closable="false"
  12. :title="`支持jpg、jpeg、png格式,单次可最多选择${limit}张图片,每张不可大于${size}M,如果大于${size}M会自动为您过滤`"
  13. type="info"
  14. ></el-alert>
  15. <br />
  16. <el-upload
  17. ref="upload"
  18. :action="action"
  19. :auto-upload="false"
  20. :close-on-click-modal="false"
  21. :data="data"
  22. :file-list="fileList"
  23. :headers="headers"
  24. :limit="limit"
  25. :multiple="true"
  26. :name="name"
  27. :on-change="handleChange"
  28. :on-error="handleError"
  29. :on-exceed="handleExceed"
  30. :on-preview="handlePreview"
  31. :on-progress="handleProgress"
  32. :on-remove="handleRemove"
  33. :on-success="handleSuccess"
  34. accept="image/png, image/jpeg"
  35. class="upload-content"
  36. list-type="picture-card"
  37. >
  38. <i slot="trigger" class="el-icon-plus"></i>
  39. <el-dialog
  40. :visible.sync="dialogVisible"
  41. append-to-body
  42. title="查看大图"
  43. >
  44. <div>
  45. <img :src="dialogImageUrl" alt="" width="100%" />
  46. </div>
  47. </el-dialog>
  48. </el-upload>
  49. </div>
  50. <div
  51. slot="footer"
  52. class="dialog-footer"
  53. style="position: relative; padding-right: 15px; text-align: right"
  54. >
  55. <div
  56. v-if="show"
  57. style="position: absolute; top: 10px; left: 15px; color: #999"
  58. >
  59. 正在上传中... 当前上传成功数:{{ imgSuccessNum }}张 当前上传失败数:{{
  60. imgErrorNum
  61. }}张
  62. </div>
  63. <el-button type="primary" @click="handleClose">关闭</el-button>
  64. <el-button
  65. :loading="loading"
  66. size="small"
  67. style="margin-left: 10px"
  68. type="success"
  69. @click="submitUpload"
  70. >
  71. 开始上传
  72. </el-button>
  73. </div>
  74. </el-dialog>
  75. </template>
  76. <script>
  77. import { baseURL, tokenName } from '@/config'
  78. export default {
  79. name: 'VabUpload',
  80. props: {
  81. url: {
  82. type: String,
  83. default: '/upload',
  84. required: true,
  85. },
  86. name: {
  87. type: String,
  88. default: 'file',
  89. required: true,
  90. },
  91. limit: {
  92. type: Number,
  93. default: 50,
  94. required: true,
  95. },
  96. size: {
  97. type: Number,
  98. default: 1,
  99. required: true,
  100. },
  101. },
  102. data() {
  103. return {
  104. show: false,
  105. loading: false,
  106. dialogVisible: false,
  107. dialogImageUrl: '',
  108. action: 'https://vab-unicloud-3a9da9.service.tcloudbase.com/upload',
  109. headers: {},
  110. fileList: [],
  111. picture: 'picture',
  112. imgNum: 0,
  113. imgSuccessNum: 0,
  114. imgErrorNum: 0,
  115. typeList: null,
  116. title: '上传',
  117. dialogFormVisible: false,
  118. data: {},
  119. }
  120. },
  121. computed: {
  122. percentage() {
  123. if (this.allImgNum == 0) return 0
  124. return this.$baseLodash.round(this.imgNum / this.allImgNum, 2) * 100
  125. },
  126. },
  127. methods: {
  128. submitUpload() {
  129. this.$refs.upload.submit()
  130. },
  131. handleProgress(event, file, fileList) {
  132. this.loading = true
  133. this.show = true
  134. },
  135. handleChange(file, fileList) {
  136. if (file.size > 1048576 * this.size) {
  137. fileList.map((item, index) => {
  138. if (item === file) {
  139. fileList.splice(index, 1)
  140. }
  141. })
  142. this.fileList = fileList
  143. } else {
  144. this.allImgNum = fileList.length
  145. }
  146. },
  147. handleSuccess(response, file, fileList) {
  148. this.imgNum = this.imgNum + 1
  149. this.imgSuccessNum = this.imgSuccessNum + 1
  150. if (fileList.length === this.imgNum) {
  151. setTimeout(() => {
  152. this.$baseMessage(
  153. `上传完成! 共上传${fileList.length}张图片`,
  154. 'success'
  155. )
  156. }, 1000)
  157. }
  158. setTimeout(() => {
  159. this.loading = false
  160. this.show = false
  161. }, 1000)
  162. },
  163. handleError(err, file, fileList) {
  164. this.imgNum = this.imgNum + 1
  165. this.imgErrorNum = this.imgErrorNum + 1
  166. this.$baseMessage(
  167. `文件[${file.raw.name}]上传失败,文件大小为${this.$baseLodash.round(
  168. file.raw.size / 1024,
  169. 0
  170. )}KB`,
  171. 'error'
  172. )
  173. setTimeout(() => {
  174. this.loading = false
  175. this.show = false
  176. }, 1000)
  177. },
  178. handleRemove(file, fileList) {
  179. this.imgNum = this.imgNum - 1
  180. this.allNum = this.allNum - 1
  181. },
  182. handlePreview(file) {
  183. this.dialogImageUrl = file.url
  184. this.dialogVisible = true
  185. },
  186. handleExceed(files, fileList) {
  187. this.$baseMessage(
  188. `当前限制选择 ${this.limit} 个文件,本次选择了
  189. ${files.length}
  190. 个文件`,
  191. 'error'
  192. )
  193. },
  194. handleShow(data) {
  195. this.title = '上传'
  196. this.data = data
  197. this.dialogFormVisible = true
  198. },
  199. handleClose() {
  200. this.fileList = []
  201. this.picture = 'picture'
  202. this.allImgNum = 0
  203. this.imgNum = 0
  204. this.imgSuccessNum = 0
  205. this.imgErrorNum = 0
  206. /* if ("development" === process.env.NODE_ENV) {
  207. this.api = process.env.VUE_APP_BASE_API;
  208. } else {
  209. this.api = `${window.location.protocol}//${window.location.host}`;
  210. }
  211. this.action = this.api + this.url; */
  212. this.dialogFormVisible = false
  213. },
  214. },
  215. }
  216. </script>
  217. <style lang="scss" scoped>
  218. .upload {
  219. height: 500px;
  220. .upload-content {
  221. .el-upload__tip {
  222. display: block;
  223. height: 30px;
  224. line-height: 30px;
  225. }
  226. ::v-deep {
  227. .el-upload--picture-card {
  228. width: 128px;
  229. height: 128px;
  230. margin: 3px 8px 8px 8px;
  231. border: 2px dashed #c0ccda;
  232. }
  233. .el-upload-list--picture {
  234. margin-bottom: 20px;
  235. }
  236. .el-upload-list--picture-card {
  237. .el-upload-list__item {
  238. width: 128px;
  239. height: 128px;
  240. margin: 3px 8px 8px 8px;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. </style>