Browse Source

[dev]login的逻辑接口化

cris 2 weeks ago
parent
commit
b1b2882c15

+ 4 - 1
internal/cmd/cmd.go

@@ -6,6 +6,8 @@ import (
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/net/ghttp"
 	"github.com/gogf/gf/v2/os/gcmd"
+
+	"cris/internal/controller/login"
 )
 
 var (
@@ -16,8 +18,9 @@ var (
 		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
 			s := g.Server()
 			//测试login路由
-			s.Group("/test", func(group *ghttp.RouterGroup) {
+			s.Group("/", func(group *ghttp.RouterGroup) {
 				group.Middleware(ghttp.MiddlewareHandlerResponse)
+				group.Bind(login.NewV1())
 			})
 			//websocket路由
 			s.Group("/websocket", func(group *ghttp.RouterGroup) {

+ 10 - 4
internal/controller/login/login_v1_login.go

@@ -3,12 +3,18 @@ package login
 import (
 	"context"
 
-	"github.com/gogf/gf/v2/errors/gcode"
-	"github.com/gogf/gf/v2/errors/gerror"
+	"github.com/gogf/gf/v2/frame/g"
 
-	"cris/api/login/v1"
+	v1 "cris/api/login/v1"
+	"cris/internal/service"
 )
 
 func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) {
-	return nil, gerror.NewCode(gcode.CodeNotImplemented)
+	r := g.RequestFromCtx(ctx)
+	login, err := service.Login().Login(ctx, req)
+	if err != nil {
+		r.Response.WriteExit("login error")
+	}
+	r.Response.WriteJson(login)
+	return
 }

+ 2 - 6
internal/logic/chat/chat.go

@@ -17,13 +17,9 @@ var clients = make(map[*websocket.Conn]bool)
 var broadcast = make(chan string) // 广播消息的通道
 var mu sync.Mutex                 //互斥锁
 
-type chat struct{}
+type schat struct{}
 
-func New() *chat {
-	return &chat{}
-}
-
-func (s *chat) Chat(ctx context.Context, r *ghttp.Request, req *v1.ChatReq) {
+func (s *schat) Chat(ctx context.Context, r *ghttp.Request, req *v1.ChatReq) {
 
 	// 升级 HTTP 请求为 WebSocket
 	upgrader := websocket.Upgrader{

+ 5 - 5
internal/logic/login/login.go

@@ -4,24 +4,24 @@ import (
 	"context"
 
 	v1 "cris/api/login/v1"
+	"cris/internal/service"
 )
 
 type (
 	slogin struct{}
 )
 
-func New() *slogin {
-	return &slogin{}
+func init() {
+	service.RegisterLogin(&slogin{})
 }
 
-func (s *slogin) Login(ctx context.Context, req *v1.LoginReq) *v1.LoginRes {
+func (s *slogin) Login(ctx context.Context, req *v1.LoginReq) (*v1.LoginRes, error) {
 	// 在这里进行具体的逻辑处理
-
 	return &v1.LoginRes{
 		Message: "Login successful",
 		Data: map[string]interface{}{
 			"email":    req.Email,
 			"password": req.Password,
 		},
-	}
+	}, nil
 }

+ 0 - 7
internal/service/chat.go

@@ -1,8 +1 @@
-// ================================================================================
-// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
-// You can delete these comments if you wish manually maintain this interface file.
-// ================================================================================
-
 package service
-
-type ()

+ 22 - 6
internal/service/login.go

@@ -1,8 +1,24 @@
-// ================================================================================
-// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
-// You can delete these comments if you wish manually maintain this interface file.
-// ================================================================================
-
 package service
 
-type ()
+import (
+	"context"
+
+	v1 "cris/api/login/v1"
+)
+
+type ILogin interface {
+	Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error)
+}
+
+var login ILogin
+
+func Login() ILogin {
+	if login == nil {
+		panic("login 接口未实现或注册")
+	}
+	return login
+}
+
+func RegisterLogin(l ILogin) {
+	login = l
+}