index.vue 6.4 KB

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