小程序识别结果逐行入库;保质期拆数值与单位(AI 三选一或人手选)落库原文;选择商品弹层 root-portal 防 tab 遮挡,并预填 AI 品名。 Co-authored-by: Cursor <cursoragent@cursor.com>
120 lines
4.6 KiB
JavaScript
120 lines
4.6 KiB
JavaScript
/**
|
||
* 保质期「原文 ⇄ 数值 + 单位」共用口径(2026-10-15)。
|
||
*
|
||
* 背景(开发者指令):入库多商品识别时,**保质期单位由 AI 在「年 / 月 / 日」三者中选一个**,
|
||
* 识别不到就让用户**自己选**(界面上数值框 + 单位选择器)。落库仍是**原文**(`inventory.shelf_life`
|
||
* 如「6个月」「180天」「1年」),有效期由服务端 `deriveExpiryDate` 推算 —— 与历史数据同形,读侧无感。
|
||
*
|
||
* ⚠️ 与服务端 `server/src/utils/shelfLife.ts` 同一套口径(月=30 天、年=360 天在服务端算,这里只管拆/拼):
|
||
* 改这里必须同步改那边,`server/tests/inventoryShelfLife.test.ts` 有契约用例钉住两边一致。
|
||
*/
|
||
|
||
/** 单位选项(顺序即选择器顺序):年 / 月 / 日 */
|
||
const SHELF_LIFE_UNITS = ['年', '月', '日'];
|
||
|
||
/** 原文 → { n, unit }(支持「7天」「180日」「6个月」「24个月」「1年」「常温12个月」);识别不出 null */
|
||
function parseShelfLifeText(text) {
|
||
const m = String(text == null ? '' : text)
|
||
.trim()
|
||
.match(/(\d+)\s*(天|日|个月|月|年)/);
|
||
if (!m) return null;
|
||
const n = Number(m[1]);
|
||
if (!Number.isFinite(n) || n <= 0) return null;
|
||
const u = m[2];
|
||
if (u === '天' || u === '日') return { n, unit: '日' };
|
||
if (u === '个月' || u === '月') return { n, unit: '月' };
|
||
return { n, unit: '年' };
|
||
}
|
||
|
||
/** { 数值, 单位 } → 原文(年→`N年`、月→`N个月`、日/天→`N天`);数值非法或单位缺 ⇒ '' */
|
||
function composeShelfLifeText(value, unit) {
|
||
const n = Number(value);
|
||
if (!Number.isFinite(n) || n <= 0) return '';
|
||
const u = String(unit == null ? '' : unit).trim();
|
||
if (u === '年') return `${n}年`;
|
||
if (u === '月' || u === '个月') return `${n}个月`;
|
||
if (u === '日' || u === '天') return `${n}天`;
|
||
return '';
|
||
}
|
||
|
||
/** 归一到选择器三档:年 / 月 / 日;其它 → '' */
|
||
function normalizeShelfLifeUnit(unit) {
|
||
const u = String(unit == null ? '' : unit).trim();
|
||
if (u === '年') return '年';
|
||
if (u === '月' || u === '个月') return '月';
|
||
if (u === '日' || u === '天') return '日';
|
||
return '';
|
||
}
|
||
|
||
function shelfLifeUnitIndex(unit) {
|
||
const i = SHELF_LIFE_UNITS.indexOf(normalizeShelfLifeUnit(unit));
|
||
return i >= 0 ? i : 0;
|
||
}
|
||
|
||
/**
|
||
* 把一段保质期文本铺进行的两个控件(数值框 + 单位选择器),并同步原文 `shelf_life`。
|
||
* 解析不出单位时:数值框保留原文(用户能看见并改),单位留空 ⇒ 提交时按原文原样送出(兼容历史自由文本)。
|
||
*/
|
||
function applyShelfLifeToLine(line, text) {
|
||
const raw = String(text == null ? '' : text).trim();
|
||
const parsed = parseShelfLifeText(raw);
|
||
line.shelf_life = raw;
|
||
line.shelf_life_value = parsed ? String(parsed.n) : raw;
|
||
line.shelf_life_unit = parsed ? parsed.unit : '';
|
||
line.shelf_life_unit_index = shelfLifeUnitIndex(line.shelf_life_unit);
|
||
return line;
|
||
}
|
||
|
||
/**
|
||
* AI 回填:优先「数值 + 单位」;单位未识别时仍填数字、单位留空给人自选;
|
||
* 再兼容旧字段 shelf_life 原文。
|
||
*/
|
||
function applyShelfLifeFromAi(line, item) {
|
||
const valueRaw = item && item.shelf_life_value;
|
||
const value =
|
||
valueRaw !== undefined && valueRaw !== null && String(valueRaw).toLowerCase() !== 'null'
|
||
? String(valueRaw).trim()
|
||
: '';
|
||
const unit = normalizeShelfLifeUnit(item && item.shelf_life_unit);
|
||
const composed = composeShelfLifeText(value, unit);
|
||
if (composed) {
|
||
applyShelfLifeToLine(line, composed);
|
||
return line;
|
||
}
|
||
if (value || unit) {
|
||
line.shelf_life_value = value;
|
||
line.shelf_life_unit = unit;
|
||
line.shelf_life = shelfLifeTextFromLine(line);
|
||
line.shelf_life_unit_index = shelfLifeUnitIndex(unit);
|
||
return line;
|
||
}
|
||
const legacy = String((item && item.shelf_life) || '').trim();
|
||
if (legacy && legacy.toLowerCase() !== 'null') {
|
||
applyShelfLifeToLine(line, legacy);
|
||
} else {
|
||
line.shelf_life = '';
|
||
line.shelf_life_value = '';
|
||
line.shelf_life_unit = '';
|
||
line.shelf_life_unit_index = 0;
|
||
}
|
||
return line;
|
||
}
|
||
|
||
/** 两个控件 → 原文(优先按「数值 + 单位」组装;组装不出就把数值框当原文送出) */
|
||
function shelfLifeTextFromLine(line) {
|
||
const value = String((line && line.shelf_life_value) || '').trim();
|
||
const unit = String((line && line.shelf_life_unit) || '').trim();
|
||
return composeShelfLifeText(value, unit) || value;
|
||
}
|
||
|
||
module.exports = {
|
||
SHELF_LIFE_UNITS,
|
||
parseShelfLifeText,
|
||
composeShelfLifeText,
|
||
normalizeShelfLifeUnit,
|
||
shelfLifeUnitIndex,
|
||
applyShelfLifeToLine,
|
||
applyShelfLifeFromAi,
|
||
shelfLifeTextFromLine,
|
||
};
|