chenlu
2 天以前 e5ea2c30e33f280ffc8a0ce78224ef99fb6679b1
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
<!-- ImageSizeEditor.vue -->
<template>
  <div class="img-grid">
    <div
        v-for="(img, index) in images"
        :key="getKey(img, index)"
        class="img-box"
        :style="getImgBoxStyle(getKey(img, index))"
        @click="openSizeDialog(img, index)"
    >
      <el-image
          :src="img.imageBase64"
          :fit="fit"
          style="width: 100%; height: 100%; display: block;"
      />
    </div>
  </div>
 
  <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="460px"
      append-to-body
  >
    <div class="form-row">
      <el-input-number
          v-model="form.w"
          :min="minWidth"
          :max="maxWidth"
          controls-position="right"
      />
      <span class="mul">×</span>
      <el-input-number
          v-model="form.h"
          :min="minHeight"
          :max="maxHeight"
          controls-position="right"
      />
    </div>
 
    <div class="ops-row">
<!--      <el-switch v-model="applyToAll" active-text="应用到全部图片" />-->
      <div v-if="showHint" class="hint">
        当前:{{ form.w }} × {{ form.h }} px
      </div>
    </div>
 
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
 
      <!-- ✅ 一键全图统一尺寸:不关弹窗,立即应用 -->
      <el-button @click="applyAllNow" :disabled="!canApply">
       尺寸相同
      </el-button>
 
      <!-- ✅ 确定:如果开关打开,则同样会应用全部,否则只应用当前 -->
      <el-button type="primary" @click="confirmSize" :disabled="!canApply">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { computed, reactive, ref, unref } from 'vue'
 
const props = defineProps({
  images: { type: Array, default: () => [] },
  keyField: { type: String, default: 'orderNumber' }, // 你数据 id=null,推荐 orderNumber
  srcField: { type: String, default: 'imageBase64' },
 
  sizeMap: { type: Object, default: () => ({}) },
 
  defaultWidth: { type: Number, default: 600 },
  defaultHeight: { type: Number, default: 400 },
 
  minWidth: { type: Number, default: 50 },
  maxWidth: { type: Number, default: 3000 },
  minHeight: { type: Number, default: 50 },
  maxHeight: { type: Number, default: 3000 },
 
  fit: { type: String, default: 'contain' },
  marginX: { type: String, default: '30px' },
  marginY: { type: String, default: '50px' },
  dialogTitle: { type: String, default: '设置图片尺寸' },
  showHint: { type: Boolean, default: true }
})
 
const emit = defineEmits(['update:sizeMap', 'change', 'changeAll'])
 
const sizeStore = computed({
  get: () => props.sizeMap || {},
  set: (val) => emit('update:sizeMap', val)
})
 
const dialogVisible = ref(false)
const activeKey = ref(null)
 
const form = reactive({
  w: props.defaultWidth,
  h: props.defaultHeight
})
 
const applyToAll = ref(false)
 
const canApply = computed(() => {
  const w = Number(form.w)
  const h = Number(form.h)
  return w > 0 && h > 0
})
 
const getKey = (img, index) => {
  const k = img?.[props.keyField]
  return (k !== undefined && k !== null && k !== '') ? String(k) : String(index)
}
 
const getSrc = (img) => img?.[props.srcField]
 
const getSize = (key) => {
  const s = unref(sizeStore.value?.[key])
  if (s && s.w && s.h) return { w: Number(s.w), h: Number(s.h) }
  return { w: props.defaultWidth, h: props.defaultHeight }
}
 
const getImgBoxStyle = (key) => {
  const { w, h } = getSize(key)
  return {
    width: `${w}px`,
    height: `${h}px`,
    marginLeft: props.marginX,
    marginRight: props.marginX,
    marginTop: props.marginY,
    marginBottom: props.marginY
  }
}
 
 
const openSizeDialog = (img, index) => {
  const key = getKey(img, index)
  activeKey.value = key
 
  const cur = getSize(key)
  form.w = cur.w
  form.h = cur.h
 
  applyToAll.value = false
  dialogVisible.value = true
}
 
// ✅ 应用到当前一张
const applyOne = (key, w, h) => {
  sizeStore.value = { ...sizeStore.value, [key]: { w, h } }
  emit('change', key, { w, h })
}
 
// ✅ 一键应用到全部
const applyAll = (w, h) => {
  const next = { ...sizeStore.value }
  props.images.forEach((img, idx) => {
    const k = getKey(img, idx)
    next[k] = { w, h }
  })
  sizeStore.value = next
  emit('changeAll', { w, h })
}
 
// ✅ footer 按钮:立即统一全图尺寸(不关弹窗)
const applyAllNow = () => {
  if (!canApply.value) return
  const w = Number(form.w)
  const h = Number(form.h)
  applyAll(w, h)
}
 
// ✅ 确定:如果开关打开 => 全部;否则只改当前
const confirmSize = () => {
  if (!canApply.value) return
  const w = Number(form.w)
  const h = Number(form.h)
 
  if (applyToAll.value) {
    applyAll(w, h)
    dialogVisible.value = false
    return
  }
 
  const key = activeKey.value
  if (key === null || key === undefined) return
  applyOne(key, w, h)
  dialogVisible.value = false
}
</script>
 
<style scoped>
.img-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
 
.form-row {
  display: flex;
  gap: 12px;
  align-items: center;
}
 
.mul {
  line-height: 32px;
}
 
.ops-row{
  margin-top: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
 
.hint {
  font-size: 12px;
  opacity: 0.75;
}
</style>