index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. name="file"
  13. :on-remove="handleRemove"
  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. // todo /infra/file/upload 返回的都是带 host 的可访问地址, baseUrl 有点没必要
  76. baseUrl: process.env.VUE_APP_BASE_API,
  77. uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 上传的图片服务器地址
  78. headers: {
  79. Authorization: "Bearer " + getToken(),
  80. },
  81. fileList: []
  82. };
  83. },
  84. watch: {
  85. value: {
  86. handler(val) {
  87. if (val) {
  88. // 首先将值转为数组
  89. const list = Array.isArray(val) ? val : this.value.split(',');
  90. // 然后将数组转为对象数组
  91. this.fileList = list.map(item => {
  92. if (typeof item === "string") {
  93. // 不带有 http 的路径, 才拼接 baseUrl.
  94. if (item.indexOf("http") === -1) {
  95. item = {name: this.baseUrl + item, url: this.baseUrl + item};
  96. } else {
  97. item = {name: item, url: item};
  98. }
  99. }
  100. return item;
  101. });
  102. } else {
  103. this.fileList = [];
  104. return [];
  105. }
  106. },
  107. deep: true,
  108. immediate: true
  109. }
  110. },
  111. computed: {
  112. // 是否显示提示
  113. showTip() {
  114. return this.isShowTip && (this.fileType || this.fileSize);
  115. },
  116. },
  117. methods: {
  118. // 删除图片
  119. handleRemove(file, fileList) {
  120. const findex = this.fileList.map(f => f.name).indexOf(file.name);
  121. if(findex > -1) {
  122. this.fileList.splice(findex, 1);
  123. this.$emit("input", this.listToString(this.fileList));
  124. }
  125. },
  126. // 上传成功回调
  127. handleUploadSuccess(res) {
  128. this.uploadList.push({name: res.data.fileName, url: res.data.fileUrl});
  129. if (this.uploadList.length === this.number) {
  130. this.fileList = this.fileList.concat(this.uploadList);
  131. this.uploadList = [];
  132. this.number = 0;
  133. this.$emit("input", this.listToString(this.fileList));
  134. this.$modal.closeLoading();
  135. }
  136. },
  137. // 上传前loading加载
  138. handleBeforeUpload(file) {
  139. let isImg = false;
  140. if (this.fileType.length) {
  141. let fileExtension = "";
  142. if (file.name.lastIndexOf(".") > -1) {
  143. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  144. }
  145. isImg = this.fileType.some(type => {
  146. if (file.type.indexOf(type) > -1) return true;
  147. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  148. return false;
  149. });
  150. } else {
  151. isImg = file.type.indexOf("image") > -1;
  152. }
  153. if (!isImg) {
  154. this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
  155. return false;
  156. }
  157. if (this.fileSize) {
  158. const isLt = file.size / 1024 / 1024 < this.fileSize;
  159. if (!isLt) {
  160. this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
  161. return false;
  162. }
  163. }
  164. this.$modal.loading("正在上传图片,请稍候...");
  165. this.number++;
  166. },
  167. // 文件个数超出
  168. handleExceed() {
  169. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
  170. },
  171. // 上传失败
  172. handleUploadError() {
  173. this.$modal.msgError("上传图片失败,请重试");
  174. this.$modal.closeLoading();
  175. },
  176. // 预览
  177. handlePictureCardPreview(file) {
  178. this.dialogImageUrl = file.url;
  179. this.dialogVisible = true;
  180. },
  181. // 对象转成指定字符串分隔
  182. listToString(list, separator) {
  183. let strs = "";
  184. separator = separator || ",";
  185. for (let i in list) {
  186. // 由于 infra-file 返回带有 host , 不需要替换 baseUrl // .replace(this.baseUrl, "")
  187. strs += list[i].url + separator;
  188. }
  189. return strs != '' ? strs.substr(0, strs.length - 1) : '';
  190. }
  191. }
  192. };
  193. </script>
  194. <style scoped lang="scss">
  195. // .el-upload--picture-card 控制加号部分
  196. ::v-deep.hide .el-upload--picture-card {
  197. display: none;
  198. }
  199. // 去掉动画效果
  200. ::v-deep .el-list-enter-active,
  201. ::v-deep .el-list-leave-active {
  202. transition: all 0s;
  203. }
  204. ::v-deep .el-list-enter, .el-list-leave-active {
  205. opacity: 0;
  206. transform: translateY(0);
  207. }
  208. </style>