Files
tangzhuan-yu/frontend/utils/deliveryTemplate.ts
T
jinghui b3377dccd6 feat: 躺赚鱼 v3.6.2 —— xianyu-auto-reply 深度定制版首发
新增: 实时热卖榜(11分类/想要人数排序/热门搜索词热度)、价格监控(目标价通知/卖家信用筛选/销量下限/AI精准监控)、
一键转卖(收藏自动加载/改价直发/详情直达)、AI回复免费token(ai.88531.cn)、API密钥掩码、
推理模型兼容修复、商品本地编辑、授权系统接入(在线人数/版本/公告)、品牌更名躺赚鱼。

README 重写为项目介绍(改动清单/优势/真实截图/两种安装方式/体验地址 tzy.88531.cn/使用引导),
原版 README 完整保留为 README.upstream.md。
2026-11-16 01:00:59 +08:00

100 lines
3.3 KiB
TypeScript

/**
* 卡密发货详情文案:参数词表、图片标记与预览。
*
* 文案用 `{参数}` 占位,发送时由后端 app/delivery_template.py 填充真实订单信息;
* `{分隔符}` 拆分多条消息,`{图片N}` 从图片映射取真实图片消息,
* 卖家不需要在文案里直接维护图片 URL。
*/
import { MESSAGE_SPLIT_TOKEN } from './messageTemplate';
export const DELIVERY_SPLIT_TOKEN = MESSAGE_SPLIT_TOKEN;
export const DELIVERY_CONTENT_TOKEN = '{发货内容}';
/** 旧版备注使用的英文变量,继续识别但不再作为插入按钮展示。 */
export const LEGACY_DELIVERY_CONTENT_TOKEN = '{DELIVERY_CONTENT}';
export interface DeliveryTemplateToken {
label: string;
sample: string;
}
/** 可插入参数:label 即 `{label}` 占位符,sample 仅用于预览。 */
export const DELIVERY_TEMPLATE_TOKENS: DeliveryTemplateToken[] = [
{ label: '发货内容', sample: 'CARD-1234-5678' },
{ label: '订单号', sample: '2409123456789' },
{ label: '商品标题', sample: '示例商品' },
{ label: '商品ID', sample: '123456789' },
{ label: '买家ID', sample: '987654321' },
{ label: '规格名称', sample: '套餐' },
{ label: '规格值', sample: '月卡' },
{ label: '发货数量', sample: '1' },
];
export const buildDeliverySampleValues = (): Record<string, string> =>
Object.fromEntries(DELIVERY_TEMPLATE_TOKENS.map(({ label, sample }) => [label, sample]));
/** 取下一个图片映射键(纯数字字符串),配合 `{图片N}` 标记使用。 */
export const nextDeliveryImageKey = (images: Record<string, string>): string => {
const used = Object.keys(images)
.map((key) => Number(key))
.filter((value) => Number.isFinite(value));
return String(Math.max(0, ...used) + 1);
};
export const renderDeliveryTemplate = (
template: string,
values: Record<string, string>,
): string => template.replace(/\{([^{}]+)\}/g, (match, name: string) => values[name.trim()] ?? match);
export type DeliveryPreviewSegment =
| { type: 'text'; content: string }
| { type: 'image'; url: string; label: string }
| { type: 'divider' };
const MARKER_RE = /\{图片(\d+)\}|\{分隔符\}/g;
/** 按发送语义拆分预览:参数先用示例值渲染,再拆成文本/图片/分条。 */
export const buildDeliveryPreviewSegments = (
template: string,
images: Record<string, string>,
values: Record<string, string>,
): DeliveryPreviewSegment[] => {
const rendered = renderDeliveryTemplate(template || '', values);
const segments: DeliveryPreviewSegment[] = [];
let buffer = '';
let position = 0;
const flush = () => {
const text = buffer.trim();
buffer = '';
if (text) segments.push({ type: 'text', content: text });
};
for (const match of rendered.matchAll(MARKER_RE)) {
const index = match.index ?? 0;
buffer += rendered.slice(position, index);
position = index + match[0].length;
if (match[0] === DELIVERY_SPLIT_TOKEN) {
flush();
segments.push({ type: 'divider' });
continue;
}
const key = match[1];
const url = images[key] || images[`图片${key}`];
if (url) {
flush();
segments.push({ type: 'image', url, label: `图片${key}` });
} else {
buffer += match[0];
}
}
buffer += rendered.slice(position);
flush();
return segments;
};