验证码登录

This commit is contained in:
hahwu 2025-06-18 15:25:50 +08:00
parent 64531f94f4
commit c6b40c145c
9 changed files with 2031 additions and 1983 deletions

View File

@ -12,6 +12,11 @@ import (
credential "github.com/aliyun/credentials-go/credentials"
)
const (
ACCESS_KEY_ID = "LTAI5t8DKMexEjcBH1cCmrp9"
ACCESS_KEY_SECRET = "p0KqKvTarTRXBXcBbUvfqSO9bynrCD"
)
type ClientInterface interface {
}
@ -100,8 +105,8 @@ func CreateClient(endpoint *string) (_result *sdk.Client) {
// 获取Credential工具此工具会从环境变量中读取AccessKey。
credentialConfig := &credential.Config{
Type: tea.String("access_key"),
AccessKeyId: tea.String("LTAI5t8DKMexEjcBH1cCmrp9"),
AccessKeySecret: tea.String("p0KqKvTarTRXBXcBbUvfqSO9bynrCD"),
AccessKeyId: tea.String(ACCESS_KEY_ID),
AccessKeySecret: tea.String(ACCESS_KEY_SECRET),
}
credential, _err := credential.NewCredential(credentialConfig)
if _err != nil {

View File

@ -0,0 +1,68 @@
// This file is auto-generated, don't edit it. Thanks.
package GoUtil
import (
"fmt"
"server/pkg/github.com/name5566/leaf/log"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client"
console "github.com/alibabacloud-go/tea-console/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
credential "github.com/aliyun/credentials-go/credentials"
)
const (
SIGN_NAME = "阿里云短信测试"
TEMPLATE_CODE = "SMS_154950909"
)
// Description:
//
// 使用凭据初始化账号Client
//
// @return Client
//
// @throws Exception
func CreateSmsClient() (_result *dysmsapi20170525.Client, _err error) {
// 工程代码建议使用更安全的无AK方式凭据配置方式请参见https://help.aliyun.com/document_detail/378661.html。
credential, _err := credential.NewCredential(&credential.Config{
Type: tea.String("access_key"),
AccessKeyId: tea.String(ACCESS_KEY_ID),
AccessKeySecret: tea.String(ACCESS_KEY_SECRET),
})
if _err != nil {
return _result, _err
}
config := &openapi.Config{
Credential: credential,
}
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
_result = &dysmsapi20170525.Client{}
_result, _err = dysmsapi20170525.NewClient(config)
return _result, _err
}
func SmsCode(Phone, Code string) (_err error) {
client, _err := CreateSmsClient()
if _err != nil {
return _err
}
console.Log(tea.String("生成的验证码:" + Code))
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(Phone),
SignName: tea.String(SIGN_NAME),
TemplateCode: tea.String(TEMPLATE_CODE),
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", Code)),
}
runtime := &util.RuntimeOptions{}
resp, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
log.Debug("SendSmsWithOptions resp: %v", resp)
if _err != nil {
return _err
}
return nil
}

View File

@ -295,3 +295,8 @@ func UniKey(seed string) string {
func GetUserKey(Uid int64) string {
return fmt.Sprintf("user_data_%d", Uid)
}
func Rand6DigitNumber() string {
n := rand.Intn(1000000)
return fmt.Sprintf("%06d", n)
}

View File

@ -922,3 +922,32 @@ func Destroy() {
G_GameLogicPtr.MLogManager.Close()
}
}
func GeneratedCode(Phone string) error {
Code := GoUtil.Rand6DigitNumber()
log.Debug("生成验证码: %s", Code)
key := "Code_" + Phone
value, _ := db.RedisGetKey(key)
if value != "" {
//return fmt.Errorf("验证码已发送,请稍后再试")
}
db.RedisSetKey(key, Code, time.Minute)
err := GoUtil.SmsCode(Phone, Code)
if err != nil {
log.Error("发送验证码失败: %v", err)
}
return err
}
func VerifyCode(Phone, Code string) error {
key := "Code_" + Phone
value, err := db.RedisGetKey(key)
if err != nil {
return fmt.Errorf("获取验证码失败: %v", err)
}
if value == Code {
db.RedisDelKey(key)
return nil
}
return fmt.Errorf("获取验证码失败: 不一致")
}

View File

@ -39,18 +39,6 @@ func AdminProcess(Func string, args []interface{}) {
log.Debug("AdminProcess error: %v", "Func not found")
}
func LoginCodeProcess(Detail *msg.ReqLoginCode) {
// 处理登录验证码请求
}
func ReqIdentifyCard(a gate.Agent, Detail *msg.ReqIdentifyCard) {
// 处理身份证请求
res := &msg.ResIdentifyCard{}
res.ResultCode = 0
data, _ := proto.Marshal(res)
G_getGameLogic().PackResInfo(a, "ResIdentifyCard", data)
}
func VerifyUser(accountInfo *db.Db_Account, detail *msg.ReqLogin) (ResLogin *msg.ResLogin) {
if accountInfo == nil {
ResLogin = &msg.ResLogin{
@ -69,7 +57,7 @@ func VerifyUser(accountInfo *db.Db_Account, detail *msg.ReqLogin) (ResLogin *msg
}
if detail.Type == msg.LOGIN_TYPE_CODE_LOGIN {
err := VerifyUserCode(detail.UserName, detail.Code)
err := VerifyCode(detail.UserName, detail.Code)
if err != nil {
ResLogin = &msg.ResLogin{
ResultCode: MergeConst.Protocol_Error_Account_Code_Error,
@ -105,10 +93,6 @@ func VerifyUser(accountInfo *db.Db_Account, detail *msg.ReqLogin) (ResLogin *msg
return
}
func VerifyUserCode(telphone string, code string) error {
return nil
}
func AdminPlayerInfo(args []interface{}) error {
a, buf := ParseAdminArgs(args)
req := &msg.ReqAdminInfo{}

View File

@ -105,12 +105,14 @@ func HandleClientReq(args []interface{}) {
case "ReqLoginCode":
Detail := &msg.ReqLoginCode{}
proto.Unmarshal(buf, Detail)
LoginCodeProcess(Detail)
case "ReqIdentifyCard":
detail := &msg.ReqIdentifyCard{}
proto.Unmarshal(buf, detail)
ReqIdentifyCard(a, detail)
err := GeneratedCode(Detail.TelPhone)
ResLoginCode := &msg.ResLoginCode{}
if err != nil {
ResLoginCode.ResultCode = MergeConst.Protocol_Error_Account_Code_Error
ResLoginCode.Msg = err.Error()
}
data, _ := proto.Marshal(ResLoginCode)
G_GameLogicPtr.PackResInfo(a, "ResLoginCode", data)
case "ReqServerVersion":
G_GameLogicPtr.SendServerVersion(a)
case "ReqRegisterAccount":

View File

@ -5,6 +5,7 @@ go 1.23
require (
github.com/alibabacloud-go/cloudauth-20190307/v4 v4.9.2
github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.11
github.com/alibabacloud-go/dysmsapi-20170525/v5 v5.1.1
github.com/alibabacloud-go/tea v1.3.8
github.com/alibabacloud-go/tea-console v1.0.0
github.com/alibabacloud-go/tea-utils/v2 v2.0.7

View File

@ -28,6 +28,8 @@ github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68/go.mod h1:6p
github.com/alibabacloud-go/debug v1.0.0/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc=
github.com/alibabacloud-go/debug v1.0.1 h1:MsW9SmUtbb1Fnt3ieC6NNZi6aEwrXfDksD4QA6GSbPg=
github.com/alibabacloud-go/debug v1.0.1/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc=
github.com/alibabacloud-go/dysmsapi-20170525/v5 v5.1.1 h1:Kle0H03Z85fDLHnO3dadhSGMzOnEOcUbHCjbDjvY9lc=
github.com/alibabacloud-go/dysmsapi-20170525/v5 v5.1.1/go.mod h1:mYOaEwXaib4RLB2NY8cXFjKbxPQHUqt6lhPEOvqR8aw=
github.com/alibabacloud-go/endpoint-util v1.1.0 h1:r/4D3VSw888XGaeNpP994zDUaxdgTSHBbVfZlzf6b5Q=
github.com/alibabacloud-go/endpoint-util v1.1.0/go.mod h1:O5FuCALmCKs2Ff7JFJMudHs0I5EBgecXXxZRyswlEjE=
github.com/alibabacloud-go/openapi-util v0.0.11/go.mod h1:sQuElr4ywwFRlCCberQwKRFhRzIyG4QTP/P4y1CJ6Ws=

File diff suppressed because it is too large Load Diff