index.vue 5.8 KB

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