WMS_MAIN/app/lib/report/notifyTemplate.js

102 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 货主报表文本通知模板渲染器(纯函数模块,无 ctx 依赖,可单测)。
*
* 标签白名单(其余 {xxx} 一律不替换,保存入口由 reportDelivery.savePlan 校验拦截):
* - {time} 发送时间,格式 YYYY-MM-DD HH:mm
* - {url} 报表文件下载链接(url 为空时替换为兜底文案)
* - {report_name} 报表名称
* - {biz_date} 数据日期 YYYY-MM-DD
*/
/** 模板标签白名单 */
const ALLOWED_TAGS = Object.freeze([ 'time', 'url', 'report_name', 'biz_date' ]);
/**
* url 为空时的兜底文案。
* 理由:生成结果 url 可能因异步上传尚未就绪,以占位文案保证通知可达,
* 避免因链接缺失引发整链路失败重试风暴;实际链接可在投递记录中查看。
*/
const EMPTY_URL_FALLBACK = '文件生成中,稍后可在投递记录中查看';
/** 模板标签匹配正则:{xxx} 占位 */
const TAG_PATTERN = /\{([^{}]+)\}/g;
/**
* 提取模板中出现过的全部标签名(按出现顺序,含重复项)。
* @param {string} template 模板字符串
* @return {Array<string>} 标签名数组
*/
function extractTagNames(template) {
const names = [];
const source = String(template || '');
let match = TAG_PATTERN.exec(source);
while (match) {
names.push(String(match[1]).trim());
match = TAG_PATTERN.exec(source);
}
return names;
}
/**
* 校验模板标签是否全部位于白名单内(未知标签不替换、保存时拒绝)。
* @param {string} template 模板字符串
* @return {{valid: boolean, invalidTags: Array<string>}} 校验结果(invalidTags 已去重)
*/
function validateNotifyTemplate(template) {
const names = extractTagNames(template);
const invalidSet = new Set();
for (const name of names) {
if (!ALLOWED_TAGS.includes(name)) {
invalidSet.add(name);
}
}
return { valid: invalidSet.size === 0, invalidTags: [ ...invalidSet ] };
}
/**
* 渲染通知模板:仅替换白名单标签,未知标签原样保留(保存入口已校验拦截)。
* @param {string} template 模板字符串
* @param {Object} [context={}] 渲染上下文
* @param {string} [context.time=''] 发送时间(YYYY-MM-DD HH:mm)
* @param {string} [context.url=''] 报表文件下载链接(空则替换为兜底文案)
* @param {string} [context.reportName=''] 报表名称
* @param {string} [context.bizDate=''] 数据日期(YYYY-MM-DD)
* @return {string} 渲染后的通知文本
*/
function renderNotifyTemplate(template, context = {}) {
const url = String((context && context.url) || '').trim() || EMPTY_URL_FALLBACK;
const replacements = {
time: String((context && context.time) || ''),
url,
report_name: String((context && context.reportName) || ''),
biz_date: String((context && context.bizDate) || ''),
};
return String(template || '').replace(TAG_PATTERN, (raw, name) => {
const key = String(name).trim();
return Object.prototype.hasOwnProperty.call(replacements, key) ? replacements[key] : raw;
});
}
/**
* 格式化发送时间为 YYYY-MM-DD HH:mm(服务器本地时区)。
* @param {Date} [date=new Date()] 时刻
* @return {string} 格式化后的时间字符串
*/
function formatNotifyTime(date) {
const target = date instanceof Date ? date : new Date();
const pad = value => String(value).padStart(2, '0');
return `${target.getFullYear()}-${pad(target.getMonth() + 1)}-${pad(target.getDate())} `
+ `${pad(target.getHours())}:${pad(target.getMinutes())}`;
}
module.exports = {
ALLOWED_TAGS,
EMPTY_URL_FALLBACK,
extractTagNames,
validateNotifyTemplate,
renderNotifyTemplate,
formatNotifyTime,
};