You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.9 KiB
96 lines
2.9 KiB
package service |
|
|
|
import ( |
|
"fmt" |
|
"github.com/gogf/gf/v2/encoding/gjson" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"github.com/gogf/gf/v2/os/gctx" |
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" |
|
"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" |
|
"log" |
|
) |
|
|
|
type ITencentSms interface { |
|
ConfigInit() |
|
Main(args []string) (_err error) |
|
} |
|
|
|
type tencentSmsServiceTmpl struct { |
|
} |
|
|
|
var tencentSmsService = tencentSmsServiceTmpl{} |
|
|
|
func TencentSms() ITencentSms { |
|
return &tencentSmsService |
|
} |
|
|
|
type TencentSmsData struct { |
|
AccessKeyId string |
|
AccessSecret string |
|
AppId string |
|
CodeSignName string |
|
TemplateCode string |
|
} |
|
|
|
var ( |
|
tencentSmsConfig TencentSmsData |
|
) |
|
|
|
func init() { |
|
tencentSmsService.ConfigInit() |
|
} |
|
|
|
func (a *tencentSmsServiceTmpl) ConfigInit() { |
|
//加载游戏相关配置 |
|
ctx := gctx.New() |
|
|
|
tencentSmsConfig.AccessKeyId = g.Cfg().MustGet(ctx, "game.tencentSms.accessKeyId").String() // 替换为你的 SecretId |
|
tencentSmsConfig.AccessSecret = g.Cfg().MustGet(ctx, "game.tencentSms.accessSecret").String() // 替换为你的 SecretKey |
|
tencentSmsConfig.AppId = g.Cfg().MustGet(ctx, "game.tencentSms.appId").String() // 短信应用ID (SdkAppId) |
|
tencentSmsConfig.CodeSignName = g.Cfg().MustGet(ctx, "game.tencentSms.codeSignName").String() // 已审核通过的短信签名内容 |
|
tencentSmsConfig.TemplateCode = g.Cfg().MustGet(ctx, "game.tencentSms.templateCode").String() // 已审核通过的模板ID |
|
|
|
log.Println("tencentSms init", gjson.MustEncodeString(tencentSmsConfig)) |
|
} |
|
|
|
func (a *tencentSmsServiceTmpl) Main(args []string) (_err error) { |
|
// 初始化认证对象 |
|
credential := common.NewCredential(tencentSmsConfig.AccessKeyId, tencentSmsConfig.AccessSecret) |
|
|
|
// 初始化客户端配置 |
|
cpf := profile.NewClientProfile() |
|
cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com" // 接口域名 |
|
|
|
// 创建 SMS 客户端 |
|
client, _ := sms.NewClient(credential, "ap-guangzhou", cpf) // 地域根据实际情况选择 |
|
|
|
// 构建请求 |
|
request := sms.NewSendSmsRequest() |
|
request.SmsSdkAppId = common.StringPtr(tencentSmsConfig.AppId) |
|
request.SignName = common.StringPtr(tencentSmsConfig.CodeSignName) |
|
request.TemplateId = common.StringPtr(tencentSmsConfig.TemplateCode) |
|
request.PhoneNumberSet = common.StringPtrs([]string{"+86" + args[0]}) |
|
|
|
// 模板参数(根据模板要求填写,若无参数则留空) |
|
templateParams := []string{args[1], "10"} // 示例验证码 |
|
request.TemplateParamSet = common.StringPtrs(templateParams) |
|
|
|
// 发送请求 |
|
response, err := client.SendSms(request) |
|
|
|
// 错误处理 |
|
if _, ok := err.(*errors.TencentCloudSDKError); ok { |
|
fmt.Printf("API error: %v\n", err) |
|
return |
|
} |
|
if err != nil { |
|
fmt.Printf("Failed: %v\n", err) |
|
return |
|
} |
|
|
|
// 打印结果 |
|
fmt.Printf("Response: %s\n", response.ToJsonString()) |
|
return |
|
}
|
|
|