|
|
@@ -0,0 +1,73 @@
|
|
|
+package sms
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+
|
|
|
+ aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"
|
|
|
+
|
|
|
+ "github.com/runningwater/gohub/pkg/logger"
|
|
|
+)
|
|
|
+
|
|
|
+// Aliyun 阿里云短信驱动,实现 Driver 接口
|
|
|
+type Aliyun struct{}
|
|
|
+
|
|
|
+// Send 实现短信驱动接口,通过阿里云服务发送短信
|
|
|
+// 参数:
|
|
|
+//
|
|
|
+// phone : 接收方手机号码(格式:国际区号+手机号,示例:+8613711112222)
|
|
|
+// message : 短信消息体,包含模板ID和模板参数
|
|
|
+// config : 阿里云访问配置(需包含 access_key_id 和 access_key_secret)
|
|
|
+//
|
|
|
+// 返回值:
|
|
|
+//
|
|
|
+// bool : 发送成功返回 true,失败返回 false
|
|
|
+func (a *Aliyun) Send(phone string, message Message, config map[string]string) bool {
|
|
|
+
|
|
|
+ const moduleName = "短信[阿里云]"
|
|
|
+
|
|
|
+ smsClient := aliyunsmsclient.New("https://dysmsapi.aliyuncs.com/")
|
|
|
+
|
|
|
+ templateParam, err := json.Marshal(message.Data)
|
|
|
+ if err != nil {
|
|
|
+ logger.ErrorString(moduleName, "解析模板参数失败", err.Error())
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.DebugJSON(moduleName, "配置信息", config)
|
|
|
+
|
|
|
+ // 调用阿里云短信服务API(参数顺序需与官方SDK保持一致)
|
|
|
+ result, err := smsClient.Execute(
|
|
|
+ config["access_key_id"], // 访问密钥ID
|
|
|
+ config["access_key_secret"], // 访问密钥
|
|
|
+ phone, // 接收手机号
|
|
|
+ config["sign_name"], // 短信签名
|
|
|
+ message.Template, // 模板ID
|
|
|
+ string(templateParam), // JSON格式的模板参数
|
|
|
+ )
|
|
|
+
|
|
|
+ // 记录原始请求和响应(用于调试和审计)
|
|
|
+ logger.DebugJSON(moduleName, "请求内容", smsClient.Request)
|
|
|
+ logger.DebugJSON(moduleName, "接口响应", result)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ logger.ErrorString(moduleName, "发送失败", err.Error())
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ // 序列化服务端响应结果
|
|
|
+ resultJson, err := json.Marshal(result)
|
|
|
+ if err != nil {
|
|
|
+ logger.ErrorString(moduleName, "解析接口响应 JSON 失败", err.Error())
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据官方SDK提供的方法判断是否成功
|
|
|
+ if result.IsSuccessful() {
|
|
|
+ logger.DebugString(moduleName, "发送成功", string(resultJson))
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ logger.ErrorString(moduleName, "发送失败, 服务商返回错误", string(resultJson))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+}
|