index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <div class="component-upload-image">
  3. <el-upload
  4. multiple
  5. :action="uploadImgUrl"
  6. list-type="picture-card"
  7. :on-success="handleUploadSuccess"
  8. :before-upload="handleBeforeUpload"
  9. :data="data"
  10. :limit="limit"
  11. :on-error="handleUploadError"
  12. :on-exceed="handleExceed"
  13. ref="imageUpload"
  14. :on-remove="handleDelete"
  15. :show-file-list="true"
  16. :headers="headers"
  17. :file-list="fileList"
  18. :on-preview="handlePictureCardPreview"
  19. :class="{hide: this.fileList.length >= this.limit}"
  20. >
  21. <i class="el-icon-plus"></i>
  22. </el-upload>
  23. <!-- 上传提示 -->
  24. <div class="el-upload__tip" slot="tip" v-if="showTip">
  25. 请上传
  26. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  27. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  28. 的文件
  29. </div>
  30. <el-dialog
  31. :visible.sync="dialogVisible"
  32. title="预览"
  33. width="800"
  34. append-to-body
  35. >
  36. <img
  37. :src="dialogImageUrl"
  38. style="display: block; max-width: 100%; margin: 0 auto"
  39. />
  40. </el-dialog>
  41. </div>
  42. </template>
  43. <script>
  44. import { getToken } from "@/utils/auth"
  45. import { isExternal } from "@/utils/validate"
  46. export default {
  47. props: {
  48. value: [String, Object, Array],
  49. // 上传接口地址
  50. action: {
  51. type: String,
  52. default: "/common/upload"
  53. },
  54. // 上传携带的参数
  55. data: {
  56. type: Object
  57. },
  58. // 图片数量限制
  59. limit: {
  60. type: Number,
  61. default: 5,
  62. },
  63. // 大小限制(MB)
  64. fileSize: {
  65. type: Number,
  66. default: 5,
  67. },
  68. // 文件类型, 例如['png', 'jpg', 'jpeg']
  69. fileType: {
  70. type: Array,
  71. default: () => ["png", "jpg", "jpeg"],
  72. },
  73. // 是否显示提示
  74. isShowTip: {
  75. type: Boolean,
  76. default: true
  77. }
  78. },
  79. data() {
  80. return {
  81. number: 0,
  82. uploadList: [],
  83. dialogImageUrl: "",
  84. dialogVisible: false,
  85. hideUpload: false,
  86. baseUrl: process.env.VUE_APP_BASE_API,
  87. uploadImgUrl: process.env.VUE_APP_BASE_API + this.action, // 上传的图片服务器地址
  88. headers: {
  89. Authorization: "Bearer " + getToken(),
  90. },
  91. fileList: []
  92. }
  93. },
  94. watch: {
  95. value: {
  96. handler(val) {
  97. if (val) {
  98. // 首先将值转为数组
  99. const list = Array.isArray(val) ? val : this.value.split(',')
  100. // 然后将数组转为对象数组
  101. this.fileList = list.map(item => {
  102. if (typeof item === "string") {
  103. if (item.indexOf(this.baseUrl) === -1 && !isExternal(item)) {
  104. item = { name: this.baseUrl + item, url: this.baseUrl + item }
  105. } else {
  106. item = { name: item, url: item }
  107. }
  108. }
  109. return item
  110. })
  111. } else {
  112. this.fileList = []
  113. return []
  114. }
  115. },
  116. deep: true,
  117. immediate: true
  118. }
  119. },
  120. computed: {
  121. // 是否显示提示
  122. showTip() {
  123. return this.isShowTip && (this.fileType || this.fileSize)
  124. },
  125. },
  126. methods: {
  127. // 上传前loading加载
  128. handleBeforeUpload(file) {
  129. let isImg = false
  130. if (this.fileType.length) {
  131. let fileExtension = ""
  132. if (file.name.lastIndexOf(".") > -1) {
  133. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1)
  134. }
  135. isImg = this.fileType.some(type => {
  136. if (file.type.indexOf(type) > -1) return true
  137. if (fileExtension && fileExtension.indexOf(type) > -1) return true
  138. return false
  139. })
  140. } else {
  141. isImg = file.type.indexOf("image") > -1
  142. }
  143. if (!isImg) {
  144. this.$modal.msgError(`文件格式不正确,请上传${this.fileType.join("/")}图片格式文件!`)
  145. return false
  146. }
  147. if (file.name.includes(',')) {
  148. this.$modal.msgError('文件名不正确,不能包含英文逗号!')
  149. return false
  150. }
  151. if (this.fileSize) {
  152. const isLt = file.size / 1024 / 1024 < this.fileSize
  153. if (!isLt) {
  154. this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`)
  155. return false
  156. }
  157. }
  158. this.$modal.loading("正在上传图片,请稍候...")
  159. this.number++
  160. },
  161. // 文件个数超出
  162. handleExceed() {
  163. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`)
  164. },
  165. // 上传成功回调
  166. handleUploadSuccess(res, file) {
  167. if (res.code === 200) {
  168. this.uploadList.push({ name: res.fileName, url: res.fileName })
  169. this.uploadedSuccessfully()
  170. } else {
  171. this.number--
  172. this.$modal.closeLoading()
  173. this.$modal.msgError(res.msg)
  174. this.$refs.imageUpload.handleRemove(file)
  175. this.uploadedSuccessfully()
  176. }
  177. },
  178. // 删除图片
  179. handleDelete(file) {
  180. const findex = this.fileList.map(f => f.name).indexOf(file.name)
  181. if (findex > -1) {
  182. this.fileList.splice(findex, 1)
  183. this.$emit("input", this.listToString(this.fileList))
  184. }
  185. },
  186. // 上传失败
  187. handleUploadError() {
  188. this.$modal.msgError("上传图片失败,请重试")
  189. this.$modal.closeLoading()
  190. },
  191. // 上传结束处理
  192. uploadedSuccessfully() {
  193. if (this.number > 0 && this.uploadList.length === this.number) {
  194. this.fileList = this.fileList.concat(this.uploadList)
  195. this.uploadList = []
  196. this.number = 0
  197. this.$emit("input", this.listToString(this.fileList))
  198. this.$modal.closeLoading()
  199. }
  200. },
  201. // 预览
  202. handlePictureCardPreview(file) {
  203. this.dialogImageUrl = file.url
  204. this.dialogVisible = true
  205. },
  206. // 对象转成指定字符串分隔
  207. listToString(list, separator) {
  208. let strs = ""
  209. separator = separator || ","
  210. for (let i in list) {
  211. if (list[i].url) {
  212. strs += list[i].url.replace(this.baseUrl, "") + separator
  213. }
  214. }
  215. return strs != '' ? strs.substr(0, strs.length - 1) : ''
  216. }
  217. }
  218. }
  219. </script>
  220. <style scoped lang="scss">
  221. // .el-upload--picture-card 控制加号部分
  222. ::v-deep.hide .el-upload--picture-card {
  223. display: none;
  224. }
  225. // 去掉动画效果
  226. ::v-deep .el-list-enter-active,
  227. ::v-deep .el-list-leave-active {
  228. transition: all 0s;
  229. }
  230. ::v-deep .el-list-enter, .el-list-leave-active {
  231. opacity: 0;
  232. transform: translateY(0);
  233. }
  234. </style>