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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
}