更新logic response等使用方法

This commit is contained in:
eson 2023-06-07 11:57:04 +08:00
parent 12fd21df01
commit 50106e3c12
9 changed files with 31 additions and 41 deletions

View File

@ -19,8 +19,7 @@ func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}}
} }
func (l *{{.logic}}) {{.function}}({{.request}}) (resp *types.Response) { func (l *{{.logic}}) {{.function}}({{.request}}) (resp *types.Response) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error") // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
resp = &types.Response{}
{{.returnString}} resp {{.returnString}} resp
} }

View File

@ -13,7 +13,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
[]rest.Route{ []rest.Route{
{ {
Method: http.MethodGet, Method: http.MethodPost,
Path: "/user/login", Path: "/user/login",
Handler: UserLoginHandler(serverCtx), Handler: UserLoginHandler(serverCtx),
}, },

View File

@ -27,7 +27,6 @@ func NewGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTypeLo
func (l *GetTypeLogic) GetType(req *types.Request) (resp *types.Response) { func (l *GetTypeLogic) GetType(req *types.Request) (resp *types.Response) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error") // 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx) data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx)
if err != nil { if err != nil {
@ -35,7 +34,5 @@ func (l *GetTypeLogic) GetType(req *types.Request) (resp *types.Response) {
return return
} }
resp.SetStatus(basic.CodeOK, "success", data) return resp.SetStatus(basic.CodeOK, "success", data)
return resp
} }

View File

@ -30,16 +30,14 @@ func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request) (resp *types.Resp
resp = &types.Response{} resp = &types.Response{}
loginInfo := auth.GetUserInfoFormCtx(l.ctx) loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 { if loginInfo.UserId == 0 {
resp.SetStatus(basic.CodeOK, "parse login info err ") return resp.SetStatus(basic.CodeOK, "parse login info err ")
return resp
} }
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, loginInfo.UserId) fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, loginInfo.UserId)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
resp.Set(510, err.Error())
return resp return resp.Set(510, err.Error())
} }
resp.SetStatus(basic.CodeOK, fsUserModel) return resp.SetStatus(basic.CodeOK, fsUserModel)
return resp
} }

View File

@ -31,12 +31,9 @@ func (l *UserFontsLogic) UserFonts(req *types.Request) (resp *types.Response) {
data, err := model.NewFsFontModel(l.svcCtx.MysqlConn).FindAllOrderSortByDesc(l.ctx) data, err := model.NewFsFontModel(l.svcCtx.MysqlConn).FindAllOrderSortByDesc(l.ctx)
if err != nil { if err != nil {
// panic(err)
logx.Error(err) logx.Error(err)
resp.SetStatus(basic.CodeOK, data) return resp.SetStatus(basic.CodeOK, data)
return resp
} }
resp.SetStatus(basic.CodeOK) return resp.SetStatus(basic.CodeOK)
return resp
} }

View File

@ -47,8 +47,7 @@ func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Res
if err == model.ErrNotFound { if err == model.ErrNotFound {
logx.Error(err) logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()), jwtToken
return resp, jwtToken
} }
// jwt 生成 // jwt 生成
@ -56,8 +55,7 @@ func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Res
jwtToken, err = l.genJwtToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, userModel.Id) jwtToken, err = l.genJwtToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, userModel.Id)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
resp.SetStatus(basic.CodeUnAuth) return resp.SetStatus(basic.CodeUnAuth), jwtToken
return resp, jwtToken
} }
data := &types.DataUserLogin{ data := &types.DataUserLogin{
@ -65,6 +63,5 @@ func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Res
JwtToken: jwtToken, JwtToken: jwtToken,
} }
resp.SetStatus(basic.CodeOK, data) return resp.SetStatus(basic.CodeOK, data), jwtToken
return resp, jwtToken
} }

View File

@ -32,8 +32,7 @@ func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoFo
loginInfo := auth.GetUserInfoFormCtx(l.ctx) loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 { if loginInfo.UserId == 0 {
resp.SetStatus(basic.CodeOK, "parse login info err ") return resp.SetStatus(basic.CodeOK, "parse login info err ")
return resp
} }
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, loginInfo.UserId) fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, loginInfo.UserId)
if err != nil { if err != nil {
@ -45,6 +44,5 @@ func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoFo
return resp return resp
} }
resp.SetStatus(basic.CodeOK, fsUserModel) return resp.SetStatus(basic.CodeOK, fsUserModel)
return resp
} }

View File

@ -75,6 +75,13 @@ type Response struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type ResponseJwt struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
AccessSecret string `json:"accessSecret"`
AccessExpire int64 `json:"accessExpire"`
}
type Auth struct { type Auth struct {
AccessSecret string `json:"accessSecret"` AccessSecret string `json:"accessSecret"`
@ -85,41 +92,39 @@ type Auth struct {
// Set 设置Response的Code和Message值 // Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response { func (resp *Response) Set(Code int, Message string) *Response {
return &Response{ return &Response{
Code: Code, Code: Code,
Message: Message, Message: Message,
} }
} }
// Set 设置整个Response // Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
return &Response{ return &Response{
Code: Code, Code: Code,
Message: Message, Message: Message,
Data: Data, Data: Data,
} }
} }
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 // SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response { func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
newResp := &Response{ newResp := &Response{
Code: sr.Code, Code: sr.Code,
} }
if len(data) == 1 { if len(data) == 1 {
newResp.Data = data[0] newResp.Data = data[0]
} }
return newResp return newResp
} }
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 // SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{ newResp := &Response{
Code: sr.Code, Code: sr.Code,
Message: sr.Message, Message: sr.Message,
} }
if len(data) == 1 { if len(data) == 1 {
newResp.Data = data[0] newResp.Data = data[0]
} }
return newResp return newResp
} }

View File

@ -36,7 +36,6 @@ service home-user-auth {
get /user/basic-info(request) returns (response); get /user/basic-info(request) returns (response);
} }
type RequestBasicInfoForm { type RequestBasicInfoForm {
FirstName string `form:"first_name,optional" db:"first_name"` // FirstName FirstName string `form:"first_name,optional" db:"first_name"` // FirstName
LastName string `form:"last_name,optional" db:"last_name"` // LastName LastName string `form:"last_name,optional" db:"last_name"` // LastName
@ -101,4 +100,4 @@ type DataUserBasicInfo {
type DataGetType { type DataGetType {
Id int64 `db:"id" json:"key"` // ID Id int64 `db:"id" json:"key"` // ID
Name string `db:"name" json:"name"` // 餐厅名字 Name string `db:"name" json:"name"` // 餐厅名字
} }