You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.0 KiB
Go

1 month ago
package sms
import (
"errors"
1 month ago
"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"
1 month ago
)
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}
1 month ago
}
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
}
1 month ago
if err != nil {
return err
}
if len(response.Response.SendStatusSet) == 0 {
return errors.New("发送失败")
1 month ago
}
if *response.Response.SendStatusSet[0].Code != "Ok" {
return errors.New(fmt.Sprintf("发送失败[%s]", response.Response.SendStatusSet[0].Message))
}
1 month ago
//fmt.Printf("【中国移动】 您的验证码为:%s您正在进行身份验证该验证码10分钟内有效如非本人操作请忽略本短信\n", msg)
return nil
}