diff --git a/index.js b/index.js index d134fce..384c3d7 100644 --- a/index.js +++ b/index.js @@ -7,12 +7,14 @@ let smtpTransporter = null let smsClient = null // 在调用时,才创建 smsClient,防止 wo.envi 还没有建立好。 module.exports = { - sendMail: async function (option) { + + async sendMail (option) { // 或者如果smtp参数已经确定,就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp) smtpTransporter = smtpTransporter || NodeMailer.createTransport(wo.envi.SMTP) return await util.promisify(smtpTransporter.sendMail).call(smtpTransporter, option) }, - sendSms: async function (phone, option) { + + async sendSms (phone, option) { // 通过option对象,对外提供统一的调用参数格式 if (/^\+\d+-\d+$/.test(phone)) { option = option || {} @@ -24,7 +26,8 @@ module.exports = { } return null // 手机号格式错误,或者 option.vendor 错误。 }, - sendSmsDxton: async function (phone, msg) { + + async sendSmsDxton (phone, msg) { // 使用 dxton.com 的短信接口。 var matches = phone.match(/\d+/g) var smsNumber, smsUrl @@ -37,11 +40,11 @@ module.exports = { } // return Bluebird.promisify(Http.get)(smsUrl+'&mobile='+smsNumber+"&content="+encodeURIComponent(msg)); let returnValue = await RequestPromise.get(smsUrl + '&mobile=' + smsNumber + '&content=' + encodeURIComponent(msg)) - let result = { state: 'FAIL', code: returnValue } + let result = { _state: 'SMS_SEND_FAIL', code: returnValue } switch (parseInt(returnValue)) { // 短信接口代码:http://www.dxton.com/help_detail/2.html case 100: - return { state: 'DONE', code: '100', msg: 'sendSms: 发送成功 (表示已和我们接口连通)' } + return { _state: 'SMS_SENT_SUCCESS', code: '100', msg: 'sendSms: 发送成功 (表示已和我们接口连通)' } case 101: result.msg = 'sendSms: 验证失败(账号、密码可能错误)' case 102: @@ -69,7 +72,8 @@ module.exports = { return result } }, - sendSmsAliyun: async function (phone, msgParam, templateCode, signName) { + + async sendSmsAliyun (phone, msgParam, templateCode, signName) { // msgParam 是消息模板参数对象,例如 { code: "890353" } smsClient = smsClient || @@ -97,13 +101,13 @@ module.exports = { function (res) { let { Code } = res if (Code === 'OK') { - return { state: 'DONE' } + return { _state: 'SMS_SENT_SUCCESS' } } else { - return { state: 'FAIL', result: res } + return { _state: 'SMS_SEND_FAIL', result: res } } }, function (err) { - return { state: 'ERROR', error: err } + return { _state: 'SMS_SEND_ERROR', error: err } } ) },