mirror of
https://gitee.com/blackfox/geekai.git
synced 2024-12-02 20:28:00 +08:00
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"chatplus/core/types"
|
|
"fmt"
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
|
|
)
|
|
|
|
type AliYunSmsService struct {
|
|
config *types.AliYunSmsConfig
|
|
client *dysmsapi.Client
|
|
}
|
|
|
|
func NewAliYunSmsService(config *types.AppConfig) (*AliYunSmsService, error) {
|
|
// 创建阿里云短信客户端
|
|
client, err := dysmsapi.NewClientWithAccessKey(
|
|
"cn-hangzhou",
|
|
config.SmsConfig.AccessKey,
|
|
config.SmsConfig.AccessSecret)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create client: %v", err)
|
|
}
|
|
|
|
return &AliYunSmsService{
|
|
config: &config.SmsConfig,
|
|
client: client,
|
|
}, nil
|
|
}
|
|
|
|
func (s *AliYunSmsService) SendVerifyCode(mobile string, code int) error {
|
|
// 创建短信请求并设置参数
|
|
request := dysmsapi.CreateSendSmsRequest()
|
|
request.Scheme = "https"
|
|
request.Domain = s.config.Domain
|
|
request.PhoneNumbers = mobile
|
|
request.SignName = s.config.Sign
|
|
request.TemplateCode = s.config.CodeTempId
|
|
request.TemplateParam = fmt.Sprintf("{\"code\":\"%d\"}", code) // 短信模板中的参数
|
|
|
|
// 发送短信
|
|
response, err := s.client.SendSms(request)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to send SMS:%v", err)
|
|
}
|
|
|
|
if response.Code != "OK" {
|
|
return fmt.Errorf("failed to send SMS:%v", response.Message)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
var _ SmsService = &AliYunSmsService{}
|