diff --git a/goctl_template/api/logic.tpl b/goctl_template/api/logic.tpl index 4eba01a7..163c667a 100644 --- a/goctl_template/api/logic.tpl +++ b/goctl_template/api/logic.tpl @@ -21,5 +21,5 @@ func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}} func (l *{{.logic}}) {{.function}}({{.request}}) (resp *types.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - {{.returnString}} resp + {{.returnString}} resp.SetStatus(basic.CodeOK) } diff --git a/home-user-auth/internal/handler/routes.go b/home-user-auth/internal/handler/routes.go index 09e53cbd..b1e03545 100644 --- a/home-user-auth/internal/handler/routes.go +++ b/home-user-auth/internal/handler/routes.go @@ -32,6 +32,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/user/basic-info", Handler: UserSaveBasicInfoHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/user/status-config", + Handler: UserStatusConfigHandler(serverCtx), + }, }, ) diff --git a/home-user-auth/internal/handler/userstatusconfighandler.go b/home-user-auth/internal/handler/userstatusconfighandler.go new file mode 100644 index 00000000..2c1e0406 --- /dev/null +++ b/home-user-auth/internal/handler/userstatusconfighandler.go @@ -0,0 +1,37 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/home-user-auth/internal/logic" + "fusenapi/home-user-auth/internal/svc" + "fusenapi/home-user-auth/internal/types" +) + +func UserStatusConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.RequestBasicInfoForm + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &types.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + + l := logic.NewUserStatusConfigLogic(r.Context(), svcCtx) + resp := l.UserStatusConfig(&req) + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/home-user-auth/internal/logic/userbasicinfologic.go b/home-user-auth/internal/logic/userbasicinfologic.go index dca8ae67..1ae44c8e 100644 --- a/home-user-auth/internal/logic/userbasicinfologic.go +++ b/home-user-auth/internal/logic/userbasicinfologic.go @@ -35,6 +35,5 @@ func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request) (resp *types.Resp logx.Error(err) return resp.Set(510, err.Error()) } - return resp.SetStatus(basic.CodeOK, fsUserModel) } diff --git a/home-user-auth/internal/logic/userloginlogic.go b/home-user-auth/internal/logic/userloginlogic.go index 7baa6d3f..b8f0c551 100644 --- a/home-user-auth/internal/logic/userloginlogic.go +++ b/home-user-auth/internal/logic/userloginlogic.go @@ -28,7 +28,6 @@ func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLog } func (l *UserLoginLogic) genJwtToken(accessSecret string, accessExpire, nowSec, userid int64) (string, error) { - claims := make(jwt.MapClaims) claims["exp"] = nowSec + accessExpire claims["iat"] = nowSec @@ -39,13 +38,19 @@ func (l *UserLoginLogic) genJwtToken(accessSecret string, accessExpire, nowSec, } func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Response, jwtToken string) { - userModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOneByEmail(l.ctx, req.Name) + m := model.NewFsUserModel(l.svcCtx.MysqlConn) + userModel, err := m.FindOneByEmail(l.ctx, req.Name) if err == model.ErrNotFound { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()), jwtToken } + if userModel.PasswordHash != req.Password { + logx.Info("密码错误") + return resp.SetStatusWithMessage(basic.CodeUnAuth, "密码错误"), jwtToken + } + // jwt 生成 nowSec := time.Now().Unix() jwtToken, err = l.genJwtToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, userModel.Id) @@ -54,9 +59,13 @@ func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Res return resp.SetStatus(basic.CodeUnAuth), jwtToken } + err = m.UpdateVerificationToken(l.ctx, userModel.Id, jwtToken) + if err != nil { + return resp.SetStatus(basic.CodeUnAuth), jwtToken + } + data := &types.DataUserLogin{ - Token: userModel.PasswordResetToken.String, - JwtToken: jwtToken, + Token: jwtToken, } return resp.SetStatus(basic.CodeOK, data), jwtToken diff --git a/home-user-auth/internal/logic/userstatusconfiglogic.go b/home-user-auth/internal/logic/userstatusconfiglogic.go new file mode 100644 index 00000000..c3d3cbac --- /dev/null +++ b/home-user-auth/internal/logic/userstatusconfiglogic.go @@ -0,0 +1,31 @@ +package logic + +import ( + "context" + + "fusenapi/home-user-auth/internal/svc" + "fusenapi/home-user-auth/internal/types" + "fusenapi/utils/basic" + + "github.com/zeromicro/go-zero/core/logx" +) + +type UserStatusConfigLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserStatusConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserStatusConfigLogic { + return &UserStatusConfigLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *UserStatusConfigLogic) UserStatusConfig(req *types.RequestBasicInfoForm) (resp *types.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + + return resp.SetStatus(basic.CodeOK) +} diff --git a/home-user-auth/internal/types/types.go b/home-user-auth/internal/types/types.go index 3b4828e9..a785df49 100644 --- a/home-user-auth/internal/types/types.go +++ b/home-user-auth/internal/types/types.go @@ -121,7 +121,7 @@ func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) * func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { newResp := &Response{ Code: sr.Code, - Message: sr.Message, + Message: msg, } if len(data) == 1 { newResp.Data = data[0] diff --git a/model/fsusermodel.go b/model/fsusermodel.go index 39ec5d31..ad370750 100755 --- a/model/fsusermodel.go +++ b/model/fsusermodel.go @@ -1,6 +1,11 @@ package model -import "github.com/zeromicro/go-zero/core/stores/sqlx" +import ( + "context" + "fmt" + + "github.com/zeromicro/go-zero/core/stores/sqlx" +) var _ FsUserModel = (*customFsUserModel)(nil) @@ -9,6 +14,7 @@ type ( // and implement the added methods in customFsUserModel. FsUserModel interface { fsUserModel + UpdateVerificationToken(ctx context.Context, userid int64, token string) error } customFsUserModel struct { @@ -22,3 +28,9 @@ func NewFsUserModel(conn sqlx.SqlConn) FsUserModel { defaultFsUserModel: newFsUserModel(conn), } } + +func (m *defaultFsUserModel) UpdateVerificationToken(ctx context.Context, userid int64, verificationToken string) error { + query := fmt.Sprintf("update %s set `verification_token` = ? where `id` = ?", m.table) + _, err := m.conn.ExecCtx(ctx, query, verificationToken, userid) + return err +} diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index 4a72aefa..fd1bd500 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -26,6 +26,9 @@ service home-user-auth { @handler UserSaveBasicInfoHandler post /user/basic-info(RequestBasicInfoForm) returns (response); + + @handler UserStatusConfigHandler + post /user/status-config(RequestBasicInfoForm) returns (response); } @server( diff --git a/utils/basic/basic.go b/utils/basic/basic.go index 039bf70e..197832fb 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -7,7 +7,11 @@ type StatusResponse struct { } var ( - CodeOK = &StatusResponse{200, "success"} // 成功 - CodeServiceErr = &StatusResponse{510, "unknown error"} // 错误 - CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权 + CodeOK = &StatusResponse{200, "success"} // 成功 + CodeApiErr = &StatusResponse{500, "api error"} // api 错误 + CodeSaveErr = &StatusResponse{501, "fail to save"} // 储存错误 + CodeServiceErr = &StatusResponse{510, "server logic error"} // server logic 错误 + CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权 + + CodeUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误 )