diff --git a/config/config.yaml b/config/config.yaml index d30e21afa..d604191aa 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -689,11 +689,20 @@ demo: listenIP: 0.0.0.0 #demo对外服务端口,默认即可,需要开放此端口或做nginx转发 openImDemoPort: [ 10004 ] - alismsverify: #阿里云短信配置,在阿里云申请成功后修改以下四项,必须修改 + alismsverify: #阿里云短信配置,在阿里云申请成功后修改以下四项,enable为true则必须修改,阿里云为默认短信验证方式 accessKeyId: LTAI5tJPkn4HuuePdiLdGqe7 accessKeySecret: 4n9OJ7ZCVN1U6KeHDAtOyNeVZcjOuV signName: 托云信息技术 verificationCodeTemplateCode: SMS_226810164 + enable: true + tencentsms: #腾讯云短信配置,在腾讯云申请成功后,修改以下选项,enable为true则必须修改 + appID: 1400529182 + region: ap-chengdu + secretID: AKIDGNYVChzIQinu7QEgtNp0hnNgqcV8vZTC1 + secretKey: kz15vW83qM6dBUWIq681eBZA0c0vlIbe1 + signName: "" + verificationCodeTemplateCode: 1449250 + enable: true superCode: 666666 #超级验证码,建议修改掉,收不到短信验证码时可以用此替代 # second codeTTL: 300 diff --git a/internal/demo/register/CreateTencentSMSClient_test.go b/internal/demo/register/CreateTencentSMSClient_test.go new file mode 100644 index 000000000..7c3a06fd7 --- /dev/null +++ b/internal/demo/register/CreateTencentSMSClient_test.go @@ -0,0 +1,14 @@ +package register + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_CreateTencentSMSClient(t *testing.T) { + result, err := CreateTencentSMSClient() + assert.Nil(t, err) + fmt.Println("return result is ", result) + +} diff --git a/internal/demo/register/send_code.go b/internal/demo/register/send_code.go index 089a55800..fc24e9bdd 100644 --- a/internal/demo/register/send_code.go +++ b/internal/demo/register/send_code.go @@ -6,11 +6,16 @@ import ( "Open_IM/pkg/common/db" "Open_IM/pkg/common/db/mysql_model/im_mysql_model" "Open_IM/pkg/common/log" + "encoding/json" "fmt" openapi "github.com/alibabacloud-go/darabonba-openapi/client" dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client" "github.com/alibabacloud-go/tea/tea" "github.com/gin-gonic/gin" + "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" "gopkg.in/gomail.v2" "math/rand" "net/http" @@ -128,3 +133,34 @@ func CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsap result, err = dysmsapi20170525.NewClient(c) return result, err } +func CreateTencentSMSClient() (string, error) { + credential := common.NewCredential( + config.Config.Demo.TencentSMS.SecretID, + config.Config.Demo.TencentSMS.SecretKey, + ) + cpf := profile.NewClientProfile() + client, err := sms.NewClient(credential, config.Config.Demo.TencentSMS.Region, cpf) + if err != nil { + + } + request := sms.NewSendSmsRequest() + request.SmsSdkAppId = common.StringPtr(config.Config.Demo.TencentSMS.AppID) + request.SignName = common.StringPtr(config.Config.Demo.TencentSMS.SignName) + request.TemplateId = common.StringPtr(config.Config.Demo.TencentSMS.VerificationCodeTemplateCode) + request.TemplateParamSet = common.StringPtrs([]string{"1234"}) + request.PhoneNumberSet = common.StringPtrs([]string{"+8613711112222"}) + // 通过client对象调用想要访问的接口,需要传入请求对象 + response, err := client.SendSms(request) + // 非SDK异常,直接失败。实际代码中可以加入其他的处理。 + if err != nil { + log.Error("test", "send code to tencent err", err.Error()) + } + // 处理异常 + if _, ok := err.(*errors.TencentCloudSDKError); ok { + log.Error("test", "An API error has returned:", err.Error()) + return "", err + } + + b, _ := json.Marshal(response.Response) + return string(b), nil +} diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 9f6b4ba18..4fb8a9276 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -452,6 +452,16 @@ type config struct { AccessKeySecret string `yaml:"accessKeySecret"` SignName string `yaml:"signName"` VerificationCodeTemplateCode string `yaml:"verificationCodeTemplateCode"` + Enable bool `yaml:"enable"` + } + TencentSMS struct { + AppID string `yaml:"appID"` + Region string `yaml:"region"` + SecretID string `yaml:"secretID"` + SecretKey string `yaml:"secretKey"` + SignName string `yaml:"signName"` + VerificationCodeTemplateCode string `yaml:"verificationCodeTemplateCode"` + Enable bool `yaml:"enable"` } SuperCode string `yaml:"superCode"` CodeTTL int `yaml:"codeTTL"`