|
|
@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
package tencent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
|
|
|
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|
|
|
|
|
|
|
sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20190711" //引入sms
|
|
|
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var Sms = &smsCli{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type SmsConfig struct {
|
|
|
|
|
|
|
|
SecretId string
|
|
|
|
|
|
|
|
SecretKey string
|
|
|
|
|
|
|
|
AppID string
|
|
|
|
|
|
|
|
Sign string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type smsCli struct {
|
|
|
|
|
|
|
|
config *SmsConfig
|
|
|
|
|
|
|
|
client *sms.Client
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// InitSms @Title 初始化连接工具
|
|
|
|
|
|
|
|
func InitSms(config *SmsConfig) {
|
|
|
|
|
|
|
|
Sms.config = config
|
|
|
|
|
|
|
|
credential := common.NewCredential(config.SecretId, config.SecretKey)
|
|
|
|
|
|
|
|
/* 非必要步骤:
|
|
|
|
|
|
|
|
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
|
|
|
|
|
|
|
cpf := profile.NewClientProfile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SDK 会自动指定域名,通常无需指定域名,但访问金融区的服务时必须手动指定域名
|
|
|
|
|
|
|
|
* 例如 SMS 的上海金融区域名为 sms.ap-shanghai-fsi.tencentcloudapi.com */
|
|
|
|
|
|
|
|
//cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SDK 默认用 TC3-HMAC-SHA256 进行签名,非必要请不要修改该字段 */
|
|
|
|
|
|
|
|
//cpf.SignMethod = "HmacSHA1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 实例化 SMS 的 client 对象
|
|
|
|
|
|
|
|
* 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量 */
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
Sms.client, err = sms.NewClient(credential, "ap-guangzhou", cpf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Fatalln("短信接口错误")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SendSms @Title 发送短信
|
|
|
|
|
|
|
|
func (s *smsCli) SendSms(templateID string, phones []string, params []string) error {
|
|
|
|
|
|
|
|
request := sms.NewSendSmsRequest()
|
|
|
|
|
|
|
|
/* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666 */
|
|
|
|
|
|
|
|
request.SmsSdkAppid = &s.config.AppID
|
|
|
|
|
|
|
|
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */
|
|
|
|
|
|
|
|
request.Sign = &s.config.Sign
|
|
|
|
|
|
|
|
/* 模板参数: 若无模板参数,则设置为空*/
|
|
|
|
|
|
|
|
request.TemplateParamSet = common.StringPtrs(params)
|
|
|
|
|
|
|
|
/* 模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID */
|
|
|
|
|
|
|
|
request.TemplateID = &templateID
|
|
|
|
|
|
|
|
/* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
|
|
|
|
|
|
|
|
* 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
|
|
|
|
|
|
|
|
for _, phone := range phones {
|
|
|
|
|
|
|
|
wordPhone := "+86" + phone
|
|
|
|
|
|
|
|
request.PhoneNumberSet = append(request.PhoneNumberSet, &wordPhone)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通过 client 对象调用想要访问的接口,需要传入请求对象
|
|
|
|
|
|
|
|
_, err := s.client.SendSms(request)
|
|
|
|
|
|
|
|
// 处理异常
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return errors.New("发送失败")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|