56 lines
1.7 KiB
JavaScript
56 lines
1.7 KiB
JavaScript
function splitTags(tags) {
|
||
if (!tags) return [];
|
||
return String(tags)
|
||
.split(/[,#,\s]+/)
|
||
.map(s => s.trim())
|
||
.filter(Boolean);
|
||
}
|
||
|
||
function buildPushMessage(product) {
|
||
const intro = product.push_intro || '🔥 商家产品精选推荐';
|
||
const merchant = product.merchant_name || '未知商家';
|
||
const lines = [intro];
|
||
|
||
// 配置信息
|
||
if (product.spec_summary || product.traffic || product.coupon_code) {
|
||
const extras = [];
|
||
if (product.spec_summary) extras.push(`💻 ${product.spec_summary}`);
|
||
if (product.traffic) extras.push(`📊 流量 ${product.traffic}`);
|
||
if (product.coupon_code) extras.push(`🎟 优惠码: \`${product.coupon_code}\``);
|
||
lines.push(extras.join('|'));
|
||
}
|
||
|
||
lines.push('');
|
||
|
||
// 标题行
|
||
const titleBits = [];
|
||
if (product.location) titleBits.push(product.location);
|
||
titleBits.push(product.name);
|
||
lines.push(titleBits.join('|'));
|
||
|
||
// 价格
|
||
if (product.price) lines.push(`💰 ${product.price}`);
|
||
if (product.annual_price) {
|
||
const annualPrefix = product.billing_cycle ? `${product.billing_cycle}:` : '年付:';
|
||
lines.push(`${annualPrefix}${product.annual_price}`);
|
||
}
|
||
|
||
// aff 链接(优先用 generated_aff_url)
|
||
const link = product.generated_aff_url || product.buy_url || product.url;
|
||
if (link) lines.push(`✅ 购买: ${link}`);
|
||
|
||
// 标签
|
||
const tags = splitTags(product.tags);
|
||
if (tags.length) {
|
||
lines.push('');
|
||
lines.push(tags.map(t => (t.startsWith('#') ? t : `#${t}`)).join(' '));
|
||
}
|
||
|
||
lines.push('');
|
||
lines.push(`#${merchant.replace(/\s+/g, '')}`);
|
||
|
||
return lines.filter((line, idx, arr) => !(line === '' && arr[idx - 1] === '')).join('\n').trim();
|
||
}
|
||
|
||
module.exports = { buildPushMessage };
|