index.vue 6.4 KB

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