From 21d44026d9f23ca7f91ee90cb2643e03c2f43ea1 Mon Sep 17 00:00:00 2001 From: jacy <18049821889@163.com> Date: Fri, 7 Aug 2026 20:46:26 +0800 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFfix(store):=20persist=20package=20imag?= =?UTF-8?q?eUrls=20on=20audit=20approve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ensure multi-image packages are written on approval and always returned as arrays from store detail. Co-authored-by: Cursor --- packages/shared-types/src/store-package.ts | 23 ++++++++++++++--- .../modules/store/store-package.service.ts | 25 +++++++++++++------ .../src/modules/store/store.service.ts | 3 ++- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/shared-types/src/store-package.ts b/packages/shared-types/src/store-package.ts index ba924c8..abb36e3 100644 --- a/packages/shared-types/src/store-package.ts +++ b/packages/shared-types/src/store-package.ts @@ -34,7 +34,7 @@ export const STORE_PACKAGE_IMAGE_MAX_COUNT = 20; /** 门店环境照最多张数(总部/合伙人上传) */ export const STORE_ENV_PHOTO_MAX_COUNT = 20; -/** 归一化套餐图片:兼容 imageUrl / imageUrls,去重后截断上限 */ +/** 归一化套餐图片:兼容 imageUrl / imageUrls(含 JSON 字符串),去重后截断上限 */ export function normalizeStorePackageImageUrls(input: { imageUrl?: string | null; imageUrls?: unknown; @@ -47,8 +47,25 @@ export function normalizeStorePackageImageUrls(input: { seen.add(url); out.push(url); }; - if (Array.isArray(input.imageUrls)) { - for (const item of input.imageUrls) push(item); + + let list: unknown = input.imageUrls; + if (typeof list === 'string') { + const trimmed = list.trim(); + if (trimmed.startsWith('[')) { + try { + list = JSON.parse(trimmed); + } catch { + list = trimmed ? [trimmed] : []; + } + } else if (trimmed) { + list = [trimmed]; + } else { + list = []; + } + } + + if (Array.isArray(list)) { + for (const item of list) push(item); } if (out.length === 0) push(input.imageUrl); return out.slice(0, STORE_PACKAGE_IMAGE_MAX_COUNT); diff --git a/server/dukang-api/src/modules/store/store-package.service.ts b/server/dukang-api/src/modules/store/store-package.service.ts index f1a58f0..13192ff 100644 --- a/server/dukang-api/src/modules/store/store-package.service.ts +++ b/server/dukang-api/src/modules/store/store-package.service.ts @@ -69,7 +69,7 @@ export class StorePackageService { usableTime, otherNotes, imageUrl: imageUrls[0] ?? null, - imageUrls: imageUrls.length ? imageUrls : null, + imageUrls, sortOrder: Number.isFinite(sortOrder) ? sortOrder : index, }; } @@ -97,7 +97,7 @@ export class StorePackageService { usableTime: row.usableTime, otherNotes: row.otherNotes, imageUrl: imageUrls[0] ?? null, - imageUrls: imageUrls.length ? imageUrls : null, + imageUrls, sortOrder: row.sortOrder, }; } @@ -229,6 +229,19 @@ export class StorePackageService { return serializeBigInt({ live }); } + private packageImageWrite(pkg: { + imageUrl?: string | null; + imageUrls?: string[] | null; + }) { + const imageUrls = normalizeStorePackageImageUrls(pkg); + return { + imageUrl: imageUrls[0] ?? null, + imageUrls: imageUrls.length + ? (imageUrls as Prisma.InputJsonValue) + : Prisma.JsonNull, + }; + } + private async replaceLivePackages(storeId: bigint, packages: StorePackageItemDto[]) { await this.prisma.$transaction([ this.prisma.storePackage.deleteMany({ where: { storeId } }), @@ -241,10 +254,7 @@ export class StorePackageService { dishes: pkg.dishes, usableTime: pkg.usableTime ?? null, otherNotes: pkg.otherNotes ?? null, - imageUrl: pkg.imageUrl ?? null, - imageUrls: pkg.imageUrls?.length - ? (pkg.imageUrls as Prisma.InputJsonValue) - : Prisma.JsonNull, + ...this.packageImageWrite(pkg), sortOrder: pkg.sortOrder ?? index, }, }), @@ -336,7 +346,7 @@ export class StorePackageService { }); return serializeBigInt({ id: updated.id.toString(), status: updated.status }); } - const packages = req.packagesJson as unknown as StorePackageItemDto[]; + const packages = this.normalizePackages(req.packagesJson); await this.prisma.$transaction(async (tx) => { await tx.storePackage.deleteMany({ where: { storeId: req.storeId } }); for (let i = 0; i < packages.length; i++) { @@ -349,6 +359,7 @@ export class StorePackageService { dishes: pkg.dishes, usableTime: pkg.usableTime ?? null, otherNotes: pkg.otherNotes ?? null, + ...this.packageImageWrite(pkg), sortOrder: pkg.sortOrder ?? i, }, }); diff --git a/server/dukang-api/src/modules/store/store.service.ts b/server/dukang-api/src/modules/store/store.service.ts index 90e2069..a5036db 100644 --- a/server/dukang-api/src/modules/store/store.service.ts +++ b/server/dukang-api/src/modules/store/store.service.ts @@ -247,7 +247,8 @@ export class StoreService { usableTime: p.usableTime, otherNotes: p.otherNotes, imageUrl: imageUrls[0] ?? null, - imageUrls: imageUrls.length ? imageUrls : null, + /** C 端始终返回数组,便于多图轮播 */ + imageUrls, sortOrder: p.sortOrder, }; }),