hcong
2024-11-20 d7932d42a9c4a08376d542dbff8301e7d985b787
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<template>
  <CompGenericWrapper type="dialog">
    <template #content>
      <el-row>
        <el-col>
          <el-form-item label="整改图片">
            <el-button @click="choseChangePic" :disabled="fileList.length >= 3"
              >从文件夹选取</el-button
            >
          </el-form-item>
        </el-col>
        <el-col v-if="!fileList || fileList.length <= 0">
          <el-empty style="height: 145px" description="请添加图片" />
        </el-col>
        <el-col>
          <el-upload
            class="img-upload"
            ref="uploadRef"
            v-model:file-list="fileList"
            list-type="picture-card"
            multiple
            :auto-upload="false"
            crossorigin="Anonymous"
            :before-remove="beforeRemoveFile"
            :on-preview="handlePictureCardPreview"
            :disabled="readonly"
            accept="image/*"
          >
            <template #trigger v-if="fileList.length < 3 && !readonly">
              <el-button
                v-if="fileList.length < 3"
                type="primary"
                id="uploadBtnId"
                style="display: none"
              ></el-button>
              <el-icon>
                <Plus />
              </el-icon>
            </template>
            <template #tip>
              <div style="color: #f56c6c">
                最少上传一张图片,最多选择三张图片
              </div>
            </template>
          </el-upload>
        </el-col>
      </el-row>
      <div class="flex-div">
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </div>
      <el-image-viewer
          v-if="previewDialogVisible"
          :url-list="fileList.map((item) => item.url)"
          :initial-index="initialIndex"
          @close="previewDialogVisible = false"
          alt="预览"
          class="preview-pic"
        />
    </template>
  </CompGenericWrapper>
</template>
<script>
import problemApi from '@/api/fysp/problemApi.js';
import CompGenericWrapper from './CompGenericWrapper.vue';
import { $fysp } from '@/api/index.js';
import fileUtil from '@/utils/fileUtils.js';
import { useCloned } from '@vueuse/core';
import { ElMessage } from 'element-plus';
export default {
  emits: ['submit', 'cancel'],
  components: {
    CompGenericWrapper
  },
  watch: {
    oldChangeFileList: {
      handler(nv, ov) {
        this.initParams();
      },
      immediate: true
    },
    fileList: {
      handler(newFileList, oldFileList) {
        this.pictureValidate();
      },
      deep: true
    }
  },
  props: {
    changeType: {
      type: Number
    },
    problemId: {
      type: String
    },
    oldChangeFileList: {
      type: Array,
      default: () => []
    },
    subtask: {
      type: Object,
      default: () => {}
    },
    month: {
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 初始图片预览index
      initialIndex: -1,
      // 图片选择最大数量
      maxSelectImgCount: 3,
      previewDialogImageUrl: '',
      previewDialogVisible: false,
      fileList: [],
      oldFileList: [],
      deleteImg: [],
 
      ledgerPicDialog: false,
      anyPhotoDialog: false
    };
  },
  mounted() {},
  methods: {
    pictureValidate() {
      if (this.changeType == 1 && this.fileList.length < 1) {
        ElMessage({
          message: '至少上传一张图片',
          type: 'error'
        });
        return false;
      } else if (this.fileList.length > 3) {
        ElMessage({
          message: '超过三张, 已删除多出的图片',
          type: 'error'
        });
        this.fileList = this.fileList.slice(0, 3);
        return false;
      }
      return true;
    },
    initParams() {
      if (!this.changeType || this.changeType == 0) {
        return;
      }
      let beforeEditImgList = [];
      useCloned(this.oldChangeFileList).cloned.value.forEach(
        (oldChangeFileitem) => {
          if (oldChangeFileitem.ischanged == 1) {
            oldChangeFileitem.url =
              $fysp.imgUrl +
              oldChangeFileitem.extension1 +
              oldChangeFileitem.guid +
              '.jpg';
            oldChangeFileitem.name = '1';
            beforeEditImgList.push(oldChangeFileitem);
          }
        }
      );
      this.fileList = useCloned(beforeEditImgList).cloned.value;
      this.oldFileList = useCloned(beforeEditImgList).cloned.value;
    },
    onCancel() {
      this.$emit("cancel")
    },
    onSubmit() {
      if (!this.pictureValidate()) {
        return;
      }
      // 数据准备
      let data = new FormData();
      var picUrls = [];
      this.fileList.forEach((item) => {
        if (!('guid' in item)) {
          // 新的
          let exclude = false;
          for (let index = 0; index < this.oldFileList.length; index++) {
            const element = this.oldFileList[index];
            if (item.url == element.url) {
              exclude = true;
              break;
            }
          }
          if (!exclude) {
            picUrls.push(item.url);
          }
          exclude = false;
          // picUrls.push(item)
        }
      });
 
      const that = this;
      let deleteImgCopy = this.deleteImg;
 
      if (this.changeType == 1) {
        fileUtil.getImageFiles(picUrls, function (files) {
          data.append('deleteImg', deleteImgCopy);
          data.append('problemId', that.problemId);
          files.forEach((image) => {
            data.append('images', image);
          });
 
          problemApi.updateChange(data).then((res) => {});
        });
        that.$emit('submit', true);
      } else {
        fileUtil.getImageFiles(picUrls, function (files) {
          data.append('problemId', that.problemId);
          files.forEach((image) => {
            data.append('images', image);
          });
          problemApi.changeProblem(data).then((res) => {});
          that.$emit('submit', true);
        });
      }
    },
    beforeRemoveFile(file, fileList) {
      if (file.remark == '已上传') {
        this.deleteImg.push(file.guid);
        this.oldFileList.filter((item) => item.url != file.url);
      }
    },
    handlePictureCardPreview(uploadFile) {
      this.initialIndex = this.fileList.indexOf(uploadFile)
      this.previewDialogVisible = true;
      this.previewDialogImageUrl = uploadFile.url;
    },
    // 从文件夹中
    choseChangePic() {
      // 获取指定ID的元素
      var btnElement = document.getElementById('uploadBtnId');
      // 检查元素是否存在
      if (btnElement) {
        // 触发点击事件
        btnElement.click();
      }
    }
  }
};
</script>
<style scoped>
.flex-div {
  display: flex;
}
.t-card_item {
  display: flex;
  padding: 5px;
}
.img-upload {
  margin-top: 30px;
  margin-bottom: 30px;
  margin-left: 63px;
}
::v-deep .el-dialog__body {
  width: 95%;
}
::v-deep .el-upload-list--picture-card .el-upload-list__item-thumbnail {
  object-fit: cover !important;
}
.preview-pic {
  object-fit: cover;
  width: 100%;
  height: 100%;
}
::v-deep .el-upload--picture-card {
  display: none;
}
/* 隐藏el-upload上传成功组件 */
::v-deep .el-upload-list__item-status-label {
  display: none !important;
}
</style>