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
<template>
  <div class="activity-cover-content">
    <div class="activity-cover fl-cen" v-if="fileImg === ''">
      <el-upload
        action
        :http-request="uploadFileHandle"
        :before-upload="beforeAvatarUploadImg"
        :show-file-list="false"
      >
        <div class="img-check-btn fl-cen">上传图片</div>
      </el-upload>
      <div
        class="img-check-btn fl-cen"
        style="margin-left: 30px"
        @click="imgListCheck"
      >
        图库选择
      </div>
    </div>
    <div v-else class="img-shwo-box">
      <el-image
        class="img-shwo"
        :src="fileImg"
        :preview-src-list="[fileImg]"
        fit="cover"
      ></el-image>
      <div class="img-check-btn2 fl-cen" @click.stop="deleteImgHandle">
        删除图片
      </div>
    </div>
    <!-- 裁剪图片 -->
    <el-dialog
      title="图片裁剪"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :close-on-click-modal="false"
      append-to-body
    >
      <div class="cropper-content">
        <div class="cropper" style="text-align: center">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :autoCropWidth="width"
            :autoCropHeight="height"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :fixedBox="option.fixedBox"
            :fixed="option.fixed"
            :fixedNumber="[width, height]"
          ></vueCropper>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="finishCofirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
export default {
  props: {
    width: {
      type: Number,
      default: 800,
    },
    height: {
      type: Number,
      default: 800,
    },
    cover: {
      type: String,
      default: "",
    },
  },
  computed: {
    fileImg: {
      get: function () {
        return this.cover;
      },
      set: function () {
        return this.cover;
      },
    },
  },
  data() {
    return {
      dialogVisible: false,
      btnLoading: false,
      option: {
        img: "",
        outputSize: 1, // 剪切后的图片质量(0.1-1)
        full: true, // 输出原图比例截图 props名full
        outputType: "png",
        canMove: false,
        original: false,
        canMoveBox: true,
        autoCrop: true,
        fixedBox: false,
        fixed: true,
        maxImgSize: 3000, // 图片最大像素
      }, // 截图配置
    };
  },
  methods: {
    uploadFileHandle(f) {
      console.log(f);
    },
    beforeAvatarUploadImg(file) {
      const imgType = ["image/png", "image/jpeg", "video/mp4"];
      if (imgType.indexOf(file.type) === -1) {
        showToast("只能上传 png jpeg jpg 格式图片或者视频文件!", "error");
        return false;
      } else {
        return true;
      }
    },
    handleClose() {
      this.dialogVisible = false;
    },
    dataURLtoFile(dataurl, filename) {
      var arr = dataurl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename, { type: mime });
    },
    uploadFileHandle(f) {
      this.dialogVisible = true;
      this.option.img = URL.createObjectURL(f.file);
    },
    finishCofirm() {
      this.$refs.cropper.getCropData((obj) => {
        const timer = new Date().getTime();
        const urldata = this.dataURLtoFile(obj, timer + ".png");
        let formData = new FormData();
        formData.append("file", urldata);
        this.btnLoading = true;
        this.$api.post("communitypartybuilding/uploadimage", formData, (e) => {
          this.btnLoading = false;
          this.fileImg = e;
          this.$emit("updateBackImg", e);
          this.dialogVisible = false;
        });
      });
    },
    deleteImgHandle() {
      this.fileImg = "";
      this.$emit("updateBackImg", "");
    },
    imgListCheck() {
      this.$emit("listCheck");
    },
  },
};
</script>
 
<style scoped>
.activity-cover-content {
  position: relative;
}
.fl-al {
  display: flex;
  align-items: center;
}
.img-shwo {
  width: 100%;
  height: 100%;
}
.fl-cen {
  display: flex;
  align-items: center;
  justify-content: center;
}
.activity-cover {
  width: 260px;
  height: 154px;
  border: 1px dashed #999;
  background-color: rgb(235, 231, 231);
}
.img-check-btn {
  color: #0f90cc;
  cursor: pointer;
  margin-top: 80px;
  width: 78px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid #0f90cc;
}
.img-check-btn:hover {
  color: #fff !important;
  border: 1px solid red;
  background-color: red;
}
.img-check-btn2:hover {
  color: #fff !important;
  border: 1px solid rgb(233, 93, 13);
  background-color: rgb(233, 93, 13);
}
.cropper-content .cropper {
  width: auto;
  height: 500px !important;
}
.img-shwo-box {
  position: relative;
  width: 260px;
  height: 154px;
}
.img-check-btn2 {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-top: -15px;
  margin-left: -39px;
  color: #999;
  cursor: pointer;
  width: 78px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid #999;
}
</style>