7 changed files with 121 additions and 16 deletions
@ -0,0 +1,93 @@ |
|||||||
|
package service |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"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" |
||||||
|
) |
||||||
|
|
||||||
|
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 ( |
||||||
|
tencnetSmsConfig TencentSmsData |
||||||
|
) |
||||||
|
|
||||||
|
func init() { |
||||||
|
tencentSmsService.ConfigInit() |
||||||
|
} |
||||||
|
|
||||||
|
func (a *tencentSmsServiceTmpl) ConfigInit() { |
||||||
|
//加载游戏相关配置
|
||||||
|
ctx := gctx.New() |
||||||
|
|
||||||
|
tencnetSmsConfig.AccessKeyId = g.Cfg().MustGet(ctx, "game.tencentSms.accessKeyId").String() // 替换为你的 SecretId
|
||||||
|
tencnetSmsConfig.AccessSecret = g.Cfg().MustGet(ctx, "game.tencentSms.accessSecret").String() // 替换为你的 SecretKey
|
||||||
|
tencnetSmsConfig.AppId = g.Cfg().MustGet(ctx, "game.tencentSms.appId").String() // 短信应用ID (SdkAppId)
|
||||||
|
tencnetSmsConfig.CodeSignName = g.Cfg().MustGet(ctx, "game.tencentSms.codeSignName").String() // 已审核通过的短信签名内容
|
||||||
|
tencnetSmsConfig.TemplateCode = g.Cfg().MustGet(ctx, "game.tencentSms.templateCodePass").String() // 已审核通过的模板ID
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
func (a *tencentSmsServiceTmpl) Main(args []string) (_err error) { |
||||||
|
// 初始化认证对象
|
||||||
|
credential := common.NewCredential(tencnetSmsConfig.AccessKeyId, tencnetSmsConfig.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(tencnetSmsConfig.AppId) |
||||||
|
request.SignName = common.StringPtr(tencnetSmsConfig.CodeSignName) |
||||||
|
request.TemplateId = common.StringPtr(tencnetSmsConfig.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 |
||||||
|
} |
Loading…
Reference in new issue