diff --git a/messenger.js b/messenger.js index eafaeb2..219561e 100644 --- a/messenger.js +++ b/messenger.js @@ -35,15 +35,16 @@ module.exports = { } */ if (result.messageId && result.response === '250 Ok') { - console.log({ _at: new Date().toJSON(), _state: 'MSG_SENT_SUCCESS', ...result }, '\n,') + //console.log({ _at: new Date().toJSON(), _state: 'MSG_SENT_SUCCESS', subject: messageObject.subject, ...result.envelope }, '\n,') return { _state: 'MSG_SENT_SUCCESS' } + } else { + console.log({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', subject: messageObject.subject, ...result }, '\n,') + return { _state: 'MSG_SEND_FAIL' } } - console.log({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', ...result }, '\n,') - return { _state: 'MSG_SEND_FAIL' } }) .catch((error) => { - console.error({ _at: new Date().toJSON(), _state: 'SMS_SEND_ERROR', error }, '\n,') - return { _state: 'SMS_SEND_ERROR' } + console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', subject: messageObject.subject, error }, '\n,') + return { _state: 'MSG_SEND_ERROR' } }) }, @@ -59,8 +60,8 @@ module.exports = { } = {}) { if (/^\+\d+-\d+$/.test(phone)) { if (!config?.vendor) { - console.error({ _at: new Date().toJSON(), _state: 'SMS_CONFIG_MALFORMED' }, '\n,') - return { _state: 'SMS_CONFIG_MALFORMED' } + console.error({ _at: new Date().toJSON(), _state: 'SMS_CONFIG_MALFORMED', config }, '\n,') + return { _state: 'SMS_CONFIG_MALFORMED', phone, config } } else if (config.vendor === 'DXTON') { return await this.sendSmsDxton({ phone, config, msg }) } else if (config.vendor === 'ALIYUN') { @@ -111,7 +112,7 @@ module.exports = { }) }) .on('error', (error) => { - reject({ _state: 'SMS_SEND_ERROR', error }) + reject({ _state: 'MSG_SEND_ERROR', error }) }) }) }, @@ -135,11 +136,13 @@ module.exports = { if (Code === 'OK') { return { _state: 'MSG_SENT_SUCCESS' } } else { + console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: result }, '\n,') return { _state: 'MSG_SEND_FAIL', error: result } } }, function (error) { - return { _state: 'SMS_SEND_ERROR', error } + console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,') + return { _state: 'MSG_SEND_ERROR', error } } ) }, @@ -162,11 +165,13 @@ module.exports = { if (Code === 'Ok') { return { _state: 'MSG_SENT_SUCCESS' } } else { + console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: { SendStatusSet, RequestId } }, '\n,') return { _state: 'MSG_SEND_FAIL', error: { SendStatusSet, RequestId } } } }, function (error) { - return { _state: 'SMS_SEND_ERROR', error } + console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,') + return { _state: 'MSG_SEND_ERROR', error } } ) }, @@ -182,14 +187,20 @@ module.exports = { data: msgParam, // 模版中的变量的值,例如 { passcode: '234345', purpose: '注册' } }) - return { _state: 'MSG_SENT_SUCCESS', result } // 调用成功,请注意这时不代表发送成功。// { code:0, errCode:0, success:true } 错误码参见 https://uniapp.dcloud.net.cn/uniCloud/send-sms.html + if (result?.errorCode === 0) { + return { _state: 'MSG_SENT_SUCCESS', result } // { code:0, errCode:0, success:true } + } else { + // 错误码参见 https://doc.dcloud.net.cn/uniCloud/sms/dev.html + console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: result }, '\n,') + return { _state: 'MSG_SEND_FAIL', result } + } } catch (error) { // 调用失败 例如 {"code":undefined,"msg":"短信发送失败:账户余额不足"} + console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,') return { - _state: 'MSG_SEND_FAIL', + _state: 'MSG_SEND_ERROR', error, // { errCode, errMsg } } } }, - }