修改 aliyun,短信,oss,验证码

This commit is contained in:
chy
2025-06-24 16:22:18 +08:00
parent b02ad91118
commit 8342c92247
21 changed files with 219 additions and 128 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/credentials-go/credentials"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
"server/internal/model"
isms "server/utility/sms"
)
@ -16,6 +17,9 @@ import (
type aliyunClient struct {
accessKeyId string
accessKeySecret string
endpoint string
templateId string
signName string
}
// 初始化并注册
@ -24,16 +28,20 @@ func init() {
client := &aliyunClient{
accessKeyId: g.Config().MustGet(ctx, "sms.aliyun.accessKeyId").String(),
accessKeySecret: g.Config().MustGet(ctx, "sms.aliyun.accessKeySecret").String(),
endpoint: g.Config().MustGet(ctx, "sms.aliyun.endpoint").String(),
templateId: g.Config().MustGet(ctx, "sms.aliyun.templateId").String(),
signName: g.Config().MustGet(ctx, "sms.aliyun.signName").String(),
}
isms.Register("aliyunsms", client)
glog.Infof(ctx, "注册阿里云短信服务成功")
}
func (a *aliyunClient) client(ctx context.Context) (_result *dysmsapi20170525.Client, _err error) {
config := new(credentials.Config).
SetType("access_key").
SetAccessKeyId(g.Config().MustGet(ctx, "sms.aliyun.accessKeyId").String()).
SetAccessKeySecret(g.Config().MustGet(ctx, "sms.aliyun.accessKeySecret").String())
SetAccessKeyId(a.accessKeyId).
SetAccessKeySecret(a.accessKeySecret)
akCredential, err := credentials.NewCredential(config)
if err != nil {
@ -45,7 +53,7 @@ func (a *aliyunClient) client(ctx context.Context) (_result *dysmsapi20170525.Cl
}
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config1.Endpoint = tea.String("dysmsapi.aliyuncs.com")
config1.Endpoint = tea.String(a.endpoint)
_result = &dysmsapi20170525.Client{}
_result, _err = dysmsapi20170525.NewClient(config1)
@ -62,61 +70,25 @@ func (a *aliyunClient) SendCode(ctx context.Context, in *model.SMSCodeIn) (out *
// 创建请求对象并设置入参
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
// 需替换成为您的短信模板code
TemplateCode: tea.String("SMS_154950909"),
TemplateCode: tea.String(a.templateId),
// 需替换成为您的短信模板变量对应的实际值,示例值:{\"code\":\"1234\"}
TemplateParam: tea.String("{\"code\":\"1234\"}"),
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", in.Code)),
// 需替换成为您的接收手机号码
PhoneNumbers: tea.String(in.Phone),
// 需替换成为您的短信签名
SignName: tea.String("阿里云短信测试"),
SignName: tea.String(a.signName),
}
glog.Infof(ctx, "aliyun sms request: phone: %s, code: %s", in.Phone, in.Code)
runtime := &util.RuntimeOptions{}
result, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
if _err != nil {
return nil, _err
}
if result.Body.Code != tea.String("OK") {
if *result.Body.Code != `OK` && *result.Body.Code != `isv.BUSINESS_LIMIT_CONTROL` {
glog.Errorf(ctx, "阿里云短信发送失败")
return nil, fmt.Errorf("发送短信失败")
}
//tryErr := func() (_e error) {
// defer func() {
// if r := tea.Recover(recover()); r != nil {
// _e = r
// }
// }()
// // 复制代码运行请自行打印 API 的返回值
// result, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
// if _err != nil {
// return _err
// }
//
// return nil
//}()
//if tryErr != nil {
// var error = &tea.SDKError{}
// var _t *tea.SDKError
// if errors.As(tryErr, &_t) {
// error = _t
// }
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// // 错误 message
// fmt.Println(tea.StringValue(error.Message))
// // 诊断地址
// var data interface{}
// d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
// d.Decode(&data)
// if m, ok := data.(map[string]interface{}); ok {
// recommend, _ := m["Recommend"]
// fmt.Println(recommend)
// }
// _, _err = util.AssertAsString(error.Message)
// if _err != nil {
// return nil, _err
// }
//}
glog.Infof(ctx, "阿里云短信发送成功")
return &model.SMSCodeOut{Success: true}, err
}