You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.8 KiB
76 lines
2.8 KiB
'use strict';
|
|
|
|
module.exports = async (app, conf) => {
|
|
app.fs = app.fs || {}
|
|
|
|
if (conf.sms?.ali) {
|
|
const Core = require('@alicloud/pop-core');
|
|
const smsAccessKeyId = String(
|
|
conf.sms.ali.accessKeyId || conf.sms.ali.accessKey || ''
|
|
).trim();
|
|
const smsAccessKeySecret = String(
|
|
conf.sms.ali.accessKeySecret || conf.sms.ali.accessSecret || ''
|
|
).trim();
|
|
|
|
if (!smsAccessKeyId || !smsAccessKeySecret) {
|
|
app.fs.logger.warn('[SMS-ALI] 短信配置缺少 accessKeyId/accessKeySecret,短信服务未启用');
|
|
return;
|
|
}
|
|
|
|
const smsAli = async ({ phone = [], templateCode, templateParam, SignName } = {}) => {
|
|
try {
|
|
if (phone.length) {
|
|
const client = new Core({
|
|
accessKeyId: smsAccessKeyId,
|
|
accessKeySecret: smsAccessKeySecret,
|
|
endpoint: 'https://dysmsapi.aliyuncs.com',//固定
|
|
apiVersion: '2017-05-25'//固定
|
|
});
|
|
const SendSmsRes = await client.request('SendSms', {
|
|
"PhoneNumbers": phone.join(','),//接收短信的手机号码。
|
|
"SignName": SignName,//短信签名名称。必须是已添加、并通过审核的短信签名。
|
|
"TemplateCode": templateCode,//短信模板ID。必须是已添加、并通过审核的短信签名;且发送国际/港澳台消息时,请使用国际/港澳台短信模版。
|
|
"TemplateParam": JSON.stringify(templateParam)//短信模板变量对应的实际值,JSON格式。
|
|
}, {
|
|
method: 'POST'
|
|
});
|
|
return SendSmsRes
|
|
}
|
|
} catch (error) {
|
|
throw error
|
|
}
|
|
}
|
|
app.fs.smsAli = smsAli
|
|
}
|
|
|
|
if (conf.email) {
|
|
const nodemailer = require('nodemailer')
|
|
|
|
const email = async ({ email = [], title, text = '', html = '', attachments = undefined, } = {}) => {
|
|
try {
|
|
let transporter = nodemailer.createTransport({
|
|
host: conf.email.host,
|
|
port: conf.email.port,
|
|
secure: true,
|
|
auth: {
|
|
user: conf.email.address,
|
|
pass: conf.email.password,
|
|
}
|
|
});
|
|
|
|
// send mail with defined transport object
|
|
await transporter.sendMail({
|
|
from: `${conf.email.name}<${conf.email.address}>`, // sender address
|
|
to: email.join(','), // list of receivers 逗号分隔字符串
|
|
subject: title, // Subject line
|
|
text: text, // plain text body
|
|
html: html, // html body
|
|
attachments: attachments
|
|
});
|
|
} catch (error) {
|
|
throw error
|
|
}
|
|
}
|
|
app.fs.email = email
|
|
}
|
|
}
|
|
|