修改 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

@ -2,6 +2,7 @@ package upload
import (
"context"
"fmt"
"server/utility/ecode"
"server/utility/oss"
"strings"
@ -38,69 +39,7 @@ func (s *sUpload) Upload(ctx context.Context, in *model.UploadIn) (res *model.Up
// 获取图片后缀
exit := strings.Split(fileType, "/")[1]
// 文件名称
fileName := strings.ToLower("image/" + grand.Letters(32) + "." + exit)
// 开始上传图片
client, ok := oss.GetClient("aliyun")
if !ok {
return nil, ecode.Fail.Sub("未配置OSS")
}
uploadRes, err := client.UploadFile(ctx, &model.OssUploadFileInput{
Filename: fileName,
File: in.File,
})
if err != nil {
return nil, err
}
return &model.UploadOut{
Url: uploadRes.Url,
}, nil
}
func (s *sUpload) UploadGameImg(ctx context.Context, in *model.UploadIn) (res *model.UploadOut, err error) {
// 判断文件格式
fileType := in.File.Header.Get("Content-Type")
if !strings.HasPrefix(fileType, "image/") {
return nil, gerror.New("图片格式错误")
}
//判断图片大小
if in.File.Size > 10*1024*1024 {
return nil, gerror.New("图片大小最多10M")
}
// 获取图片后缀
exit := strings.Split(fileType, "/")[1]
// 文件名称
fileName := strings.ToLower("gameImg/" + grand.Letters(32) + "." + exit)
// 开始上传图片
client, ok := oss.GetClient("aliyun")
if !ok {
return nil, ecode.Fail.Sub("未配置OSS")
}
uploadRes, err := client.UploadFile(ctx, &model.OssUploadFileInput{
Filename: fileName,
File: in.File,
})
if err != nil {
return nil, err
}
return &model.UploadOut{
Url: uploadRes.Url,
}, nil
}
func (s *sUpload) UploadUserImg(ctx context.Context, in *model.UploadIn) (res *model.UploadOut, err error) {
// 判断文件格式
fileType := in.File.Header.Get("Content-Type")
if !strings.HasPrefix(fileType, "image/") {
return nil, gerror.New("图片格式错误")
}
//判断图片大小
if in.File.Size > 10*1024*1024 {
return nil, gerror.New("图片大小最多10M")
}
// 获取图片后缀
exit := strings.Split(fileType, "/")[1]
// 文件名称
fileName := strings.ToLower("userImg/" + grand.Letters(32) + "." + exit)
fileName := strings.ToLower(fmt.Sprintf("%s/%s.%s", in.Type, grand.Letters(32), exit))
// 开始上传图片
client, ok := oss.GetClient("aliyun")
if !ok {

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("获取失败")
}