diff --git a/app/zeus/api/lark/internal/user/user.go b/app/zeus/api/lark/internal/user/user.go index 943784b..f19a3ee 100644 --- a/app/zeus/api/lark/internal/user/user.go +++ b/app/zeus/api/lark/internal/user/user.go @@ -1,18 +1,17 @@ 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" "github.com/gofiber/fiber/v2" - ctxLogger "github.com/luizsuper/ctxLoggers" - "go.uber.org/zap" ) +// LoginHandle 管理员登录 func LoginHandle(c *fiber.Ctx) error { var p = struct { - Code string `json:"code" validate:"required"` - Email string `json:"email" label:"邮箱" validate:"email"` + Code string `json:"code" validate:"required"` }{} err := api.Bind(c, &p) if err != nil { @@ -26,6 +25,11 @@ func LoginHandle(c *fiber.Ctx) error { return err } - ctxLogger.FInfo(c, "response", zap.Any("resp", response)) 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 c6216de..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" @@ -13,9 +14,11 @@ func main() { ErrorHandler: api.FiberMiddleWareErrHandler, }) router.Use(api.FibberMiddleWareRespHandler) + router.Use(api.FibberUserCtxWithTraceHandler) userRouter := router.Group("/user") userRouter.Post("/login", user.LoginHandle) + userRouter.Post("/info", admin.MiddlewareLarkAdmin, user.InfoHandle) err := router.Listen(":8080") if err != nil { diff --git a/app/zeus/service/lark/internal/lark/user.go b/app/zeus/service/lark/internal/lark/user.go index 6668acc..622aa2b 100644 --- a/app/zeus/service/lark/internal/lark/user.go +++ b/app/zeus/service/lark/internal/lark/user.go @@ -8,7 +8,7 @@ import ( func (l Lark) Auth(code string) (*authen.UserAccessTokenInfo, error) { return l.userService.Authens.AccessToken(core.WrapContext(context.Background()), &authen.AuthenAccessTokenReqBody{ - GrantType: "", + GrantType: "authorization_code", Code: code, }).Do() } diff --git a/app/zeus/service/lark/internal/model/init.go b/app/zeus/service/lark/internal/model/init.go index 1b40bdd..ccc1d74 100644 --- a/app/zeus/service/lark/internal/model/init.go +++ b/app/zeus/service/lark/internal/model/init.go @@ -5,7 +5,7 @@ import ( ) func init() { - err := orm.DB.AutoMigrate(&UserInfo{}) + err := orm.DB.AutoMigrate(&UserInfo{}, &UserToken{}) if err != nil { panic(err) } 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) +}