index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <div class="component-upload-image">
  3. <el-upload
  4. :action="uploadImgUrl"
  5. list-type="picture-card"
  6. :on-success="handleUploadSuccess"
  7. :before-upload="handleBeforeUpload"
  8. :on-error="handleUploadError"
  9. name="file"
  10. :show-file-list="false"
  11. :headers="headers"
  12. style="display: inline-block; vertical-align: top"
  13. >
  14. <el-image v-if="!value" :src="value">
  15. <div slot="error" class="image-slot">
  16. <i class="el-icon-plus" />
  17. </div>
  18. </el-image>
  19. <div v-else class="image">
  20. <el-image :src="value" :style="`width:150px;height:150px;`" fit="fill"/>
  21. <div class="mask">
  22. <div class="actions">
  23. <span title="预览" @click.stop="dialogVisible = true">
  24. <i class="el-icon-zoom-in" />
  25. </span>
  26. <span title="移除" @click.stop="removeImage">
  27. <i class="el-icon-delete" />
  28. </span>
  29. </div>
  30. </div>
  31. </div>
  32. </el-upload>
  33. <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
  34. <img :src="value" style="display: block; max-width: 100%; margin: 0 auto;">
  35. </el-dialog>
  36. </div>
  37. </template>
  38. <script>
  39. import { getToken } from "@/utils/auth";
  40. export default {
  41. data() {
  42. return {
  43. dialogVisible: false,
  44. uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  45. headers: {
  46. Authorization: "Bearer " + getToken(),
  47. },
  48. };
  49. },
  50. props: {
  51. value: {
  52. type: String,
  53. default: "",
  54. },
  55. },
  56. methods: {
  57. removeImage() {
  58. this.$emit("input", "");
  59. },
  60. handleUploadSuccess(res) {
  61. this.$emit("input", res.url);
  62. this.loading.close();
  63. },
  64. handleBeforeUpload() {
  65. this.loading = this.$loading({
  66. lock: true,
  67. text: "上传中",
  68. background: "rgba(0, 0, 0, 0.7)",
  69. });
  70. },
  71. handleUploadError() {
  72. this.$message({
  73. type: "error",
  74. message: "上传失败",
  75. });
  76. this.loading.close();
  77. },
  78. },
  79. watch: {},
  80. };
  81. </script>
  82. <style scoped lang="scss">
  83. .image {
  84. position: relative;
  85. .mask {
  86. opacity: 0;
  87. position: absolute;
  88. top: 0;
  89. width: 100%;
  90. background-color: rgba(0, 0, 0, 0.5);
  91. transition: all 0.3s;
  92. }
  93. &:hover .mask {
  94. opacity: 1;
  95. }
  96. }
  97. </style>