u
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
parseFullHash,
|
||||
articleErrorStatus,
|
||||
interpolateString,
|
||||
interpolateObjects,
|
||||
} from './shared.js';
|
||||
|
||||
/**
|
||||
@@ -366,7 +367,8 @@ export function register(app, basePath = '') {
|
||||
if (skuError) return res.status(400).json({ error: skuError });
|
||||
const sku = parsedSku;
|
||||
|
||||
const { templateId, agentprinterId, quantity } = req.body ?? {};
|
||||
const { templateId, quantity } = req.body ?? {};
|
||||
const agentprinterId = req.body?.agentprinterId ?? req.body?.printerId;
|
||||
|
||||
if (templateId === undefined || templateId === null) {
|
||||
return res.status(400).json({ error: 'templateId is required' });
|
||||
@@ -433,16 +435,7 @@ export function register(app, basePath = '') {
|
||||
? content
|
||||
: [];
|
||||
|
||||
const interpolatedObjects = objects.map(obj => {
|
||||
const cloned = { ...obj };
|
||||
if (typeof cloned.text === 'string') {
|
||||
cloned.text = interpolateString(cloned.text, article);
|
||||
}
|
||||
if (typeof cloned.code === 'string') {
|
||||
cloned.code = interpolateString(cloned.code, article);
|
||||
}
|
||||
return cloned;
|
||||
});
|
||||
const interpolatedObjects = interpolateObjects(objects, article);
|
||||
|
||||
const printPayload = await renderLabel({
|
||||
width: template.width,
|
||||
|
||||
@@ -149,7 +149,8 @@ export function interpolateString(str, article) {
|
||||
|
||||
export function interpolateObjects(objects, article) {
|
||||
if (!Array.isArray(objects)) return [];
|
||||
const ean = Array.isArray(article?.eans) && article.eans.length > 0 ? String(article.eans[0]) : '';
|
||||
const ean = Array.isArray(article?.eans) && article.eans.length > 0 ? String(article.eans[0]).trim() : '';
|
||||
const sku = String(article?.sku ?? '').trim();
|
||||
|
||||
return objects.map((obj) => {
|
||||
const cloned = { ...obj };
|
||||
@@ -161,6 +162,14 @@ export function interpolateObjects(objects, article) {
|
||||
if (rawCode === '0' || /^0+$/.test(rawCode)) {
|
||||
if (ean) {
|
||||
cloned.code = ean;
|
||||
if (ean.length === 8) {
|
||||
cloned.format = 'EAN8';
|
||||
} else if (ean.length === 12 || ean.length === 13) {
|
||||
cloned.format = 'EAN13';
|
||||
}
|
||||
} else if (sku) {
|
||||
cloned.code = sku;
|
||||
cloned.format = 'CODE128';
|
||||
}
|
||||
} else if (typeof cloned.code === 'string') {
|
||||
cloned.code = interpolateString(cloned.code, article);
|
||||
|
||||
@@ -96,12 +96,45 @@ export function register(app, basePath = '') {
|
||||
|
||||
try {
|
||||
const templates = await db.list(req.user.id);
|
||||
res.status(200).json({
|
||||
labeltemplates: templates.map((t) => {
|
||||
|
||||
let article = null;
|
||||
if (req.query?.sku) {
|
||||
const skuStr = String(req.query.sku).trim();
|
||||
const result = await articleDb.getBySku(req.user.id, skuStr);
|
||||
if (result?.article) {
|
||||
article = result.article;
|
||||
}
|
||||
}
|
||||
|
||||
const labeltemplates = await Promise.all(
|
||||
templates.map(async (t) => {
|
||||
const { content, ...rest } = serializeTemplate(t);
|
||||
if (article) {
|
||||
try {
|
||||
const full = await db.getById(req.user.id, t.id);
|
||||
const objects = Array.isArray(full?.content?.objects)
|
||||
? full.content.objects
|
||||
: Array.isArray(full?.content)
|
||||
? full.content
|
||||
: null;
|
||||
if (objects && objects.length > 0) {
|
||||
const interpolated = interpolateObjects(objects, article);
|
||||
const thumbBuffer = await renderThumb({
|
||||
width: t.width,
|
||||
height: t.height,
|
||||
objects: interpolated,
|
||||
});
|
||||
rest.thumb = thumbBuffer.toString('base64');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Failed to render preview thumb for template ${t.id} with sku ${req.query.sku}:`, err);
|
||||
}
|
||||
}
|
||||
return rest;
|
||||
})
|
||||
});
|
||||
);
|
||||
|
||||
res.status(200).json({ labeltemplates });
|
||||
} catch (error) {
|
||||
const status = labeltemplateErrorStatus(error);
|
||||
if (status === 500) console.error('List label templates failed:', error);
|
||||
@@ -175,7 +208,32 @@ export function register(app, basePath = '') {
|
||||
if (!template) {
|
||||
return res.status(404).json({ error: 'Label template not found' });
|
||||
}
|
||||
res.status(200).json({ labeltemplate: serializeTemplate(template) });
|
||||
const serialized = serializeTemplate(template);
|
||||
if (req.query?.sku) {
|
||||
const skuStr = String(req.query.sku).trim();
|
||||
const result = await articleDb.getBySku(req.user.id, skuStr);
|
||||
if (result?.article) {
|
||||
const objects = Array.isArray(serialized?.content?.objects)
|
||||
? serialized.content.objects
|
||||
: Array.isArray(serialized?.content)
|
||||
? serialized.content
|
||||
: null;
|
||||
if (objects && objects.length > 0) {
|
||||
try {
|
||||
const interpolated = interpolateObjects(objects, result.article);
|
||||
const thumbBuffer = await renderThumb({
|
||||
width: template.width,
|
||||
height: template.height,
|
||||
objects: interpolated,
|
||||
});
|
||||
serialized.thumb = thumbBuffer.toString('base64');
|
||||
} catch (err) {
|
||||
console.error(`Failed to render preview thumb for template ${template.id} with sku ${req.query.sku}:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
res.status(200).json({ labeltemplate: serialized });
|
||||
} catch (error) {
|
||||
const status = labeltemplateErrorStatus(error);
|
||||
if (status === 500) console.error('Get label template failed:', error);
|
||||
|
||||
Reference in New Issue
Block a user