package sms import ( "errors" "fmt" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" ter "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" ) type IsmsService interface { Send(code string, phone string) error } // ConsoleSmsService debug环境使用 type SmsService struct { client *sms.Client } func NewSmsService() IsmsService { credential := common.NewCredential( "AKIDa3HEyTihPs40f7JFvLVP3RcXCbkdFbhf", "oMefPt6pMiofGaigPuAByWq23pMejZBX", ) // 实例化一个client选项,可选的,没有特殊需求可以跳过 cpf := profile.NewClientProfile() cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com" // 实例化要请求产品的client对象,clientProfile是可选的 client, _ := sms.NewClient(credential, "ap-guangzhou", cpf) return &SmsService{client: client} } func (s *SmsService) Send(code string, phone string) error { //fmt.Printf("手机号【%s】验证码【%s】\n", phone, code) //return nil request := sms.NewSendSmsRequest() request.PhoneNumberSet = common.StringPtrs([]string{"+86" + phone}) request.SmsSdkAppId = common.StringPtr("1400237962") request.TemplateId = common.StringPtr("387221") request.SignName = common.StringPtr("上海路诚数据服务") request.TemplateParamSet = common.StringPtrs([]string{code, "3"}) response, err := s.client.SendSms(request) if _, ok := err.(*ter.TencentCloudSDKError); ok { return err } if err != nil { return err } if len(response.Response.SendStatusSet) == 0 { return errors.New("发送失败") } if *response.Response.SendStatusSet[0].Code != "Ok" { return errors.New(fmt.Sprintf("发送失败[%s]", response.Response.SendStatusSet[0].Message)) } //fmt.Printf("【中国移动】 您的验证码为:%s,您正在进行身份验证,该验证码10分钟内有效,如非本人操作,请忽略本短信!\n", msg) return nil }