完善logic.tpl的错误
This commit is contained in:
parent
996874a253
commit
dc73ff1679
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
|
37
home-user-auth/internal/handler/userstatusconfighandler.go
Normal file
37
home-user-auth/internal/handler/userstatusconfighandler.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
31
home-user-auth/internal/logic/userstatusconfiglogic.go
Normal file
31
home-user-auth/internal/logic/userstatusconfiglogic.go
Normal file
|
@ -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)
|
||||
}
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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 错误
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user