Browse Source

新增图片上传组件

RuoYi 5 years ago
parent
commit
93920707b5
1 changed files with 68 additions and 0 deletions
  1. 68 0
      ruoyi-ui/src/components/UploadImage/index.vue

+ 68 - 0
ruoyi-ui/src/components/UploadImage/index.vue

@@ -0,0 +1,68 @@
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
+      <img v-if="value" :src="value" class="avatar" />
15
+      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
16
+    </el-upload>
17
+  </div>
18
+</template>
19
+
20
+<script>
21
+import { getToken } from "@/utils/auth";
22
+
23
+export default {
24
+  components: {},
25
+  data() {
26
+    return {
27
+      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
28
+      headers: {
29
+        Authorization: "Bearer " + getToken(),
30
+      },
31
+    };
32
+  },
33
+  props: {
34
+    value: {
35
+      type: String,
36
+      default: "",
37
+    },
38
+  },
39
+  methods: {
40
+    handleUploadSuccess(res) {
41
+      this.$emit("input", res.url);
42
+      this.loading.close();
43
+    },
44
+    handleBeforeUpload() {
45
+      this.loading = this.$loading({
46
+        lock: true,
47
+        text: "上传中",
48
+        background: "rgba(0, 0, 0, 0.7)",
49
+      });
50
+    },
51
+    handleUploadError() {
52
+      this.$message({
53
+        type: "error",
54
+        message: "上传失败",
55
+      });
56
+      this.loading.close();
57
+    },
58
+  },
59
+  watch: {},
60
+};
61
+</script>
62
+
63
+<style scoped lang="scss">
64
+.avatar {
65
+  width: 100%;
66
+  height: 100%;
67
+}
68
+</style>