From fc806b71c62c03bb85440f0cada1dea90d55cd42 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Mon, 12 Jun 2023 18:08:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E6=96=B0=E5=8F=AF=E4=BB=A5=E8=B7=91?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- goctl_template/api/main.tpl | 20 +++++++ server/home-user-auth/home-user-auth.go | 26 ++++++---- .../home-user-auth/internal/handler/routes.go | 52 +++++++++++++++++++ .../internal/logic/useraddresslistlogic.go | 2 +- .../internal/logic/userbasicinfologic.go | 2 +- .../internal/logic/usergettypelogic.go | 2 +- .../internal/logic/usersavebasicinfologic.go | 2 +- .../internal/logic/userstatusconfiglogic.go | 2 +- server/home-user-auth/internal/types/types.go | 43 ++++++++++++++- .../test/useraddresslistlogic_test.go | 45 ++++++++++------ server_api/basic.api | 3 +- utils/basic/basic.go | 3 +- utils/sqlfs/builder.go | 49 ----------------- 13 files changed, 165 insertions(+), 86 deletions(-) create mode 100644 server/home-user-auth/internal/handler/routes.go delete mode 100644 utils/sqlfs/builder.go diff --git a/goctl_template/api/main.tpl b/goctl_template/api/main.tpl index ad1a46d0..1b15576a 100644 --- a/goctl_template/api/main.tpl +++ b/goctl_template/api/main.tpl @@ -24,3 +24,23 @@ func main() { fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) server.Start() } + + + +// var testConfigFile = flag.String("f", "../etc/{{.serviceName}}.yaml", "the config file") + +// func GetTestServer() *rest.Server { +// flag.Parse() + +// var c config.Config +// conf.MustLoad(*testConfigFile, &c) + +// server := rest.MustNewServer(c.RestConf) +// defer server.Stop() + +// ctx := svc.NewServiceContext(c) +// handler.RegisterHandlers(server, ctx) + +// fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) +// return server +// } \ No newline at end of file diff --git a/server/home-user-auth/home-user-auth.go b/server/home-user-auth/home-user-auth.go index 502ccb9b..6f11b21f 100644 --- a/server/home-user-auth/home-user-auth.go +++ b/server/home-user-auth/home-user-auth.go @@ -30,18 +30,22 @@ func main() { server.Start() } -func GetTestServer() *rest.Server { - flag.Parse() +// 测试代码 可以直接复制新建 - var c config.Config - conf.MustLoad(*configFile, &c) +// var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file") - server := rest.MustNewServer(c.RestConf) - defer server.Stop() +// func GetTestServer() *rest.Server { +// flag.Parse() - ctx := svc.NewServiceContext(c) - handler.RegisterHandlers(server, ctx) +// var c config.Config +// conf.MustLoad(*testConfigFile, &c) - fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) - return server -} +// server := rest.MustNewServer(c.RestConf) +// defer server.Stop() + +// ctx := svc.NewServiceContext(c) +// handler.RegisterHandlers(server, ctx) + +// fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) +// return server +// } diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go new file mode 100644 index 00000000..5738d5a4 --- /dev/null +++ b/server/home-user-auth/internal/handler/routes.go @@ -0,0 +1,52 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/server/home-user-auth/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodPost, + Path: "/user/login", + Handler: UserLoginHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/user/fonts", + Handler: UserFontsHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/user/get-type", + Handler: UserGetTypeHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/user/basic-info", + Handler: UserSaveBasicInfoHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/user/status-config", + Handler: UserStatusConfigHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/user/basic-info", + Handler: UserBasicInfoHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/user/address-list", + Handler: UserAddressListHandler(serverCtx), + }, + }, + ) +} diff --git a/server/home-user-auth/internal/logic/useraddresslistlogic.go b/server/home-user-auth/internal/logic/useraddresslistlogic.go index 242cd022..51901f3d 100644 --- a/server/home-user-auth/internal/logic/useraddresslistlogic.go +++ b/server/home-user-auth/internal/logic/useraddresslistlogic.go @@ -26,7 +26,7 @@ func NewUserAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U } } -func (l *UserAddressListLogic) UserAddressList(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) { +func (l *UserAddressListLogic) UserAddressList(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) m := model.NewFsAddressModel(l.svcCtx.MysqlConn) diff --git a/server/home-user-auth/internal/logic/userbasicinfologic.go b/server/home-user-auth/internal/logic/userbasicinfologic.go index 33286661..0dd39b84 100644 --- a/server/home-user-auth/internal/logic/userbasicinfologic.go +++ b/server/home-user-auth/internal/logic/userbasicinfologic.go @@ -25,7 +25,7 @@ func NewUserBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Use } } -func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) { +func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { loginInfo := auth.GetUserInfoFormCtx(l.ctx) if loginInfo.UserId == 0 { return resp.SetStatus(basic.CodeOK, "parse login info err ") diff --git a/server/home-user-auth/internal/logic/usergettypelogic.go b/server/home-user-auth/internal/logic/usergettypelogic.go index c1c1ef9b..3e262f85 100644 --- a/server/home-user-auth/internal/logic/usergettypelogic.go +++ b/server/home-user-auth/internal/logic/usergettypelogic.go @@ -26,7 +26,7 @@ func NewUserGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserG } } -func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) { +func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx) diff --git a/server/home-user-auth/internal/logic/usersavebasicinfologic.go b/server/home-user-auth/internal/logic/usersavebasicinfologic.go index ca0bee41..3606cdb6 100644 --- a/server/home-user-auth/internal/logic/usersavebasicinfologic.go +++ b/server/home-user-auth/internal/logic/usersavebasicinfologic.go @@ -26,7 +26,7 @@ func NewUserSaveBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) } } -func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm, userinfo *auth.UserInfo) (resp *types.Response) { +func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm, userinfo *auth.UserInfo) (resp *basic.Response) { loginInfo := auth.GetUserInfoFormCtx(l.ctx) if loginInfo.UserId == 0 { return resp.SetStatus(basic.CodeOK, "parse login info err ") diff --git a/server/home-user-auth/internal/logic/userstatusconfiglogic.go b/server/home-user-auth/internal/logic/userstatusconfiglogic.go index cf6e158d..dc27021c 100644 --- a/server/home-user-auth/internal/logic/userstatusconfiglogic.go +++ b/server/home-user-auth/internal/logic/userstatusconfiglogic.go @@ -25,7 +25,7 @@ func NewUserStatusConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) * } } -func (l *UserStatusConfigLogic) UserStatusConfig(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) { +func (l *UserStatusConfigLogic) UserStatusConfig(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) data := &types.DataStatusConfig{ diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index 1a2a01a2..c964fefc 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -2,7 +2,7 @@ package types import ( - + "fusenapi/utils/basic" ) type Request struct { @@ -125,4 +125,43 @@ type Auth struct { AccessExpire int64 `json:"accessExpire"` RefreshAfter int64 `json:"refreshAfter"` } - \ No newline at end of file + +// Set 设置Response的Code和Message值 +func (resp *Response) Set(Code int, Message string) *Response { + return &Response{ + Code: Code, + Message: Message, + } +} + +// Set 设置整个Response +func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { + return &Response{ + Code: Code, + Message: Message, + Data: Data, + } +} + +// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 +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 { + newResp := &Response{ + Code: sr.Code, + Message: msg, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} diff --git a/server/home-user-auth/test/useraddresslistlogic_test.go b/server/home-user-auth/test/useraddresslistlogic_test.go index 3ad2cd23..91303983 100644 --- a/server/home-user-auth/test/useraddresslistlogic_test.go +++ b/server/home-user-auth/test/useraddresslistlogic_test.go @@ -6,7 +6,6 @@ import ( "fusenapi/server/home-user-auth/internal/config" "fusenapi/server/home-user-auth/internal/handler" "fusenapi/server/home-user-auth/internal/svc" - "log" "testing" "github.com/474420502/requests" @@ -14,30 +13,29 @@ import ( "github.com/zeromicro/go-zero/rest" ) -var configFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file") +var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file") +var cnf config.Config func GetTestServer() *rest.Server { flag.Parse() - var c config.Config - conf.MustLoad(*configFile, &c) + conf.MustLoad(*testConfigFile, &cnf) - server := rest.MustNewServer(c.RestConf) + server := rest.MustNewServer(cnf.RestConf) defer server.Stop() - ctx := svc.NewServiceContext(c) + ctx := svc.NewServiceContext(cnf) handler.RegisterHandlers(server, ctx) - fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port) return server } func TestCaseAddressList(t *testing.T) { server := GetTestServer() - // http.NewRequest("POST", "http://localhost:8888/user/login", body io.Reader) ses := requests.NewSession() - tp := ses.Post("http://localhost:8888/user/login") + tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", cnf.Host, cnf.Port)) tp.SetBodyJson(map[string]interface{}{ "name": "devenv@sina.cn", "pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC", @@ -48,13 +46,28 @@ func TestCaseAddressList(t *testing.T) { } result := resp.Json() token := result.Get("data.token") - ses.Header.Add("Authorization", token.String()) - log.Println(resp.ContentString()) - - resp, err = ses.Get("http://localhost:8888/user/address-list").TestInServer(server) - if err != nil { - panic(err) + if !token.Exists() { + t.Error("data.token is not exists") } - log.Println(string(resp.Content())) + ses.Header.Add("Authorization", token.String()) + resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(server) + if err != nil { + t.Error(err) + } + result = resp.Json().Get("code") + if !result.Exists() { + t.Error("code is not exists") + } + if result.Int() != 200 { + t.Error("code != 200") + } + + result = resp.Json().Get("msg") + if !result.Exists() { + t.Error("msg is not exists") + } + if result.String() != "success" { + t.Error(result.String()) + } } diff --git a/server_api/basic.api b/server_api/basic.api index d3eba6ea..f400f8fd 100644 --- a/server_api/basic.api +++ b/server_api/basic.api @@ -6,9 +6,8 @@ info ( author: "" email: "" ) -type request { -} + // response 统一返回码 type response { Code int `json:"code"` diff --git a/utils/basic/basic.go b/utils/basic/basic.go index b6875e31..c5bcc5b2 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -42,7 +42,8 @@ func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *R // SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Response { newResp := &Response{ - Code: sr.Code, + Code: sr.Code, + Message: sr.Message, } if len(data) == 1 { newResp.Data = data[0] diff --git a/utils/sqlfs/builder.go b/utils/sqlfs/builder.go deleted file mode 100644 index f6117bb7..00000000 --- a/utils/sqlfs/builder.go +++ /dev/null @@ -1,49 +0,0 @@ -package sqlfs - -import ( - "reflect" - "strings" -) - -var dbTag = "db" - -// RawFieldNames converts golang struct field into slice string. -func RawFieldNames[T any]() []string { - - var out []string - - var a T - v := reflect.ValueOf(a) - typ := v.Type() - - for i := 0; i < v.NumField(); i++ { - // gets us a StructField - fi := typ.Field(i) - tagv := fi.Tag.Get(dbTag) - switch tagv { - case "-": - continue - default: - // get tag name with the tag opton, e.g.: - // `db:"id"` - // `db:"id,type=char,length=16"` - // `db:",type=char,length=16"` - // `db:"-,type=char,length=16"` - if strings.Contains(tagv, ",") { - tagv = strings.TrimSpace(strings.Split(tagv, ",")[0]) - } - - if tagv == "-" { - continue - } - - if len(tagv) == 0 { - tagv = fi.Name - } - - out = append(out, tagv) - } - } - - return out -}