Merge remote-tracking branch 'origin/master'

master
liuhaotian 2022-01-14 04:10:42 +08:00
commit ea827d8b54
5 changed files with 46 additions and 7 deletions

View File

@ -1,18 +1,17 @@
package user package user
import ( 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/api"
"git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark"
"git.icechen.cn/monorepo/backend/pkg/rpc" "git.icechen.cn/monorepo/backend/pkg/rpc"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
) )
// LoginHandle 管理员登录
func LoginHandle(c *fiber.Ctx) error { func LoginHandle(c *fiber.Ctx) error {
var p = struct { var p = struct {
Code string `json:"code" validate:"required"` Code string `json:"code" validate:"required"`
Email string `json:"email" label:"邮箱" validate:"email"`
}{} }{}
err := api.Bind(c, &p) err := api.Bind(c, &p)
if err != nil { if err != nil {
@ -26,6 +25,11 @@ func LoginHandle(c *fiber.Ctx) error {
return err return err
} }
ctxLogger.FInfo(c, "response", zap.Any("resp", response))
return c.JSON(response) return c.JSON(response)
} }
// InfoHandle 管理员信息
func InfoHandle(c *fiber.Ctx) error {
userInfo := admin.GetUserInfo(c)
return c.JSON(userInfo)
}

View File

@ -2,6 +2,7 @@ package main
import ( import (
"git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user" "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" "git.icechen.cn/monorepo/backend/pkg/api"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers" ctxLogger "github.com/luizsuper/ctxLoggers"
@ -13,9 +14,11 @@ func main() {
ErrorHandler: api.FiberMiddleWareErrHandler, ErrorHandler: api.FiberMiddleWareErrHandler,
}) })
router.Use(api.FibberMiddleWareRespHandler) router.Use(api.FibberMiddleWareRespHandler)
router.Use(api.FibberUserCtxWithTraceHandler)
userRouter := router.Group("/user") userRouter := router.Group("/user")
userRouter.Post("/login", user.LoginHandle) userRouter.Post("/login", user.LoginHandle)
userRouter.Post("/info", admin.MiddlewareLarkAdmin, user.InfoHandle)
err := router.Listen(":8080") err := router.Listen(":8080")
if err != nil { if err != nil {

View File

@ -8,7 +8,7 @@ import (
func (l Lark) Auth(code string) (*authen.UserAccessTokenInfo, error) { func (l Lark) Auth(code string) (*authen.UserAccessTokenInfo, error) {
return l.userService.Authens.AccessToken(core.WrapContext(context.Background()), &authen.AuthenAccessTokenReqBody{ return l.userService.Authens.AccessToken(core.WrapContext(context.Background()), &authen.AuthenAccessTokenReqBody{
GrantType: "", GrantType: "authorization_code",
Code: code, Code: code,
}).Do() }).Do()
} }

View File

@ -5,7 +5,7 @@ import (
) )
func init() { func init() {
err := orm.DB.AutoMigrate(&UserInfo{}) err := orm.DB.AutoMigrate(&UserInfo{}, &UserToken{})
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -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)
}