index.vue 6.2 KB

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