🎨 updated error handling logic

This commit is contained in:
landaiqing
2024-11-18 01:12:50 +08:00
parent 78a162a19a
commit 35d2da35b2
48 changed files with 395 additions and 170 deletions

View File

@@ -28,7 +28,7 @@ func NewGenerateRotateCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceConte
func (l *GenerateRotateCaptchaLogic) GenerateRotateCaptcha() (resp *types.Response, err error) {
captcha, err := generate.GenerateRotateCaptcha(l.svcCtx.RotateCaptcha, l.svcCtx.RedisClient, l.ctx)
if err != nil {
return response.Error(), err
return nil, err
}
return response.SuccessWithData(captcha), nil
}

View File

@@ -28,7 +28,7 @@ func NewGenerateSlideBasicCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceC
func (l *GenerateSlideBasicCaptchaLogic) GenerateSlideBasicCaptcha() (resp *types.Response, err error) {
captcha, err := generate.GenerateSlideBasicCaptcha(l.svcCtx.SlideCaptcha, l.svcCtx.RedisClient, l.ctx)
if err != nil {
return response.Error(), err
return nil, err
}
return response.SuccessWithData(captcha), nil
}

View File

@@ -36,7 +36,7 @@ func (l *GenerateClientIdLogic) GenerateClientId(clientIP string) (resp *types.R
}
simpleUuid := kgo.SimpleUuid()
if err = l.svcCtx.RedisClient.SetEx(l.ctx, constant.UserClientPrefix+clientIP, simpleUuid, time.Hour*24*7).Err(); err != nil {
return response.Error(), err
return nil, err
}
return response.SuccessWithData(simpleUuid), nil
}

View File

@@ -3,6 +3,8 @@ package comment
import (
"context"
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
"schisandra-album-cloud-microservices/app/core/api/common/response"
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
"schisandra-album-cloud-microservices/app/core/api/internal/types"
@@ -24,7 +26,11 @@ func NewSubmitCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sub
}
func (l *SubmitCommentLogic) SubmitComment(req *types.CommentRequest) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
res := verify.VerifySlideCaptcha(l.ctx, l.svcCtx.RedisClient, req.Point, req.Key)
if !res {
return response.ErrorWithI18n(l.ctx, "captcha.verificationFailure"), nil
}
return
}

View File

@@ -40,7 +40,7 @@ func (l *GetWechatQrcodeLogic) GetWechatQrcode(r *http.Request, req *types.OAuth
if qrcode != "" {
data := new(response.ResponseQRCodeCreate)
if err = json.Unmarshal([]byte(qrcode), data); err != nil {
return response2.Error(), err
return nil, err
}
return response2.SuccessWithData(data.Url), nil
}
@@ -48,16 +48,16 @@ func (l *GetWechatQrcodeLogic) GetWechatQrcode(r *http.Request, req *types.OAuth
// 生成临时二维码
data, err := l.svcCtx.WechatPublic.QRCode.Temporary(l.ctx, req.Client_id, 7*24*3600)
if err != nil {
return response2.Error(), err
return nil, err
}
// 序列化数据并存储到Redis
serializedData, err := json.Marshal(data)
if err != nil {
return response2.Error(), err
return nil, err
}
if err = l.svcCtx.RedisClient.Set(l.ctx, key, serializedData, time.Hour*24*7).Err(); err != nil {
return response2.Error(), err
return nil, err
}
return response2.SuccessWithData(data.Url), nil

View File

@@ -56,7 +56,7 @@ func (l *SendSmsByAliyunLogic) SendSmsByAliyun(req *types.SmsSendRequest) (resp
code := utils.GenValidateCode(6)
wrong := l.svcCtx.RedisClient.Set(l.ctx, constant.UserSmsRedisPrefix+req.Phone, code, time.Minute).Err()
if wrong != nil {
return response.ErrorWithI18n(l.ctx, "sms.smsSendFailed"), wrong
return nil, wrong
}
_, err = sms.Send(req.Phone, gosms.MapStringAny{
"content": "您的验证码是:****。请不要把验证码泄露给其他人。",
@@ -67,7 +67,7 @@ func (l *SendSmsByAliyunLogic) SendSmsByAliyun(req *types.SmsSendRequest) (resp
},
}, nil)
if err != nil {
return response.ErrorWithI18n(l.ctx, "sms.smsSendFailed"), err
return nil, err
}
return response.Success(), nil
}

View File

@@ -54,13 +54,13 @@ func (l *SendSmsBySmsbaoLogic) SendSmsBySmsbao(req *types.SmsSendRequest) (resp
code := utils.GenValidateCode(6)
wrong := l.svcCtx.RedisClient.Set(l.ctx, constant.UserSmsRedisPrefix+req.Phone, code, time.Minute).Err()
if wrong != nil {
return response.ErrorWithI18n(l.ctx, "sms.smsSendFailed"), wrong
return nil, wrong
}
_, err = sms.Send(req.Phone, gosms.MapStringAny{
"content": "您的验证码是:" + code + "。请不要把验证码泄露给其他人。",
}, nil)
if err != nil {
return response.ErrorWithI18n(l.ctx, "sms.smsSendFailed"), err
return nil, err
}
return response.Success(), nil
}

View File

@@ -44,7 +44,7 @@ func (l *SendSmsByTestLogic) SendSmsByTest(req *types.SmsSendRequest) (resp *typ
code := utils.GenValidateCode(6)
wrong := l.svcCtx.RedisClient.Set(l.ctx, constant.UserSmsRedisPrefix+req.Phone, code, time.Minute).Err()
if wrong != nil {
return response.ErrorWithI18n(l.ctx, "sms.smsSendFailed"), wrong
return nil, wrong
}
return response.Success(), nil
}

View File

@@ -2,7 +2,6 @@ package user
import (
"context"
"encoding/json"
"errors"
"net/http"
@@ -13,7 +12,6 @@ import (
"schisandra-album-cloud-microservices/app/core/api/common/constant"
"schisandra-album-cloud-microservices/app/core/api/common/utils"
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
"schisandra-album-cloud-microservices/app/core/api/internal/types"
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent"
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent/scaauthuserdevice"
)
@@ -37,17 +35,12 @@ func (l *GetUserDeviceLogic) GetUserDevice(r *http.Request) error {
if err != nil {
return err
}
sessionData, ok := session.Values[constant.SESSION_KEY]
uid, ok := session.Values["uid"].(string)
if !ok {
return errors.New("user session not found")
}
var data types.SessionData
err = json.Unmarshal(sessionData.([]byte), &data)
if err != nil {
return err
}
res := GetUserLoginDevice(data.UID, r, l.svcCtx.Ip2Region, l.svcCtx.MySQLClient, l.ctx)
res := GetUserLoginDevice(uid, r, l.svcCtx.Ip2Region, l.svcCtx.MySQLClient, l.ctx)
if !res {
return errors.New("user device not found")
}
@@ -63,7 +56,6 @@ func GetUserLoginDevice(userId string, r *http.Request, ip2location *xdb.Searche
ip := utils.GetClientIP(r)
location, err := ip2location.SearchByStr(ip)
if err != nil {
logx.Error(err)
return false
}
location = utils.RemoveZeroAndAdjust(location)

View File

@@ -46,7 +46,7 @@ func (l *PhoneLoginLogic) PhoneLogin(r *http.Request, w http.ResponseWriter, req
user, err := l.svcCtx.MySQLClient.ScaAuthUser.Query().Where(scaauthuser.Phone(req.Phone), scaauthuser.Deleted(0)).First(l.ctx)
tx, wrong := l.svcCtx.MySQLClient.Tx(l.ctx)
if wrong != nil {
return response.ErrorWithI18n(l.ctx, "login.loginFailed"), err
return nil, err
}
if ent.IsNotFound(err) {
uid := idgen.NextId()
@@ -64,12 +64,12 @@ func (l *PhoneLoginLogic) PhoneLogin(r *http.Request, w http.ResponseWriter, req
Save(l.ctx)
if fault != nil {
err = tx.Rollback()
return response.ErrorWithI18n(l.ctx, "login.registerError"), err
return nil, err
}
_, err = l.svcCtx.CasbinEnforcer.AddRoleForUser(uidStr, constant.User)
if err != nil {
err = tx.Rollback()
return response.ErrorWithI18n(l.ctx, "login.registerError"), err
return nil, err
}
data, result := HandleUserLogin(addUser, l.svcCtx, req.AutoLogin, r, w, l.ctx)
if !result {

View File

@@ -32,7 +32,7 @@ func NewRefreshTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Refr
func (l *RefreshTokenLogic) RefreshToken(r *http.Request) (resp *types.Response, err error) {
session, err := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
if err != nil {
return response.ErrorWithCode(403), err
return nil, err
}
refreshSessionToken, ok := session.Values["refresh_token"].(string)
if !ok {
@@ -49,7 +49,7 @@ func (l *RefreshTokenLogic) RefreshToken(r *http.Request) (resp *types.Response,
redisTokenData := types.RedisToken{}
err = json.Unmarshal([]byte(tokenData), &redisTokenData)
if err != nil {
return response.ErrorWithCode(403), err
return nil, err
}
if redisTokenData.RefreshToken != refreshSessionToken {
return response.ErrorWithCode(403), nil
@@ -72,7 +72,7 @@ func (l *RefreshTokenLogic) RefreshToken(r *http.Request) (resp *types.Response,
}
err = l.svcCtx.RedisClient.Set(l.ctx, constant.UserTokenPrefix+refreshToken.UserID, redisToken, time.Hour*24*7).Err()
if err != nil {
return response.ErrorWithCode(403), err
return nil, err
}
return response.SuccessWithData(accessToken), nil

View File

@@ -47,19 +47,19 @@ func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordRequest) (res
}
// 验证码检查通过后立即删除或标记为已使用
if err = l.svcCtx.RedisClient.Del(l.ctx, constant.UserSmsRedisPrefix+req.Phone).Err(); err != nil {
return response.ErrorWithI18n(l.ctx, "login.captchaError"), err
return nil, err
}
user, err := l.svcCtx.MySQLClient.ScaAuthUser.Query().Where(scaauthuser.Phone(req.Phone), scaauthuser.Deleted(constant.NotDeleted)).First(l.ctx)
if err != nil && ent.IsNotFound(err) {
return response.ErrorWithI18n(l.ctx, "login.userNotRegistered"), err
return response.ErrorWithI18n(l.ctx, "login.userNotRegistered"), nil
}
encrypt, err := utils.Encrypt(req.Password)
if err != nil {
return response.ErrorWithI18n(l.ctx, "login.resetPasswordError"), err
return nil, err
}
err = user.Update().SetPassword(encrypt).Exec(l.ctx)
if err != nil {
return response.ErrorWithI18n(l.ctx, "login.resetPasswordError"), err
return nil, err
}
return response.Success(), nil
}