index.vue 5.9 KB

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