generated from pkg/go-template
Merge remote-tracking branch 'origin/master'
commit
ea827d8b54
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
err := orm.DB.AutoMigrate(&UserInfo{})
|
||||
err := orm.DB.AutoMigrate(&UserInfo{}, &UserToken{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue