更新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) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
{{.returnString}} resp
}

View File

@ -13,7 +13,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Method: http.MethodPost,
Path: "/user/login",
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) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx)
if err != nil {
@ -35,7 +34,5 @@ func (l *GetTypeLogic) GetType(req *types.Request) (resp *types.Response) {
return
}
resp.SetStatus(basic.CodeOK, "success", data)
return resp
return resp.SetStatus(basic.CodeOK, "success", data)
}

View File

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

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)
if err != nil {
// panic(err)
logx.Error(err)
resp.SetStatus(basic.CodeOK, data)
return resp
return resp.SetStatus(basic.CodeOK, data)
}
resp.SetStatus(basic.CodeOK)
return resp
return resp.SetStatus(basic.CodeOK)
}

View File

@ -47,8 +47,7 @@ func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Res
if err == model.ErrNotFound {
logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
return resp, jwtToken
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()), jwtToken
}
// 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)
if err != nil {
logx.Error(err)
resp.SetStatus(basic.CodeUnAuth)
return resp, jwtToken
return resp.SetStatus(basic.CodeUnAuth), jwtToken
}
data := &types.DataUserLogin{
@ -65,6 +63,5 @@ func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Res
JwtToken: jwtToken,
}
resp.SetStatus(basic.CodeOK, data)
return resp, jwtToken
return resp.SetStatus(basic.CodeOK, data), jwtToken
}

View File

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

View File

@ -75,6 +75,13 @@ type Response struct {
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 {
AccessSecret string `json:"accessSecret"`
@ -85,41 +92,39 @@ type Auth struct {
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{
Code: Code,
Code: Code,
Message: Message,
}
}
// 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{
Code: Code,
Code: Code,
Message: Message,
Data: Data,
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{
Code: sr.Code,
}
if len(data) == 1 {
newResp.Data = data[0]
}
}
return newResp
}
// 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{
Code: sr.Code,
Code: sr.Code,
Message: sr.Message,
}
if len(data) == 1 {
newResp.Data = data[0]
}
}
return newResp
}

View File

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