1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| package biz
import ( "context" "errors" "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/middleware/auth/jwt" jwt2 "github.com/golang-jwt/jwt/v4" v1 "shop/api/shop/v1" "shop/internal/conf" "shop/internal/pkg/captcha" "shop/internal/pkg/middleware/auth" "time" )
var ( ErrPasswordInvalid = errors.New("password invalid") ErrUsernameInvalid = errors.New("username invalid") ErrCaptchaInvalid = errors.New("verification code error") ErrMobileInvalid = errors.New("mobile invalid") ErrUserNotFound = errors.New("user not found") ErrLoginFailed = errors.New("login failed") ErrGenerateTokenFailed = errors.New("generate token failed") ErrAuthFailed = errors.New("authentication failed") )
type User struct { ID int64 Mobile string NickName string Birthday int64 Gender string Role int CreatedAt time.Time }
type UserRepo interface { CreateUser(c context.Context, u *User) (*User, error) UserByMobile(ctx context.Context, mobile string) (*User, error) UserById(ctx context.Context, Id int64) (*User, error) CheckPassword(ctx context.Context, password, encryptedPassword string) (bool, error)
}
type UserUsecase struct { uRepo UserRepo log *log.Helper signingKey string }
func NewUserUsecase(repo UserRepo, logger log.Logger, conf *conf.Auth) *UserUsecase { helper := log.NewHelper(log.With(logger, "module", "usecase/shop")) return &UserUsecase{uRepo: repo, log: helper, signingKey: conf.JwtKey} }
func (uc *UserUsecase) GetCaptcha(ctx context.Context) (*v1.CaptchaReply, error) { captchaInfo, err := captcha.GetCaptcha(ctx) if err != nil { return nil, err }
return &v1.CaptchaReply{ CaptchaId: captchaInfo.CaptchaId, PicPath: captchaInfo.PicPath, }, nil }
func (uc *UserUsecase) UserDetailByID(ctx context.Context) (*v1.UserDetailResponse, error) { var uId int64 if claims, ok := jwt.FromContext(ctx); ok { c := claims.(jwt2.MapClaims) if c["ID"] == nil { return nil, ErrAuthFailed } uId = int64(c["ID"].(float64)) }
user, err := uc.uRepo.UserById(ctx, uId) if err != nil { return nil, err } return &v1.UserDetailResponse{ Id: user.ID, NickName: user.NickName, Mobile: user.Mobile, }, nil }
func (uc *UserUsecase) PassWordLogin(ctx context.Context, req *v1.LoginReq) (*v1.RegisterReply, error) { if len(req.Mobile) <= 0 { return nil, ErrMobileInvalid } if len(req.Password) <= 0 { return nil, ErrUsernameInvalid } if !captcha.Store.Verify(req.CaptchaId, req.Captcha, true) { return nil, ErrCaptchaInvalid }
if user, err := uc.uRepo.UserByMobile(ctx, req.Mobile); err != nil { return nil, ErrUserNotFound } else { if passRsp, pasErr := uc.uRepo.CheckPassword(ctx, req.Password, user.Password); pasErr != nil { return nil, ErrPasswordInvalid } else { if passRsp { claims := auth.CustomClaims{ ID: user.ID, NickName: user.NickName, AuthorityId: user.Role, StandardClaims: jwt2.StandardClaims{ NotBefore: time.Now().Unix(), ExpiresAt: time.Now().Unix() + 60*60*24*30, Issuer: "Gyl", }, }
token, err := auth.CreateToken(claims, uc.signingKey) if err != nil { return nil, ErrGenerateTokenFailed } return &v1.RegisterReply{ Id: user.ID, Mobile: user.Mobile, Username: user.NickName, Token: token, ExpiredAt: time.Now().Unix() + 60*60*24*30, }, nil } else { return nil, ErrLoginFailed } } } }
func (uc *UserUsecase) CreateUser(ctx context.Context, req *v1.RegisterReq) (*v1.RegisterReply, error) { newUser, err := NewUser(req.Mobile, req.Username, req.Password) if err != nil { return nil, err } createUser, err := uc.uRepo.CreateUser(ctx, &newUser) if err != nil { return nil, err } claims := auth.CustomClaims{ ID: createUser.ID, NickName: createUser.NickName, AuthorityId: createUser.Role, StandardClaims: jwt2.StandardClaims{ NotBefore: time.Now().Unix(), ExpiresAt: time.Now().Unix() + 60*60*24*30, Issuer: "Gyl", }, } token, err := auth.CreateToken(claims, uc.signingKey) if err != nil { return nil, err }
return &v1.RegisterReply{ Id: createUser.ID, Mobile: createUser.Mobile, Username: createUser.NickName, Token: token, ExpiredAt: time.Now().Unix() + 60*60*24*30, }, nil }
func NewUser(mobile, username, password string) (User, error) { if len(mobile) <= 0 { return User{}, ErrMobileInvalid } if len(username) <= 0 { return User{}, ErrUsernameInvalid } if len(password) <= 0 { return User{}, ErrPasswordInvalid } return User{ Mobile: mobile, NickName: username, Password: password, }, nil }
|