index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <div class="upload">
  3. <el-upload class="avatar-uploader"
  4. style="line-height: 0;display: inline-block"
  5. action="/api-web/uploadFile"
  6. :headers="headers"
  7. :show-file-list="false"
  8. v-loading="uploadImgLoading"
  9. accept=".jpg, .jpeg, .png"
  10. :on-success="handleImgSuccess"
  11. :on-error="handleUploadImgError"
  12. :before-upload="beforeImgUpload">
  13. <img v-if="imgUrl"
  14. :src="imgUrl"
  15. class="avatar" />
  16. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  17. </el-upload>
  18. </div>
  19. </template>
  20. <script>
  21. import load from "@/utils/loading";
  22. import { getToken } from "@/utils/auth";
  23. export default {
  24. data() {
  25. return {
  26. headers: {
  27. Authorization: getToken()
  28. },
  29. uploadImgLoading: false,
  30. imgUrl: null,
  31. };
  32. },
  33. props: {
  34. value: {
  35. // 组件状态
  36. type: String,
  37. },
  38. imageSizeM: { // 默认2M
  39. type: Number,
  40. default() {
  41. return 2
  42. }
  43. },
  44. imageType: { // 检测类型
  45. type: Object,
  46. default() {
  47. return {
  48. "image/png": true,
  49. "image/jpeg": true,
  50. "image/jpg": true,
  51. }
  52. }
  53. }
  54. },
  55. mounted() {},
  56. methods: {
  57. beforeImgUpload (file) {
  58. const isImage = this.imageType[file.type];
  59. const isLt2M = file.size / 1024 / 1024 < this.imageSizeM;
  60. console.log(isImage, isLt2M)
  61. if (!isImage) {
  62. this.$message.error("只能上传图片格式!");
  63. }
  64. if (!isLt2M) {
  65. this.$message.error("上传图片大小不能超过 2MB!");
  66. }
  67. if (isImage && isLt2M) {
  68. this.uploadImgLoading = true
  69. }
  70. return isImage && isLt2M;
  71. },
  72. handleUploadImgError (file) {
  73. this.uploadImgLoading = false
  74. this.$message.error('上传失败')
  75. },
  76. handleImgSuccess (res, file) {
  77. this.uploadImgLoading = false
  78. this.imgUrl = res.data.url
  79. },
  80. },
  81. watch: {
  82. value(newValue) {
  83. console.log(newValue);
  84. this.imgUrl = newValue
  85. },
  86. },
  87. beforeDestroy() {
  88. },
  89. };
  90. </script>
  91. <style lang="scss">
  92. /deep/.avatar-uploader .el-upload,
  93. /deep/.upload-demo .el-upload {
  94. border-radius: 6px;
  95. cursor: pointer;
  96. position: relative;
  97. overflow: hidden;
  98. }
  99. .avatar-uploader .el-upload:hover {
  100. border-color: #409eff;
  101. }
  102. .avatar-uploader-icon {
  103. border: 1px dashed #d9d9d9;
  104. font-size: 28px;
  105. color: #8c939d;
  106. width: 120px;
  107. height: 120px;
  108. line-height: 120px;
  109. text-align: center;
  110. }
  111. .avatar {
  112. width: 120px;
  113. height: 120px;
  114. display: block;
  115. }
  116. .ivu-upload {
  117. display: none;
  118. }
  119. </style>