2021-12-01 12:16:02 +08:00
package register
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/log"
2022-07-01 15:07:22 +08:00
"Open_IM/pkg/common/utils"
2021-12-01 12:16:02 +08:00
"fmt"
"github.com/gin-gonic/gin"
"gopkg.in/gomail.v2"
"math/rand"
"net/http"
2022-07-01 15:07:22 +08:00
2021-12-01 12:16:02 +08:00
"time"
)
2022-06-30 16:27:39 +08:00
var sms SMS
func init ( ) {
var err error
2022-06-30 16:42:27 +08:00
if config . Config . Demo . AliSMSVerify . Enable {
2022-06-30 16:27:39 +08:00
sms , err = NewAliSMS ( )
if err != nil {
panic ( err )
}
} else {
sms , err = NewTencentSMS ( )
if err != nil {
panic ( err )
}
}
}
2021-12-01 12:16:02 +08:00
type paramsVerificationCode struct {
Email string ` json:"email" `
PhoneNumber string ` json:"phoneNumber" `
2022-02-11 16:25:28 +08:00
OperationID string ` json:"operationID" binding:"required" `
2022-05-10 10:52:32 +08:00
UsedFor int ` json:"usedFor" `
2022-06-24 17:45:33 +08:00
AreaCode string ` json:"areaCode" `
2021-12-01 12:16:02 +08:00
}
func SendVerificationCode ( c * gin . Context ) {
params := paramsVerificationCode { }
2022-07-01 15:07:22 +08:00
2021-12-01 12:16:02 +08:00
if err := c . BindJSON ( & params ) ; err != nil {
2022-02-11 16:25:28 +08:00
log . NewError ( "" , "BindJSON failed" , "err:" , err . Error ( ) , "phoneNumber" , params . PhoneNumber , "email" , params . Email )
2021-12-01 12:16:02 +08:00
c . JSON ( http . StatusBadRequest , gin . H { "errCode" : constant . FormattingError , "errMsg" : err . Error ( ) } )
return
}
2022-07-01 15:07:22 +08:00
operationID := params . OperationID
if operationID == "" {
operationID = utils . OperationIDGenerator ( )
}
2022-07-01 15:11:33 +08:00
log . Info ( operationID , "SendVerificationCode args: " , "area code: " , params . AreaCode , "Phone Number: " , params . PhoneNumber )
2021-12-01 12:16:02 +08:00
var account string
if params . Email != "" {
account = params . Email
} else {
account = params . PhoneNumber
}
2022-06-24 17:45:33 +08:00
var accountKey = params . AreaCode + account
2022-02-21 19:12:51 +08:00
if params . UsedFor == 0 {
params . UsedFor = constant . VerificationCodeForRegister
}
2022-02-21 16:24:25 +08:00
switch params . UsedFor {
case constant . VerificationCodeForRegister :
2022-07-05 11:01:10 +08:00
_ , err := im_mysql_model . GetRegister ( account , params . AreaCode , "" )
2022-02-21 16:24:25 +08:00
if err == nil {
log . NewError ( params . OperationID , "The phone number has been registered" , params )
2022-02-21 18:50:11 +08:00
c . JSON ( http . StatusOK , gin . H { "errCode" : constant . HasRegistered , "errMsg" : "The phone number has been registered" } )
2022-02-21 16:24:25 +08:00
return
}
2022-06-30 19:19:00 +08:00
accountKey = accountKey + "_" + constant . VerificationCodeForRegisterSuffix
2022-06-24 17:45:33 +08:00
ok , err := db . DB . JudgeAccountEXISTS ( accountKey )
2022-02-21 16:24:25 +08:00
if ok || err != nil {
2022-07-01 17:23:43 +08:00
log . NewError ( params . OperationID , "Repeat send code" , params , accountKey )
2022-06-30 19:19:00 +08:00
c . JSON ( http . StatusOK , gin . H { "errCode" : constant . RepeatSendCode , "errMsg" : "Repeat send code" } )
2022-02-21 16:24:25 +08:00
return
}
case constant . VerificationCodeForReset :
2022-06-30 19:19:00 +08:00
accountKey = accountKey + "_" + constant . VerificationCodeForResetSuffix
2022-07-01 17:23:43 +08:00
ok , err := db . DB . JudgeAccountEXISTS ( accountKey )
if ok || err != nil {
log . NewError ( params . OperationID , "Repeat send code" , params , accountKey )
c . JSON ( http . StatusOK , gin . H { "errCode" : constant . RepeatSendCode , "errMsg" : "Repeat send code" } )
return
}
2021-12-01 12:16:02 +08:00
}
rand . Seed ( time . Now ( ) . UnixNano ( ) )
code := 100000 + rand . Intn ( 900000 )
2022-05-10 10:52:32 +08:00
log . NewInfo ( params . OperationID , params . UsedFor , "begin store redis" , accountKey , code )
2022-02-21 16:24:25 +08:00
err := db . DB . SetAccountCode ( accountKey , code , config . Config . Demo . CodeTTL )
2022-02-11 16:25:28 +08:00
if err != nil {
2022-02-21 16:24:25 +08:00
log . NewError ( params . OperationID , "set redis error" , accountKey , "err" , err . Error ( ) )
2022-02-11 16:25:28 +08:00
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
}
2022-06-30 19:19:00 +08:00
log . NewDebug ( params . OperationID , config . Config . Demo )
2021-12-01 12:16:02 +08:00
if params . Email != "" {
m := gomail . NewMessage ( )
m . SetHeader ( ` From ` , config . Config . Demo . Mail . SenderMail )
m . SetHeader ( ` To ` , [ ] string { account } ... )
m . SetHeader ( ` Subject ` , config . Config . Demo . Mail . Title )
m . SetBody ( ` text/html ` , fmt . Sprintf ( "%d" , code ) )
if err := gomail . NewDialer ( config . Config . Demo . Mail . SmtpAddr , config . Config . Demo . Mail . SmtpPort , config . Config . Demo . Mail . SenderMail , config . Config . Demo . Mail . SenderAuthorizationCode ) . DialAndSend ( m ) ; err != nil {
2022-05-10 10:52:32 +08:00
log . Error ( params . OperationID , "send mail error" , account , err . Error ( ) )
2022-02-11 17:44:02 +08:00
c . JSON ( http . StatusOK , gin . H { "errCode" : constant . MailSendCodeErr , "errMsg" : "" } )
2021-12-01 12:16:02 +08:00
return
}
} else {
2022-06-30 16:27:39 +08:00
//client, err := CreateClient(tea.String(config.Config.Demo.AliSMSVerify.AccessKeyID), tea.String(config.Config.Demo.AliSMSVerify.AccessKeySecret))
//if err != nil {
// 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"})
// return
//}
2021-12-01 12:16:02 +08:00
2022-06-30 16:27:39 +08:00
//sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
// PhoneNumbers: tea.String(accountKey),
// SignName: tea.String(config.Config.Demo.AliSMSVerify.SignName),
// TemplateCode: tea.String(config.Config.Demo.AliSMSVerify.VerificationCodeTemplateCode),
// TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%d\"}", code)),
//}
2022-06-30 19:11:44 +08:00
response , err := sms . SendSms ( code , params . AreaCode + params . PhoneNumber )
2022-06-30 16:27:39 +08:00
//response, err := client.SendSms(sendSmsRequest)
2021-12-01 12:16:02 +08:00
if err != nil {
2022-06-30 16:27:39 +08:00
log . NewError ( params . OperationID , "sendSms error" , account , "err" , err . Error ( ) , response )
2022-02-11 16:25:28 +08:00
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" } )
2021-12-01 12:16:02 +08:00
return
}
}
2022-06-30 18:42:30 +08:00
log . Debug ( params . OperationID , "send sms success" , code , accountKey )
2022-02-11 16:25:28 +08:00
data := make ( map [ string ] interface { } )
data [ "account" ] = account
c . JSON ( http . StatusOK , gin . H { "errCode" : constant . NoError , "errMsg" : "Verification code has been set!" , "data" : data } )
2021-12-01 12:16:02 +08:00
}
2022-06-30 16:27:39 +08:00
//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
//}
//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 {
// return "", err
// }
// 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{"666666"})
// request.PhoneNumberSet = common.StringPtrs([]string{"+971588232183"})
// // 通过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
//}