333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<template>
  <div>
    <el-upload :http-request="uploadHttp" :before-upload="(file)=>beforeAvatarUpload(file)" :multiple="options.multiple" :on-preview="(file)=>handlePicPreview(file)" :on-remove="handlePicRemove" :disabled="options.disabled" :file-list="picList" :list-type="options.listType" :class="{'hide-button': options.readonly}" action="">
      <i v-if="options.listType=='picture-card'" class="el-icon-plus"></i>
      <el-button v-else :type="options.buttonType" size="small">点击上传</el-button>
    </el-upload>
 
    <!--预览图片-->
    <el-dialog v-dialogDrag :visible.sync="dialogPicVisible" :append-to-body="true">
      <img :src="dialogImageUrl" width="100%" alt="">
    </el-dialog>
  </div>
</template>
 
<script>
import ossClient from "@/utils/aliyun.oss.client";
 
export default {
  props: {
    // 下拉框绑定值
    value: {
      type: Array,
      default: () => {
        return [];
      }
    },
    // 参数配置
    options: {
      type: Object,
      default: () => {
        return {
          multiple: true,
          disabled: false,
          listType: "text", // text/picture/picture-card
          buttonType: "text"
        };
      }
    }
  },
  data() {
    return {
      dialogImageUrl: null,
      dialogPicVisible: false,
      BASE_API: process.env.BASE_API
    };
  },
  computed: {
    // 图片列表
    picList: {
      get: function() {
        return this.value;
      },
      set: function(val) {
        this.$emit("input", val);
      }
    }
  },
  methods: {
    // 图片删除
    handlePicRemove(file, fileList) {
      this.picList = fileList;
      this.$emit("on-upload-deleted", file, fileList);
    },
    // 预览图片
    handlePicPreview(file) {
      if (this.options.listType === "text") {
        window.open(file.url);
      } else {
        this.dialogImageUrl = file.url;
        this.dialogPicVisible = true;
      }
    },
    // 阿里云图片上传
    uploadHttp({ file }) {
      // 定义唯一的文件名
      const contentType = file.type;
      const name = file.name;
      const fileName = `juhe/file/${this.common.formatDate(new Date(), "YYYY-MM-DD")}/${this.common.getGUID()}-${name}`;
      ossClient(this.common.uploadConf)
        .put(fileName, file, {
          ContentType: contentType
        })
        .then(({ res, url, name }) => {
          if (res && res.status === 200) {
            const url = res.requestUrls[0];
            const list = this.picList;
            const f = url.split("/");
            const name = decodeURI(f[f.length - 1]).substr(37);
            list.push({
              name: name,
              url: url
            });
            this.picList = list;
            this.$emit("on-upload-finished", this.picList);
          }
        })
        .catch(err => {
          this.$message.error(`上传图片失败` + err.message);
        });
    },
    /**
     * 图片限制
     */
    beforeAvatarUpload(file) {
      if (!this.options.multiple) {
        if (this.picList.length >= 1) {
          this.$message.error("已存在文件,请先删除然后重新上传");
          return false;
        }
      }
      if (this.options.listType === "text") {
        const names = file.name.split(".");
        const extName = names[names.length - 1];
        const isLt5MB = file.size / 1024 / 1024 < 5;
        const type = ["xls", "xlsx", "doc", "docx", "pdf", "jpeg", "jpg", "png"];
        if (type.indexOf(extName) < 0) {
          this.$message.error(`只能上传${type.join("/")}格式文档!`);
        }
        if (!isLt5MB) {
          this.$message.error("大小不能超过 5MB!");
        }
        return type.indexOf(extName) >= 0 && isLt5MB;
      } else {
        const names = file.name.split(".");
        const extName = names[names.length - 1];
        const isLt5MB = file.size / 1024 / 1024 < 5;
        const type = ["jpeg", "jpg", "png"];
        if (type.indexOf(extName) < 0) {
          this.$message.error("上传图片只能是 JPEG/JPG/PNG 格式!");
        }
        if (!isLt5MB) {
          this.$message.error("单张图片大小不能超过 5MB!");
        }
        return type.indexOf(extName) >= 0 && isLt5MB;
      }
    }
  }
};
</script>