u
This commit is contained in:
		
							parent
							
								
									292e138732
								
							
						
					
					
						commit
						d27531ea00
					
				
							
								
								
									
										36
									
								
								messenger.js
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								messenger.js
									
									
									
									
									
								
							| @ -14,8 +14,8 @@ module.exports = { | |||||||
|   // 或者如果smtp参数已经确定,就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp)
 |   // 或者如果smtp参数已经确定,就可以直接定义 sendMail: Bluebird.promisify(Smtp.sendMail).bind(Smtp)
 | ||||||
|   async sendMail (messageObject, smtp = my.envar?.SMTP || wo?.envar?.SMTP) { |   async sendMail (messageObject, smtp = my.envar?.SMTP || wo?.envar?.SMTP) { | ||||||
|     if (!smtp?.host) { |     if (!smtp?.host) { | ||||||
|       console.error({ _at: new Date().toJSON(), _state: 'SMTP_CONFIG_MALFORMED' }, '\n,') |       globalThis.wo?.ccerror?.({ _state: 'SMTP_CONFIG_MALFORMED', smtp }) | ||||||
|       return { _state: 'SMTP_CONFIG_MALFORMED' } |       return { _state: 'SMTP_CONFIG_MALFORMED', smtp } | ||||||
|     } |     } | ||||||
|     // messageObject: { from, to, cc, bcc, subject, text, html, sender, replyTo, inReplyTo }
 |     // messageObject: { from, to, cc, bcc, subject, text, html, sender, replyTo, inReplyTo }
 | ||||||
|     sender.smtpTransporter = sender.smtpTransporter || NodeMailer.createTransport(smtp) |     sender.smtpTransporter = sender.smtpTransporter || NodeMailer.createTransport(smtp) | ||||||
| @ -35,15 +35,15 @@ module.exports = { | |||||||
|         } |         } | ||||||
|         */ |         */ | ||||||
|         if (result.messageId && result.response === '250 Ok') { |         if (result.messageId && result.response === '250 Ok') { | ||||||
|           console.log({ _at: new Date().toJSON(), _state: 'MSG_SENT_SUCCESS', subject: messageObject.subject, ...result.envelope }, '\n,') |           globalThis.wo?.cclog?.({ _state: 'MSG_SENT_SUCCESS', subject: messageObject.subject, ...result.envelope }) | ||||||
|           return { _state: 'MSG_SENT_SUCCESS' } |           return { _state: 'MSG_SENT_SUCCESS' } | ||||||
|         } else { |         } else { | ||||||
|           console.log({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', subject: messageObject.subject, ...result }, '\n,') |           globalThis.wo?.cclog?.({ _state: 'MSG_SEND_FAIL', subject: messageObject.subject, ...result }) | ||||||
|           return { _state: 'MSG_SEND_FAIL' } |           return { _state: 'MSG_SEND_FAIL' } | ||||||
|         } |         } | ||||||
|       }) |       }) | ||||||
|       .catch((error) => { |       .catch((error) => { | ||||||
|         console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', subject: messageObject.subject, error }, '\n,') |         globalThis.wo?.ccerror?.({ _state: 'MSG_SEND_ERROR', subject: messageObject.subject, error }) | ||||||
|         return { _state: 'MSG_SEND_ERROR' } |         return { _state: 'MSG_SEND_ERROR' } | ||||||
|       }) |       }) | ||||||
|   }, |   }, | ||||||
| @ -58,24 +58,28 @@ module.exports = { | |||||||
|     signName, // for 'ALIYUN', 'TENCENT'
 |     signName, // for 'ALIYUN', 'TENCENT'
 | ||||||
|     appid, // for 'UNICLOUD', 'TENCENT'
 |     appid, // for 'UNICLOUD', 'TENCENT'
 | ||||||
|   } = {}) { |   } = {}) { | ||||||
|  |     let result | ||||||
|     if (/^\+\d+-\d+$/.test(phone)) { |     if (/^\+\d+-\d+$/.test(phone)) { | ||||||
|       if (!config?.vendor) { |       if (!config?.vendor) { | ||||||
|         console.error({ _at: new Date().toJSON(), _state: 'SMS_CONFIG_MALFORMED', config }, '\n,') |         result = { _state: 'SMS_CONFIG_MALFORMED', phone, config } | ||||||
|         return { _state: 'SMS_CONFIG_MALFORMED', phone, config } |  | ||||||
|       } else if (config.vendor === 'DXTON') { |       } else if (config.vendor === 'DXTON') { | ||||||
|         return await this.sendSmsDxton({ phone, config, msg }) |         result = await this.sendSmsDxton({ phone, config, msg }) | ||||||
|       } else if (config.vendor === 'ALIYUN') { |       } else if (config.vendor === 'ALIYUN') { | ||||||
|         return await this.sendSmsAliyun({ phone, config, msgParam, msgTemplate, signName }) |         result = await this.sendSmsAliyun({ phone, config, msgParam, msgTemplate, signName }) | ||||||
|       } else if (config.vendor === 'TENCENT') { |       } else if (config.vendor === 'TENCENT') { | ||||||
|         return await this.sendSmsTencent({ phone, config, msgParam, msgTemplate, appid, signName }) |         result = await this.sendSmsTencent({ phone, config, msgParam, msgTemplate, appid, signName }) | ||||||
|       } else if (config.vendor === 'UNICLOUD') { |       } else if (config.vendor === 'UNICLOUD') { | ||||||
|         return await this.sendSmsUnicloud({ phone, config, msgParam, msgTemplate, appid }) |         result = await this.sendSmsUnicloud({ phone, config, msgParam, msgTemplate, appid }) | ||||||
|       } else { |       } else { | ||||||
|         return { _state: 'SMS_UNKNOWN_VENDOR', error: { unknownVendor: config.vendor } } |         result = { _state: 'SMS_UNKNOWN_VENDOR', error: { unknownVendor: config.vendor } } | ||||||
|       } |       } | ||||||
|     } else { |     } else { | ||||||
|       return { _state: 'SMS_INVALID_PHONE', error: {} } |       result = { _state: 'SMS_INVALID_PHONE', error: {} } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     globalThis.wo?.cclog?.({ phone, signName, msgParam, msgTemplate, ...result }) | ||||||
|  | 
 | ||||||
|  |     return result | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   /*  使用 dxton.com 的短信接口 http://www.dxton.com/help_detail/38.html |   /*  使用 dxton.com 的短信接口 http://www.dxton.com/help_detail/38.html | ||||||
| @ -136,12 +140,10 @@ module.exports = { | |||||||
|           if (Code === 'OK') { |           if (Code === 'OK') { | ||||||
|             return { _state: 'MSG_SENT_SUCCESS' } |             return { _state: 'MSG_SENT_SUCCESS' } | ||||||
|           } else { |           } else { | ||||||
|             console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: result }, '\n,') |  | ||||||
|             return { _state: 'MSG_SEND_FAIL', error: result } |             return { _state: 'MSG_SEND_FAIL', error: result } | ||||||
|           } |           } | ||||||
|         }, |         }, | ||||||
|         function (error) { |         function (error) { | ||||||
|           console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,') |  | ||||||
|           return { _state: 'MSG_SEND_ERROR', error } |           return { _state: 'MSG_SEND_ERROR', error } | ||||||
|         } |         } | ||||||
|       ) |       ) | ||||||
| @ -165,12 +167,10 @@ module.exports = { | |||||||
|           if (Code === 'Ok') { |           if (Code === 'Ok') { | ||||||
|             return { _state: 'MSG_SENT_SUCCESS' } |             return { _state: 'MSG_SENT_SUCCESS' } | ||||||
|           } else { |           } else { | ||||||
|             console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_FAIL', error: { SendStatusSet, RequestId } }, '\n,') |  | ||||||
|             return { _state: 'MSG_SEND_FAIL', error: { SendStatusSet, RequestId } } |             return { _state: 'MSG_SEND_FAIL', error: { SendStatusSet, RequestId } } | ||||||
|           } |           } | ||||||
|         }, |         }, | ||||||
|         function (error) { |         function (error) { | ||||||
|           console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,') |  | ||||||
|           return { _state: 'MSG_SEND_ERROR', error } |           return { _state: 'MSG_SEND_ERROR', error } | ||||||
|         } |         } | ||||||
|       ) |       ) | ||||||
| @ -191,12 +191,10 @@ module.exports = { | |||||||
|         return { _state: 'MSG_SENT_SUCCESS', result } // { code:0, errCode:0, success:true }
 |         return { _state: 'MSG_SENT_SUCCESS', result } // { code:0, errCode:0, success:true }
 | ||||||
|       } else { |       } else { | ||||||
|         // 错误码参见 https://doc.dcloud.net.cn/uniCloud/sms/dev.html
 |         // 错误码参见 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 } |         return { _state: 'MSG_SEND_FAIL', result } | ||||||
|       } |       } | ||||||
|     } catch (error) { |     } catch (error) { | ||||||
|       // 调用失败 例如 {"code":undefined,"msg":"短信发送失败:账户余额不足"}
 |       // 调用失败 例如 {"code":undefined,"msg":"短信发送失败:账户余额不足"}
 | ||||||
|       console.error({ _at: new Date().toJSON(), _state: 'MSG_SEND_ERROR', error }, '\n,') |  | ||||||
|       return { |       return { | ||||||
|         _state: 'MSG_SEND_ERROR', |         _state: 'MSG_SEND_ERROR', | ||||||
|         error, // { errCode, errMsg }
 |         error, // { errCode, errMsg }
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user