mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
sms test
This commit is contained in:
parent
e5ccc1dd36
commit
832871d49d
60
internal/demo/register/ali_sms.go
Normal file
60
internal/demo/register/ali_sms.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package register
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/common/config"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
||||||
|
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
|
||||||
|
"github.com/alibabacloud-go/tea/tea"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AliSMS struct {
|
||||||
|
client *dysmsapi20170525.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a AliSMS) SendSms(code int, phoneNumber string) (resp interface{}, err error) {
|
||||||
|
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||||||
|
PhoneNumbers: tea.String(phoneNumber),
|
||||||
|
SignName: tea.String(config.Config.Demo.AliSMSVerify.SignName),
|
||||||
|
TemplateCode: tea.String(config.Config.Demo.AliSMSVerify.VerificationCodeTemplateCode),
|
||||||
|
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%d\"}", code)),
|
||||||
|
}
|
||||||
|
response, err := a.client.SendSms(sendSmsRequest)
|
||||||
|
if err != nil {
|
||||||
|
//log.NewError(params.OperationID, "sendSms error", account, "err", err.Error())
|
||||||
|
//c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
if *response.Body.Code != "OK" {
|
||||||
|
//log.NewError(params.OperationID, "alibabacloud sendSms error", account, "err", response.Body.Code, response.Body.Message)
|
||||||
|
//c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
|
||||||
|
//return
|
||||||
|
return resp, errors.New("alibabacloud sendSms error")
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAliSMS() (*AliSMS, error) {
|
||||||
|
var a AliSMS
|
||||||
|
client, err := createClient(tea.String(config.Config.Demo.AliSMSVerify.AccessKeyID), tea.String(config.Config.Demo.AliSMSVerify.AccessKeySecret))
|
||||||
|
if err != nil {
|
||||||
|
return &a, err
|
||||||
|
}
|
||||||
|
a.client = client
|
||||||
|
return &a, nil
|
||||||
|
}
|
||||||
|
func createClient(accessKeyId *string, accessKeySecret *string) (result *dysmsapi20170525.Client, err error) {
|
||||||
|
c := &openapi.Config{
|
||||||
|
// 您的AccessKey ID
|
||||||
|
AccessKeyId: accessKeyId,
|
||||||
|
// 您的AccessKey Secret
|
||||||
|
AccessKeySecret: accessKeySecret,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 访问的域名
|
||||||
|
c.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
||||||
|
result = &dysmsapi20170525.Client{}
|
||||||
|
result, err = dysmsapi20170525.NewClient(c)
|
||||||
|
return result, err
|
||||||
|
}
|
@ -6,22 +6,31 @@ import (
|
|||||||
"Open_IM/pkg/common/db"
|
"Open_IM/pkg/common/db"
|
||||||
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||||
"Open_IM/pkg/common/log"
|
"Open_IM/pkg/common/log"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"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/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"
|
"gopkg.in/gomail.v2"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var sms SMS
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
if config.Config.Demo.TencentSMS.Enable {
|
||||||
|
sms, err = NewAliSMS()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sms, err = NewTencentSMS()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type paramsVerificationCode struct {
|
type paramsVerificationCode struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
PhoneNumber string `json:"phoneNumber"`
|
PhoneNumber string `json:"phoneNumber"`
|
||||||
@ -88,28 +97,23 @@ func SendVerificationCode(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
client, err := CreateClient(tea.String(config.Config.Demo.AliSMSVerify.AccessKeyID), tea.String(config.Config.Demo.AliSMSVerify.AccessKeySecret))
|
//client, err := CreateClient(tea.String(config.Config.Demo.AliSMSVerify.AccessKeyID), tea.String(config.Config.Demo.AliSMSVerify.AccessKeySecret))
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
log.NewError(params.OperationID, "create sendSms client err", "err", err.Error())
|
// log.NewError(params.OperationID, "create sendSms client err", "err", err.Error())
|
||||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
|
// c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
|
||||||
return
|
// return
|
||||||
}
|
//}
|
||||||
|
|
||||||
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
//sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||||||
PhoneNumbers: tea.String(accountKey),
|
// PhoneNumbers: tea.String(accountKey),
|
||||||
SignName: tea.String(config.Config.Demo.AliSMSVerify.SignName),
|
// SignName: tea.String(config.Config.Demo.AliSMSVerify.SignName),
|
||||||
TemplateCode: tea.String(config.Config.Demo.AliSMSVerify.VerificationCodeTemplateCode),
|
// TemplateCode: tea.String(config.Config.Demo.AliSMSVerify.VerificationCodeTemplateCode),
|
||||||
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%d\"}", code)),
|
// TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%d\"}", code)),
|
||||||
}
|
//}
|
||||||
|
response, err := sms.SendSms(code, accountKey)
|
||||||
response, err := client.SendSms(sendSmsRequest)
|
//response, err := client.SendSms(sendSmsRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(params.OperationID, "sendSms error", account, "err", err.Error())
|
log.NewError(params.OperationID, "sendSms error", account, "err", err.Error(), response)
|
||||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if *response.Body.Code != "OK" {
|
|
||||||
log.NewError(params.OperationID, "alibabacloud sendSms error", account, "err", response.Body.Code, response.Body.Message)
|
|
||||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
|
c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -120,48 +124,48 @@ func SendVerificationCode(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verification code has been set!", "data": data})
|
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verification code has been set!", "data": data})
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsapi20170525.Client, err error) {
|
//func CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsapi20170525.Client, err error) {
|
||||||
c := &openapi.Config{
|
// c := &openapi.Config{
|
||||||
// 您的AccessKey ID
|
// // 您的AccessKey ID
|
||||||
AccessKeyId: accessKeyId,
|
// AccessKeyId: accessKeyId,
|
||||||
// 您的AccessKey Secret
|
// // 您的AccessKey Secret
|
||||||
AccessKeySecret: accessKeySecret,
|
// AccessKeySecret: accessKeySecret,
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 访问的域名
|
// // 访问的域名
|
||||||
c.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
// c.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
||||||
result = &dysmsapi20170525.Client{}
|
// result = &dysmsapi20170525.Client{}
|
||||||
result, err = dysmsapi20170525.NewClient(c)
|
// result, err = dysmsapi20170525.NewClient(c)
|
||||||
return result, err
|
// return result, err
|
||||||
}
|
//}
|
||||||
func CreateTencentSMSClient() (string, error) {
|
//func CreateTencentSMSClient() (string, error) {
|
||||||
credential := common.NewCredential(
|
// credential := common.NewCredential(
|
||||||
config.Config.Demo.TencentSMS.SecretID,
|
// config.Config.Demo.TencentSMS.SecretID,
|
||||||
config.Config.Demo.TencentSMS.SecretKey,
|
// config.Config.Demo.TencentSMS.SecretKey,
|
||||||
)
|
// )
|
||||||
cpf := profile.NewClientProfile()
|
// cpf := profile.NewClientProfile()
|
||||||
client, err := sms.NewClient(credential, config.Config.Demo.TencentSMS.Region, cpf)
|
// client, err := sms.NewClient(credential, config.Config.Demo.TencentSMS.Region, cpf)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return "", err
|
// return "", err
|
||||||
}
|
// }
|
||||||
request := sms.NewSendSmsRequest()
|
// request := sms.NewSendSmsRequest()
|
||||||
request.SmsSdkAppId = common.StringPtr(config.Config.Demo.TencentSMS.AppID)
|
// request.SmsSdkAppId = common.StringPtr(config.Config.Demo.TencentSMS.AppID)
|
||||||
request.SignName = common.StringPtr(config.Config.Demo.TencentSMS.SignName)
|
// request.SignName = common.StringPtr(config.Config.Demo.TencentSMS.SignName)
|
||||||
request.TemplateId = common.StringPtr(config.Config.Demo.TencentSMS.VerificationCodeTemplateCode)
|
// request.TemplateId = common.StringPtr(config.Config.Demo.TencentSMS.VerificationCodeTemplateCode)
|
||||||
request.TemplateParamSet = common.StringPtrs([]string{"666666"})
|
// request.TemplateParamSet = common.StringPtrs([]string{"666666"})
|
||||||
request.PhoneNumberSet = common.StringPtrs([]string{"+971588232183"})
|
// request.PhoneNumberSet = common.StringPtrs([]string{"+971588232183"})
|
||||||
// 通过client对象调用想要访问的接口,需要传入请求对象
|
// // 通过client对象调用想要访问的接口,需要传入请求对象
|
||||||
response, err := client.SendSms(request)
|
// response, err := client.SendSms(request)
|
||||||
// 非SDK异常,直接失败。实际代码中可以加入其他的处理。
|
// // 非SDK异常,直接失败。实际代码中可以加入其他的处理。
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Error("test", "send code to tencent err", err.Error())
|
// log.Error("test", "send code to tencent err", err.Error())
|
||||||
}
|
// }
|
||||||
// 处理异常
|
// // 处理异常
|
||||||
if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
// if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
||||||
log.Error("test", "An API error has returned:", err.Error())
|
// log.Error("test", "An API error has returned:", err.Error())
|
||||||
return "", err
|
// return "", err
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
b, _ := json.Marshal(response.Response)
|
// b, _ := json.Marshal(response.Response)
|
||||||
return string(b), nil
|
// return string(b), nil
|
||||||
}
|
//}
|
||||||
|
5
internal/demo/register/sms_interface.go
Normal file
5
internal/demo/register/sms_interface.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package register
|
||||||
|
|
||||||
|
type SMS interface {
|
||||||
|
SendSms(code int, phoneNumber string) (resp interface{}, err error)
|
||||||
|
}
|
51
internal/demo/register/tencent_sms.go
Normal file
51
internal/demo/register/tencent_sms.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package register
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/common/config"
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
|
"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"
|
||||||
|
v20210111 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TencentSMS struct {
|
||||||
|
client *v20210111.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TencentSMS) SendSms(code int, phoneNumber string) (resp interface{}, err error) {
|
||||||
|
request := v20210111.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{utils.IntToString(code)})
|
||||||
|
request.PhoneNumberSet = common.StringPtrs([]string{phoneNumber})
|
||||||
|
//+971588232183
|
||||||
|
// 通过client对象调用想要访问的接口,需要传入请求对象
|
||||||
|
response, err := t.client.SendSms(request)
|
||||||
|
// 非SDK异常,直接失败。实际代码中可以加入其他的处理。
|
||||||
|
if err != nil {
|
||||||
|
return response, err
|
||||||
|
}
|
||||||
|
// 处理异常
|
||||||
|
if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
||||||
|
return response, err
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTencentSMS() (*TencentSMS, error) {
|
||||||
|
var a TencentSMS
|
||||||
|
credential := common.NewCredential(
|
||||||
|
config.Config.Demo.TencentSMS.SecretID,
|
||||||
|
config.Config.Demo.TencentSMS.SecretKey,
|
||||||
|
)
|
||||||
|
cpf := profile.NewClientProfile()
|
||||||
|
client, err := v20210111.NewClient(credential, config.Config.Demo.TencentSMS.Region, cpf)
|
||||||
|
if err != nil {
|
||||||
|
return &a, err
|
||||||
|
}
|
||||||
|
a.client = client
|
||||||
|
return &a, nil
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user