Files
crawler-plugin/admin-frontend-vue/src/pages/jd/JdProductImageManager.vue
T

301 lines
9.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/** 京东商品编辑抽屉「图片区」子组件(2026-11-16 从 JdProductEditDrawer 拆出:编辑抽屉
* 856 行超后管 800 报警线,图片区自洽成件——× 右上角标删+hover 替换+新增主图,
* B109-01 落地,2026-11-16 客户口径「删除做成打叉的 icon 放图右上角,也要支持编辑
* 替换本地的图」)。
*
* 交互语义:× 标删=前端标记、随父级「保存」一并提交(deleteImageIds 上抛);替换/新增
* =multipart 立即生效(服务端置锁+刷投影),上传成功用返回详情回填本区并上抛 saved。
* objectKey 由服务端按上传字节 sha256 内容寻址生成(铁律 32),前端不自造 key。
* 形状照 JdProductEditDrawer 原图片区(image-grid + image-cell,零 element-plus)。 */
import { computed, ref, watch } from 'vue'
import { uploadJdProductImage } from './jd-product-api.ts'
import type { JdProductImage } from './jd-product-model.ts'
import { requestErrorMessage } from '../../api/envelope.ts'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
const props = defineProps<{
productId: number
images: JdProductImage[]
/** 父级 loading 中禁上传入口。 */
disabled: boolean
}>()
const emit = defineEmits<{
(event: 'update:images', images: JdProductImage[]): void
(event: 'update:deleteIds', ids: number[]): void
(event: 'saved'): void
}>()
/** 标删的图片行 id(前端只标记,父级保存时一次提交 deleteImageIds)。 */
const deleteImageIds = ref<Set<number>>(new Set())
function toggleDeleteImage(image: JdProductImage): void {
const next = new Set(deleteImageIds.value)
if (next.has(image.id)) next.delete(image.id)
else next.add(image.id)
deleteImageIds.value = next
emit('update:deleteIds', [...next])
}
// ---------------- 图片上传/替换(立即生效,与标删的「保存时生效」不同) ----------------
/** 文件选择上下文:null=新增主图;有值=替换该登记行(档位继承原行,后端语义)。 */
const pendingReplaceImageId = ref<number | null>(null)
const fileInputRef = ref<HTMLInputElement | null>(null)
/** 上传中的图行 id(0=新增上传中);上传期间防重复点。 */
const uploadingImageId = ref<number | null>(null)
const uploadError = ref('')
function pickImage(replaceImageId: number | null): void {
if (uploadingImageId.value !== null || props.disabled) return
uploadError.value = ''
pendingReplaceImageId.value = replaceImageId
// 同选一张图也要重触发 change:先清值再弹窗。
if (fileInputRef.value) {
fileInputRef.value.value = ''
fileInputRef.value.click()
}
}
async function onFileChosen(event: Event): Promise<void> {
const input = event.target as HTMLInputElement
const file = input.files?.[0]
if (!file || props.disabled) return
uploadingImageId.value = pendingReplaceImageId.value ?? 0
try {
const detail = await uploadJdProductImage(props.productId, file, pendingReplaceImageId.value
? { replaceImageId: pendingReplaceImageId.value }
: { imageType: 'MAIN' })
// 立即生效:用返回详情回填图片区(标删集合里已不存在的行 id 由 images watch 清理)。
emit('update:images', detail.images)
showAdminFeedback(pendingReplaceImageId.value ? '图片已替换(该商品已转为人工维护)' : '图片已新增(该商品已转为人工维护)', 'success')
emit('saved')
} catch (error) {
uploadError.value = requestErrorMessage(error) || '图片上传失败,请稍后重试'
} finally {
uploadingImageId.value = null
pendingReplaceImageId.value = null
input.value = ''
}
}
/** 换商品打开时标删集合必须清零(父级 loadDetail 换 item.id)。 */
watch(() => props.productId, () => {
deleteImageIds.value = new Set()
emit('update:deleteIds', [])
})
/** 行集变化时清掉已不存在的标删 id(保存删行回填/上传回填两路共用;不清理的话
* 下次保存会把已删的 id 再提交一遍=后端按「图片不存在」整笔拒绝)。 */
watch(() => props.images, (images) => {
const live = new Set(images.map((image) => image.id))
const next = new Set([...deleteImageIds.value].filter((id) => live.has(id)))
if (next.size !== deleteImageIds.value.size) {
deleteImageIds.value = next
emit('update:deleteIds', [...next])
}
})
const markedCount = computed(() => deleteImageIds.value.size)
</script>
<template>
<section class="detail-block">
<div class="image-head">
<h4>图片(右上角 × 标记删除,保存时一并删除;替换 / 新增立即生效)</h4>
<button class="btn-add-image" type="button" :disabled="uploadingImageId !== null || disabled" @click="pickImage(null)">
{{ uploadingImageId === 0 ? '上传中...' : '+ 新增主图' }}
</button>
</div>
<input ref="fileInputRef" type="file" accept="image/jpeg,image/png,image/webp,image/gif" hidden @change="onFileChosen" />
<div v-if="images.length" class="image-grid">
<div
v-for="image in images"
:key="`${image.id}-${image.url}`"
class="image-cell"
:class="{ marked: deleteImageIds.has(image.id) }"
>
<img
:src="image.url"
alt=""
loading="lazy"
:class="{ dimmed: deleteImageIds.has(image.id) || uploadingImageId === image.id }"
/>
<!-- 右上角 × 删除标记(京麦样式,2026-11-16 客户口径):点 × 标记,再点撤销。 -->
<button
class="image-x"
type="button"
:title="deleteImageIds.has(image.id) ? '撤销删除标记' : '标记删除(保存时一并删除)'"
@click="toggleDeleteImage(image)"
>×</button>
<!-- 标删遮罩(整格可点撤销),未标删时 hover 底部出现「替换」条。 -->
<button
v-if="deleteImageIds.has(image.id)"
class="image-marked-mask"
type="button"
title="点击撤销删除标记"
@click="toggleDeleteImage(image)"
>已标记删除 · 点击撤销</button>
<button
v-else
class="image-replace"
type="button"
:disabled="uploadingImageId !== null"
@click="pickImage(image.id)"
>{{ uploadingImageId === image.id ? '上传中...' : '替换' }}</button>
</div>
</div>
<p v-else class="drawer-tip">暂无图片(可点右上「+ 新增主图」上传本地图)</p>
<p v-if="uploadError" class="drawer-failure">{{ uploadError }}</p>
<p v-if="markedCount" class="delete-count">已标记删除 {{ markedCount }} 张,保存后剩余 {{ images.length - markedCount }} 张</p>
</section>
</template>
<style scoped>
.image-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.image-head h4 {
margin: 0 0 10px;
font-size: 13.5px;
font-weight: 650;
color: #4e6479;
}
.btn-add-image {
min-height: 30px;
padding: 4px 12px;
border: 1px solid #95b1cb;
border-radius: 8px;
background: #ffffff;
color: #2f5d8b;
font-size: 12.5px;
cursor: pointer;
white-space: nowrap;
}
.btn-add-image:hover {
background: #edf5fb;
}
.btn-add-image:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.image-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
gap: 10px;
}
.image-cell {
position: relative;
}
.image-cell img {
display: block;
width: 100%;
aspect-ratio: 1;
object-fit: cover;
border: 1px solid #d8e3ee;
border-radius: 8px;
background: #f2f7fb;
}
.image-cell img.dimmed {
opacity: 0.35;
}
.image-cell.marked img {
outline: 2px solid #d08a7b;
}
/* 右上角 × 删除标记(京麦样式):hover 格子时出现;已标删常显。 */
.image-x {
position: absolute;
top: 4px;
right: 4px;
display: none;
align-items: center;
justify-content: center;
width: 22px;
height: 22px;
border: none;
border-radius: 50%;
background: rgba(38, 50, 63, 0.72);
color: #ffffff;
font-size: 15px;
line-height: 1;
cursor: pointer;
}
.image-cell:hover .image-x,
.image-cell.marked .image-x {
display: flex;
}
.image-x:hover {
background: #a04432;
}
/* 标删态:整格遮罩可点撤销。 */
.image-marked-mask {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
padding: 6px;
border: none;
border-radius: 8px;
background: rgba(160, 68, 50, 0.32);
color: #ffffff;
font-size: 12px;
line-height: 1.5;
cursor: pointer;
}
.image-marked-mask:hover {
background: rgba(160, 68, 50, 0.5);
}
/* hover 底部替换条(京麦替换入口);上传中常显。 */
.image-replace {
position: absolute;
left: 0;
right: 0;
bottom: 0;
display: none;
align-items: center;
justify-content: center;
min-height: 26px;
border: none;
border-radius: 0 0 8px 8px;
background: rgba(38, 50, 63, 0.72);
color: #ffffff;
font-size: 12px;
cursor: pointer;
}
.image-cell:hover .image-replace {
display: flex;
}
.image-cell .image-replace:disabled {
display: flex;
background: rgba(38, 50, 63, 0.55);
cursor: wait;
}
.image-replace:hover {
background: #2f5d8b;
}
.delete-count {
margin: 10px 0 0;
color: #a04432;
font-size: 12.5px;
}
.drawer-tip {
margin: 0;
color: #8293a5;
font-size: 13.5px;
}
.drawer-failure {
margin: 10px 0 0;
padding: 10px 14px;
border: 1px solid #f0c9c2;
border-radius: 10px;
background: #fdf1ef;
color: #a04432;
font-size: 13px;
}
</style>