From c2dc33357bc6ef56153f8b4b5a5dfed09be9b026 Mon Sep 17 00:00:00 2001 From: kanade <3136520963@qq.com> Date: Tue, 27 Apr 2021 14:26:04 +0800 Subject: [PATCH] ini --- README.md | 1 + go.mod | 5 ++++ go.sum | 2 ++ sms.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 sms.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff296d8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# 腾讯sdk工具 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f291608 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.oa00.com/go/tencent + +go 1.16 + +require github.com/tencentcloud/tencentcloud-sdk-go v1.0.143 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b6b7e1b --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/tencentcloud/tencentcloud-sdk-go v1.0.143 h1:Q5sSP5UHW30tdyJrH2GK78CFD4a8K8cIxw55UfSOn+k= +github.com/tencentcloud/tencentcloud-sdk-go v1.0.143/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI= diff --git a/sms.go b/sms.go new file mode 100644 index 0000000..ca5a8fe --- /dev/null +++ b/sms.go @@ -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 +}