Files
t-admin/uniCloud-alipay/cloudfunctions/uni-sms-co/build-template-data.js
T

23 lines
879 B
JavaScript

module.exports = (templateData = [], user = {}) => {
const data = {};
if (!Array.isArray(templateData)) return data;
for (const template of templateData) {
if (!template || typeof template.field !== 'string') continue;
const value = template.value;
const isDynamic = typeof value === 'string' && /\{.*?\}/.test(value);
if (isDynamic) {
const [collection, field] = value.replace(/\{|\}/g, '').split('.');
// 保留 0、false、空字符串;仅在用户字段确实缺失时使用模板原值。
const userValue = collection === 'uni-id-users' && field &&
Object.prototype.hasOwnProperty.call(user, field) ? user[field] : undefined;
data[template.field] = userValue === undefined || userValue === null ? value : userValue;
} else {
data[template.field] = value;
}
}
return data;
};