🎨 完善注册逻辑
This commit is contained in:
@@ -193,3 +193,51 @@ func (*AppUserApi) PwdLogin(ctx *gin.Context) {
|
||||
"ExpiresAt": claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
|
||||
}, "登录成功", ctx)
|
||||
}
|
||||
|
||||
func (a *AppUserApi) Register(context *gin.Context) {
|
||||
var p request.RegisterReq
|
||||
if err := context.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), context)
|
||||
global.GVA_LOG.Error("参数错误,注册失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
if result, _ := global.GVA_REDIS.Get(context, fmt.Sprintf("VerifyCode:%s", p.Phone)).Result(); result != p.Code {
|
||||
global.GVA_LOG.Error("验证码错误", zap.String("phone", p.Phone))
|
||||
r.FailWithMessage("验证码错误", context)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := appUserService.Register(p)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("注册失败", zap.Error(err))
|
||||
r.FailWithMessage("注册失败", context)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成token
|
||||
token, claims, err := user_jwt.LoginToken(user)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取token失败!", zap.Error(err))
|
||||
r.FailWithMessage("获取token失败", context)
|
||||
return
|
||||
}
|
||||
|
||||
// 此处过期时间等于jwt过期时间
|
||||
dr, err := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
timer := dr
|
||||
if err := global.GVA_REDIS.Set(context, user.Phone, token, timer).Err(); err != nil {
|
||||
global.GVA_LOG.Error("设置登录状态失败!", zap.Error(err))
|
||||
r.FailWithMessage("设置登录状态失败", context)
|
||||
return
|
||||
}
|
||||
user_jwt.SetToken(context, token, int(claims.RegisteredClaims.ExpiresAt.Unix()-time.Now().Unix()))
|
||||
r.OkWithDetailed(gin.H{
|
||||
"User": user,
|
||||
"Token": token,
|
||||
"ExpiresAt": claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
|
||||
}, "注册成功", context)
|
||||
}
|
||||
|
@@ -62,6 +62,7 @@ type RegisterReq struct {
|
||||
Phone string `json:"phone" form:"phone" vd:"@:len($)>0; msg:'手机号码不能为空'"`
|
||||
Password string `json:"password" form:"password" vd:"@:len($)>0; msg:'密码不能为空'"`
|
||||
NickName string `json:"nick_name" form:"nick_name"`
|
||||
UserType int8 `json:"user_type" form:"user_type" vd:"@:len($)>0; msg:'用户类型不能为空'"`
|
||||
UserLabel int64 `json:"user_label" form:"user_label" vd:"@:len($)>0; msg:'用户标签不能为空'"`
|
||||
Code string `json:"code" form:"code" vd:"@:len($)>0; msg:'验证码不能为空'"`
|
||||
UserType int8 `json:"user_type" form:"user_type"`
|
||||
UserLabel int64 `json:"user_label" form:"user_label"`
|
||||
}
|
||||
|
@@ -17,5 +17,6 @@ func (s *UserRouter) InitAppUserRouter(AppAuthGroup, PublicRouter *gin.RouterGro
|
||||
publicRouter.POST("pwdlogin", userApi.PwdLogin) // 密码登录
|
||||
publicRouter.POST("sms/send", userApi.SendCode) // 发送短信验证码
|
||||
publicRouter.POST("login", userApi.Login) // 短信验证码登录
|
||||
publicRouter.POST("register", userApi.Register) // 注册
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user