修改 aliyun,短信,oss,验证码

This commit is contained in:
chy
2025-06-24 16:22:18 +08:00
parent b02ad91118
commit 8342c92247
21 changed files with 219 additions and 128 deletions

View File

@ -17,6 +17,7 @@ import (
"server/utility/encrypt"
"server/utility/gamelife"
"server/utility/jwt"
"server/utility/sms"
)
type sUser struct{}
@ -163,16 +164,29 @@ func (s *sUser) Info(ctx context.Context, in *model.UserInfoIn) (out *model.User
}
func (s *sUser) Code(ctx context.Context, in *model.GetPhoneCodeIn) (out *model.GetPhoneCodeOut, err error) {
// TODO 短信平台获取验证码
code := "123456"
code := grand.Digits(6)
// 存入 redis
err = g.Redis().SetEX(ctx, fmt.Sprintf(consts.UserBindPhoneKey, in.Id), code, consts.UserCodeExpire)
err = g.Redis().SetEX(ctx, fmt.Sprintf(consts.UserBindPhoneKey, in.Phone), code, consts.UserCodeExpire)
if err != nil {
return nil, ecode.Fail.Sub("设置验证码失败")
}
client, ok := sms.GetClient("aliyunsms")
if !ok {
return nil, ecode.Fail.Sub("未配置短信平台")
}
_, err = client.SendCode(ctx, &model.SMSCodeIn{
Code: code,
Phone: in.Phone,
})
if err != nil {
return nil, ecode.Fail.Sub("发送验证码失败")
}
return &model.GetPhoneCodeOut{
Success: true,
}, nil
@ -203,7 +217,7 @@ func (s *sUser) Update(ctx context.Context, in *model.UserUpdateIn) (out *model.
func (s *sUser) BindPhone(ctx context.Context, in *model.UserBindPhoneIn) (out *model.UserBindPhoneOut, err error) {
// 绑定手机号,需要验证入参和缓存中的验证码时候相同
value, err := g.Redis().Get(ctx, fmt.Sprintf(consts.UserBindPhoneKey, in.Id))
value, err := g.Redis().Get(ctx, fmt.Sprintf(consts.UserBindPhoneKey, in.Phone))
if err != nil {
return nil, ecode.Fail.Sub("获取失败")
}