hejianhao
2025-04-16 dab2d210ca06c1faa514c6388fbd5de1ab355360
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<template>
  <!--  <el-upload-->
  <!--    class="avatar-uploader"-->
  <!--    :action="api"-->
  <!--    :show-file-list="false"-->
  <!--    :on-success="handleAvatarSuccess"-->
  <!--    :before-upload="beforeAvatarUpload"-->
  <!--    :on-error="onError"-->
  <!--    :on-progress="onProgress"-->
  <!--    :headers="headers"-->
  <!--    :ref="ids"-->
  <!--  >-->
  <!--    <div-->
  <!--      v-if="!slots"-->
  <!--      class="db"-->
  <!--    >-->
  <!--      <img-->
  <!--        :src="imageUrl"-->
  <!--        alt=""-->
  <!--        v-if="imageUrl"-->
  <!--      >-->
  <!--      <i-->
  <!--        v-else-->
  <!--        class="el-icon-plus"-->
  <!--      ></i><b v-if="!imageUrl">上传</b>-->
  <!--    </div>-->
  <!--    <div-->
  <!--      v-else-->
  <!--      class="db"-->
  <!--    >-->
  <!--      <slot></slot>-->
  <!--    </div>-->
  <!--  </el-upload>-->
  <ImgCutter
    :isModal="true"
    toolBgc="#bbb"
    :tool="false"
    :boxWidth="boxWidth"
    :boxHeight="boxHeight"
    :cutWidth="width"
    :cutHeight="height"
    :sizeChange="false"
    v-on:cutDown="cutDown"
    class="as"
  >
    <div
      slot="open"
      v-if="!slots"
      class="db"
    >
      <img
        class="contImg"
        :src="imageUrl"
        alt=""
        v-if="imageUrl"
      >
      <i
        v-else
        class="el-icon-plus"
      ></i><b v-if="!imageUrl">上传</b>
    </div>
  </ImgCutter>
</template>
 
<script>
// vue-img-cutter 裁剪工具 https://gitee.com/GLUESTICK/vue-img-cutter
import ImgCutter from "vue-img-cutter";
 
export default {
  props: {
    boxWidth: { type: Number, default: 400 }, //裁剪工具宽高
    boxHeight: { type: Number, default: 200 },
    width: { type: Number, default: 200 }, //裁剪图片宽高
    height: { type: Number, default: 200 },
    slots: { type: Boolean, default: false },
    pic: { type: String, default: "" },
    ids: { type: String, default: "upload" },
    submit: { type: Number, default: 0 },
    url: { type: String, default: "" },
    exp: {
      type: Array,
      default: () => {
        return ["image"];
      },
    },
  },
  components: { ImgCutter },
  data() {
    return {
      imageUrl: "",
      api: "",
      headers: { Authorization: "" },
      upload: null,
      load: null,
    };
  },
  watch: {
    pic(n) {
      this.setPic(n);
    },
    submit(n) {
      if (n) {
        this.upload && this.upload.submit();
      }
    },
    url(n) {
      this.api = n;
    },
  },
  methods: {
    cutDown(e) {
      //新的下载组件回调
      console.log(e);
      // return
      var formData = new FormData();
      formData.append("file", e.file);
      this.$api.post("communitypartybuilding/uploadimage", formData, (e) => {
        // console.log(e)
        this.imageUrl = e || "";
        this.$emit("path", e);
      });
    },
    handleAvatarSuccess(v) {
      this.openLoad(true);
      this.imageUrl = v.data || "";
      if (v.code === 200) {
        this.$emit("path", v.data);
      } else {
        this.$emit("error", v);
      }
    },
    beforeAvatarUpload(v) {
      let type = this.exp.indexOf(v.type.split("/")[0]) >= 0;
      this.$emit("on-before", v);
      let size = v.size / 1024 / 1024 < 20;
      if (!type) {
        demo.toast("上传格式不正确");
      }
      if (!size) {
        demo.toast("上传大小不能超过 20MB!");
      }
      this.$api.load();
      return type && size;
    },
    setPic(n) {
      if (!this.slots) {
        this.imageUrl = n;
      }
    },
    onProgress(v) {
      this.openLoad();
      this.$emit("on-progress", v.percent);
    },
    onError(err) {
      demo.alert(err);
      this.openLoad(true);
    },
    openLoad(f) {
      if (f) {
        if (this.load) {
          demo.remove(this.load);
          this.load = null;
        }
        return 0;
      }
      if (this.load !== null) return 0;
      this.load = demo.loading();
    },
  },
  mounted() {
    this.api = this.$js.api.upload;
    if (this.url) {
      this.api = this.url;
    }
    this.headers.Authorization = "Bearer " + demo.$session.get("token") || "";
    this.upload = this.$refs[this.ids];
    this.setPic(this.pic);
  },
};
</script>
<style lang="less">
.el-upload {
  height: 100%;
  width: 100%;
}
</style>
<style lang='less' scope>
.contImg {
  height: 100%;
}
.as {
  width: 104px;
  height: 104px;
}
.db {
  color: #888;
  text-align: center;
  height: 104px;
  background-color: #f7f7f7;
  cursor: pointer;
  overflow: hidden;
  i {
    margin: 0 auto !important;
    font-size: 26px;
    line-height: 70px;
    height: 70px;
    display: block;
  }
  b {
    line-height: 1;
    display: block;
  }
}
.avatar-uploader .el-upload {
  /*border: 1px dashed #d9d9d9;*/
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
 
  img {
    border-radius: 5px;
  }
}
 
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
 
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
 
.avatar-uploader-icon,
.avatar-uploader,
img {
  width: 100%;
  height: 100%;
  display: block;
}
</style>