From e932171ffa7c8e4e6d2417a8d85be6daefe1ba7e Mon Sep 17 00:00:00 2001 From: icechen Date: Fri, 14 Jan 2022 01:28:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=81=E8=A3=85=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/zeus/api/lark/internal/user/user.go | 23 ++++-------------- app/zeus/api/lark/main.go | 3 ++- app/zeus/service/lark/pkg/admin/user.go | 32 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 app/zeus/service/lark/pkg/admin/user.go diff --git a/app/zeus/api/lark/internal/user/user.go b/app/zeus/api/lark/internal/user/user.go index f74ba83..f19a3ee 100644 --- a/app/zeus/api/lark/internal/user/user.go +++ b/app/zeus/api/lark/internal/user/user.go @@ -1,6 +1,7 @@ package user import ( + "git.icechen.cn/monorepo/backend/app/zeus/service/lark/pkg/admin" "git.icechen.cn/monorepo/backend/pkg/api" "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" "git.icechen.cn/monorepo/backend/pkg/rpc" @@ -27,22 +28,8 @@ func LoginHandle(c *fiber.Ctx) error { return c.JSON(response) } -// UserInfoHandle 管理员信息 -func UserInfoHandle(c *fiber.Ctx) error { - var p = struct { - Token string `json:"token"` - }{} - err := api.Bind(c, &p) - if err != nil { - return err - } - - response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Info(c.Context(), &lark.InfoRequest{ - Token: p.Token, - }) - if err != nil { - return err - } - - return c.JSON(response) +// InfoHandle 管理员信息 +func InfoHandle(c *fiber.Ctx) error { + userInfo := admin.GetUserInfo(c) + return c.JSON(userInfo) } diff --git a/app/zeus/api/lark/main.go b/app/zeus/api/lark/main.go index 7173b9a..a005931 100755 --- a/app/zeus/api/lark/main.go +++ b/app/zeus/api/lark/main.go @@ -2,6 +2,7 @@ package main import ( "git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user" + "git.icechen.cn/monorepo/backend/app/zeus/service/lark/pkg/admin" "git.icechen.cn/monorepo/backend/pkg/api" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" @@ -17,7 +18,7 @@ func main() { userRouter := router.Group("/user") userRouter.Post("/login", user.LoginHandle) - userRouter.Post("/info", user.UserInfoHandle) + userRouter.Post("/info", admin.MiddlewareLarkAdmin, user.InfoHandle) err := router.Listen(":8080") if err != nil { diff --git a/app/zeus/service/lark/pkg/admin/user.go b/app/zeus/service/lark/pkg/admin/user.go new file mode 100644 index 0000000..a36835b --- /dev/null +++ b/app/zeus/service/lark/pkg/admin/user.go @@ -0,0 +1,32 @@ +package admin + +import ( + "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" + "git.icechen.cn/monorepo/backend/pkg/rpc" + "github.com/gofiber/fiber/v2" +) + +const AuthorizationHeader = "Authorization" +const UserInfoKey = "user_info" + +type UserInfo = *lark.InfoResponse + +// MiddlewareLarkAdmin 飞书管理员token校验 +func MiddlewareLarkAdmin(c *fiber.Ctx) error { + token := c.Get(AuthorizationHeader) + + response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Info(c.Context(), &lark.InfoRequest{ + Token: token, + }) + if err != nil { + return err + } + + c.Context().SetUserValue(UserInfoKey, response) + return c.Next() +} + +// GetUserInfo 获取用户信息 +func GetUserInfo(c *fiber.Ctx) UserInfo { + return c.Context().UserValue(UserInfoKey).(UserInfo) +}