index.vue 6.3 KB

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