mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: sku增加图片显示
This commit is contained in:
+3
@@ -57,5 +57,8 @@ public class CreateProductDTO {
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "SKU 图文件ID(系统上传)")
|
||||
private Long imageId;
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -37,4 +37,7 @@ public class CreateProductSkuDTO {
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "SKU 图文件ID(系统上传)")
|
||||
private Long imageId;
|
||||
}
|
||||
|
||||
+3
@@ -63,5 +63,8 @@ public class UpdateProductDTO {
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "SKU 图文件ID(系统上传)")
|
||||
private Long imageId;
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -37,4 +37,7 @@ public class UpdateProductSkuDTO {
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "SKU 图文件ID(系统上传)")
|
||||
private Long imageId;
|
||||
}
|
||||
|
||||
+4
@@ -116,6 +116,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
createDto.setShippingCost(specDTO.getShippingCost());
|
||||
createDto.setOtherCost(specDTO.getOtherCost());
|
||||
createDto.setRemark(specDTO.getRemark());
|
||||
createDto.setImageId(specDTO.getImageId());
|
||||
ProductSkuVO createdVo = productSkuService.createProductSku(createDto);
|
||||
appendSpecIdIfSkuCodeHasLinkedOrders(companyId, createdVo.getId(), specDTO.getSkuCode(),
|
||||
specDTO.getProductCost(), specDTO.getShippingCost(), specDTO.getOtherCost(), syncSkuIds);
|
||||
@@ -173,6 +174,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
createDto.setShippingCost(specDTO.getShippingCost());
|
||||
createDto.setOtherCost(specDTO.getOtherCost());
|
||||
createDto.setRemark(specDTO.getRemark());
|
||||
createDto.setImageId(specDTO.getImageId());
|
||||
productSkuService.createProductSkuForImport(createDto, companyId, userId);
|
||||
}
|
||||
}
|
||||
@@ -367,6 +369,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
updateSpecDto.setShippingCost(specDTO.getShippingCost());
|
||||
updateSpecDto.setOtherCost(specDTO.getOtherCost());
|
||||
updateSpecDto.setRemark(specDTO.getRemark());
|
||||
updateSpecDto.setImageId(specDTO.getImageId());
|
||||
ProductSkuVO specVO = productSkuService.updateProductSku(updateSpecDto, specDTO.getId());
|
||||
if (Boolean.TRUE.equals(specVO.getCostChanged())) {
|
||||
rawCostChangedSkuIds.add(specDTO.getId());
|
||||
@@ -396,6 +399,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
createDto.setShippingCost(specDTO.getShippingCost());
|
||||
createDto.setOtherCost(specDTO.getOtherCost());
|
||||
createDto.setRemark(specDTO.getRemark());
|
||||
createDto.setImageId(specDTO.getImageId());
|
||||
ProductSkuVO createdVo = productSkuService.createProductSku(createDto);
|
||||
if (Boolean.TRUE.equals(createdVo.getCostChanged()) && createdVo.getId() != null) {
|
||||
rawCostChangedSkuIds.add(createdVo.getId());
|
||||
|
||||
+3
@@ -290,6 +290,9 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
byCode.setName(dto.getName());
|
||||
byCode.setSkuCode(trimmedCode);
|
||||
byCode.setRemark(dto.getRemark());
|
||||
if (dto.getImageId() != null) {
|
||||
byCode.setImageId(dto.getImageId());
|
||||
}
|
||||
byCode.setUpdateBy(createByUserId);
|
||||
this.updateById(byCode);
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ export namespace ProductSkuDTO {
|
||||
otherCost?: number;
|
||||
/** 备注 */
|
||||
remark?: string;
|
||||
/** SKU 图文件 ID(系统上传) */
|
||||
imageId?: number;
|
||||
}
|
||||
|
||||
/** 更新商品 SKU 参数 */
|
||||
@@ -37,6 +39,8 @@ export namespace ProductSkuDTO {
|
||||
otherCost?: number;
|
||||
/** 备注 */
|
||||
remark?: string;
|
||||
/** SKU 图文件 ID(系统上传) */
|
||||
imageId?: number;
|
||||
}
|
||||
|
||||
/** 商品 SKU 过滤条件 */
|
||||
|
||||
@@ -25,6 +25,8 @@ export namespace ProductSkuVO {
|
||||
otherCost?: number;
|
||||
/** 备注 */
|
||||
remark?: string;
|
||||
/** SKU 图 URL(系统文件或渠道外链) */
|
||||
imageUrl?: string;
|
||||
/** 创建时间 */
|
||||
createTime: string;
|
||||
/** 更新时间 */
|
||||
|
||||
@@ -47,6 +47,8 @@ export namespace ProductDTO {
|
||||
otherCost?: number;
|
||||
/** 备注 */
|
||||
remark?: string;
|
||||
/** SKU 图文件 ID(系统上传) */
|
||||
imageId?: number;
|
||||
}
|
||||
|
||||
/** 创建商品 SKU 参数(独立接口用) */
|
||||
@@ -65,6 +67,8 @@ export namespace ProductDTO {
|
||||
otherCost?: number;
|
||||
/** 备注 */
|
||||
remark?: string;
|
||||
/** SKU 图文件 ID(系统上传) */
|
||||
imageId?: number;
|
||||
}
|
||||
|
||||
/** 更新商品 SKU 参数 */
|
||||
@@ -83,6 +87,8 @@ export namespace ProductDTO {
|
||||
otherCost?: number;
|
||||
/** 备注 */
|
||||
remark?: string;
|
||||
/** SKU 图文件 ID(系统上传) */
|
||||
imageId?: number;
|
||||
}
|
||||
|
||||
/** 商品过滤条件 */
|
||||
|
||||
@@ -70,6 +70,8 @@ export namespace ProductVO {
|
||||
otherCost?: number;
|
||||
/** 备注 */
|
||||
remark?: string;
|
||||
/** SKU 图 URL(系统文件或渠道外链) */
|
||||
imageUrl?: string;
|
||||
/** 创建时间 */
|
||||
createTime: string;
|
||||
/** 更新时间 */
|
||||
|
||||
+23
-2
@@ -1,7 +1,7 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ProductVO } from '#/api/ecommerce/product/types';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
import { computed, h, ref } from 'vue';
|
||||
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
@@ -29,6 +29,27 @@ const formatCost = (val?: number) => `¥${(val ?? 0).toFixed(2)}`;
|
||||
const skuDataSource = computed(() => product.value?.skus ?? []);
|
||||
|
||||
const skuColumns = [
|
||||
{
|
||||
title: '图片',
|
||||
dataIndex: 'imageUrl',
|
||||
key: 'imageUrl',
|
||||
width: 72,
|
||||
align: 'center' as const,
|
||||
customRender: ({ record }: { record: ProductVO.SkuInfo }) => {
|
||||
if (record.imageUrl) {
|
||||
return h(Image, {
|
||||
src: record.imageUrl,
|
||||
width: 40,
|
||||
height: 40,
|
||||
class: 'object-cover',
|
||||
preview: true,
|
||||
});
|
||||
}
|
||||
return h(ImageIcon, {
|
||||
class: 'mx-auto h-5 w-5 text-slate-300 dark:text-[hsl(240deg_5%_45%)]',
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'SKU 编码',
|
||||
dataIndex: 'skuCode',
|
||||
@@ -174,7 +195,7 @@ defineExpose({
|
||||
row-key="id"
|
||||
size="small"
|
||||
bordered
|
||||
:scroll="{ x: 760 }"
|
||||
:scroll="{ x: 820 }"
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
+191
-4
@@ -36,7 +36,7 @@ const emit = defineEmits<Emits>();
|
||||
// 创建Vben Modal实例
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
title: '添加商品',
|
||||
class: 'w-[1000px]',
|
||||
class: 'w-[1100px]',
|
||||
draggable: true, // 开启拖拽
|
||||
contentClass: 'px-5 py-4',
|
||||
onConfirm: handleSubmit,
|
||||
@@ -152,6 +152,12 @@ const categoryTreeDropdownRender: DropdownRender = (opt) => {
|
||||
type SkuFormRow = ProductDTO.Sku & {
|
||||
_clientKey?: string;
|
||||
id?: number;
|
||||
/** 已有图预览 URL(系统文件或渠道外链) */
|
||||
imageUrl?: string;
|
||||
/** 新选择待上传的文件 */
|
||||
newImageFile?: File | null;
|
||||
/** 本地预览 object URL,需 revoke */
|
||||
_previewUrl?: string;
|
||||
};
|
||||
|
||||
// 规格数据 - 支持创建和更新
|
||||
@@ -212,6 +218,7 @@ function initFormData() {
|
||||
shippingCost: spec.shippingCost ?? 0,
|
||||
otherCost: spec.otherCost ?? 0,
|
||||
remark: spec.remark || '',
|
||||
imageUrl: spec.imageUrl || '',
|
||||
});
|
||||
skus.value = isEdit.value
|
||||
? currentProductData.value.skus.map((spec) => {
|
||||
@@ -246,6 +253,9 @@ function resetForm() {
|
||||
newImageFile.value = null;
|
||||
originalImageUrl.value = '';
|
||||
imageLoadError.value = false;
|
||||
for (const row of skus.value) {
|
||||
revokeSkuPreview(row);
|
||||
}
|
||||
skus.value = [];
|
||||
showSkus.value = false;
|
||||
initialEditSkuIds.value = new Set();
|
||||
@@ -272,6 +282,16 @@ async function handleSubmit() {
|
||||
delete submitData.mainImageId;
|
||||
}
|
||||
|
||||
// SKU 新选图片先上传,写入 imageId
|
||||
for (const row of skus.value) {
|
||||
if (row.newImageFile) {
|
||||
const imageId = await uploadSkuImageAndGetId(row.newImageFile);
|
||||
if (imageId) {
|
||||
row.imageId = imageId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 调用API
|
||||
if (
|
||||
isEdit.value &&
|
||||
@@ -291,6 +311,9 @@ async function handleSubmit() {
|
||||
const skusPayload: ProductDTO.UpdateSku[] = skus.value.map((row) => {
|
||||
const {
|
||||
_clientKey: _c,
|
||||
_previewUrl: _p,
|
||||
newImageFile: _f,
|
||||
imageUrl: _u,
|
||||
id,
|
||||
name,
|
||||
skuCode,
|
||||
@@ -298,8 +321,9 @@ async function handleSubmit() {
|
||||
shippingCost,
|
||||
otherCost,
|
||||
remark,
|
||||
imageId,
|
||||
} = row;
|
||||
const base = {
|
||||
const base: ProductDTO.UpdateSku = {
|
||||
name,
|
||||
skuCode: skuCode ?? '',
|
||||
productCost: productCost ?? 0,
|
||||
@@ -307,6 +331,9 @@ async function handleSubmit() {
|
||||
otherCost: otherCost ?? 0,
|
||||
remark: remark ?? '',
|
||||
};
|
||||
if (imageId != null) {
|
||||
base.imageId = imageId;
|
||||
}
|
||||
if (id != null && id !== undefined) {
|
||||
return { ...base, id: Number(id) };
|
||||
}
|
||||
@@ -336,7 +363,16 @@ async function handleSubmit() {
|
||||
categoryId: submitData.categoryId,
|
||||
mainImageId: submitData.mainImageId,
|
||||
...(skus.value.length > 0 && {
|
||||
skus: skus.value.map(({ _clientKey: _c, id: _id, ...rest }) => rest),
|
||||
skus: skus.value.map(
|
||||
({
|
||||
_clientKey: _c,
|
||||
_previewUrl: _p,
|
||||
newImageFile: _f,
|
||||
imageUrl: _u,
|
||||
id: _id,
|
||||
...rest
|
||||
}) => rest,
|
||||
),
|
||||
}),
|
||||
};
|
||||
const createResult = await createProductApi(createData);
|
||||
@@ -444,6 +480,8 @@ const defaultSku = (): SkuFormRow => ({
|
||||
shippingCost: 0,
|
||||
otherCost: 0,
|
||||
remark: '',
|
||||
imageUrl: '',
|
||||
newImageFile: null,
|
||||
});
|
||||
|
||||
// 添加 SKU
|
||||
@@ -453,16 +491,148 @@ function addSku() {
|
||||
|
||||
// 删除规格
|
||||
function removeSku(index: number) {
|
||||
const row = skus.value[index];
|
||||
if (row) {
|
||||
revokeSkuPreview(row);
|
||||
}
|
||||
skus.value.splice(index, 1);
|
||||
}
|
||||
|
||||
function revokeSkuPreview(row: SkuFormRow) {
|
||||
if (row._previewUrl) {
|
||||
URL.revokeObjectURL(row._previewUrl);
|
||||
row._previewUrl = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function getSkuPreviewUrl(record: SkuFormRow): string {
|
||||
return record._previewUrl || record.imageUrl || '';
|
||||
}
|
||||
|
||||
function selectSkuImageFile(record: SkuFormRow) {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = 'image/*';
|
||||
input.style.display = 'none';
|
||||
document.body.append(input);
|
||||
|
||||
input.addEventListener('change', (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0];
|
||||
if (file) {
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
input.remove();
|
||||
return;
|
||||
}
|
||||
const isLt2M = file.size / 1024 / 1024 < 2;
|
||||
if (!isLt2M) {
|
||||
message.error('图片大小不能超过 2MB');
|
||||
input.remove();
|
||||
return;
|
||||
}
|
||||
revokeSkuPreview(record);
|
||||
record.newImageFile = file;
|
||||
record._previewUrl = URL.createObjectURL(file);
|
||||
// 触发表格重绘
|
||||
skus.value = [...skus.value];
|
||||
message.success('SKU 图片选择成功');
|
||||
}
|
||||
input.remove();
|
||||
});
|
||||
|
||||
input.click();
|
||||
}
|
||||
|
||||
function clearSkuNewImage(record: SkuFormRow) {
|
||||
revokeSkuPreview(record);
|
||||
record.newImageFile = null;
|
||||
skus.value = [...skus.value];
|
||||
message.success('已重置 SKU 图片');
|
||||
}
|
||||
|
||||
// 表格列配置
|
||||
const skuColumns = [
|
||||
{
|
||||
title: '图片',
|
||||
key: 'image',
|
||||
width: 88,
|
||||
align: 'center' as const,
|
||||
customRender: ({ record }: { record: SkuFormRow }) => {
|
||||
const previewUrl = getSkuPreviewUrl(record);
|
||||
const thumbStyle = {
|
||||
width: '48px',
|
||||
height: '48px',
|
||||
objectFit: 'cover' as const,
|
||||
borderRadius: '6px',
|
||||
border: '1px solid #d9d9d9',
|
||||
backgroundColor: '#f5f5f5',
|
||||
cursor: 'pointer',
|
||||
display: 'block',
|
||||
margin: '0 auto',
|
||||
};
|
||||
const emptyStyle = {
|
||||
width: '48px',
|
||||
height: '48px',
|
||||
border: '1px dashed #d9d9d9',
|
||||
borderRadius: '6px',
|
||||
backgroundColor: '#fafafa',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
color: '#999',
|
||||
cursor: 'pointer',
|
||||
margin: '0 auto',
|
||||
};
|
||||
const thumb = previewUrl
|
||||
? h('img', {
|
||||
src: previewUrl,
|
||||
alt: 'SKU 图',
|
||||
style: thumbStyle,
|
||||
onClick: () => selectSkuImageFile(record),
|
||||
})
|
||||
: h(
|
||||
'div',
|
||||
{
|
||||
style: emptyStyle,
|
||||
onClick: () => selectSkuImageFile(record),
|
||||
},
|
||||
[h(CameraIcon, { style: { fontSize: '16px' } })],
|
||||
);
|
||||
const children = [thumb];
|
||||
if (record.newImageFile) {
|
||||
children.push(
|
||||
h(
|
||||
Button,
|
||||
{
|
||||
type: 'link',
|
||||
size: 'small',
|
||||
style: { padding: '0', height: 'auto', marginTop: '2px' },
|
||||
onClick: () => clearSkuNewImage(record),
|
||||
},
|
||||
{ default: () => '重置' },
|
||||
),
|
||||
);
|
||||
}
|
||||
return h(
|
||||
'div',
|
||||
{
|
||||
style: {
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
padding: '4px 0',
|
||||
},
|
||||
},
|
||||
children,
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'SKU 名称',
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
width: 300,
|
||||
width: 260,
|
||||
customRender: ({
|
||||
record,
|
||||
}: {
|
||||
@@ -622,6 +792,19 @@ async function uploadImageAndGetId(): Promise<number | undefined> {
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadSkuImageAndGetId(file: File): Promise<number | undefined> {
|
||||
try {
|
||||
const fileInfo = await uploadFileToOss(file, {
|
||||
category: 'product-sku-image',
|
||||
errorMessage: 'SKU 图片上传失败',
|
||||
});
|
||||
return fileInfo.id;
|
||||
} catch (error) {
|
||||
console.error('SKU 图片上传失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 图片加载错误处理
|
||||
function handleImageError(event: Event) {
|
||||
console.error('图片加载失败:', event);
|
||||
@@ -635,6 +818,9 @@ onUnmounted(() => {
|
||||
const url = URL.createObjectURL(newImageFile.value);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
for (const row of skus.value) {
|
||||
revokeSkuPreview(row);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -798,6 +984,7 @@ onUnmounted(() => {
|
||||
:data-source="skus"
|
||||
:pagination="false"
|
||||
:row-key="(r: SkuFormRow) => (r.id != null ? String(r.id) : r._clientKey ?? '')"
|
||||
:scroll="{ x: 980 }"
|
||||
size="small"
|
||||
>
|
||||
<template #emptyText>
|
||||
|
||||
Reference in New Issue
Block a user