index.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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" />
  21. <div class="mask">
  22. <div class="actions">
  23. <span title="移除" @click.stop="removeImage">
  24. <i class="el-icon-delete" />
  25. </span>
  26. </div>
  27. </div>
  28. </div>
  29. </el-upload>
  30. </div>
  31. </template>
  32. <script>
  33. import { getToken } from "@/utils/auth";
  34. export default {
  35. components: {},
  36. data() {
  37. return {
  38. uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  39. headers: {
  40. Authorization: "Bearer " + getToken(),
  41. },
  42. };
  43. },
  44. props: {
  45. value: {
  46. type: String,
  47. default: "",
  48. },
  49. },
  50. methods: {
  51. removeImage() {
  52. this.$emit("input", "");
  53. },
  54. handleUploadSuccess(res) {
  55. this.$emit("input", res.url);
  56. this.loading.close();
  57. },
  58. handleBeforeUpload() {
  59. this.loading = this.$loading({
  60. lock: true,
  61. text: "上传中",
  62. background: "rgba(0, 0, 0, 0.7)",
  63. });
  64. },
  65. handleUploadError() {
  66. this.$message({
  67. type: "error",
  68. message: "上传失败",
  69. });
  70. this.loading.close();
  71. },
  72. },
  73. watch: {},
  74. };
  75. </script>
  76. <style scoped lang="scss">
  77. .avatar {
  78. width: 100%;
  79. height: 100%;
  80. }
  81. .image {
  82. position: relative;
  83. .mask {
  84. opacity: 0;
  85. position: absolute;
  86. top: 0;
  87. width: 100%;
  88. background-color: rgba(0, 0, 0, 0.5);
  89. transition: all 0.3s;
  90. }
  91. &:hover .mask {
  92. opacity: 1;
  93. }
  94. }
  95. </style>