From d247aded23bd04ead13927f54958961f40d0bcd2 Mon Sep 17 00:00:00 2001 From: icechen Date: Wed, 12 Jan 2022 01:44:10 +0800 Subject: [PATCH] feat: service-lark --- app/zeus/api/lark/internal/user/user.go | 31 +++++++++++ app/zeus/api/lark/main.go | 59 +++++++++++++++++++-- app/zeus/service/lark/internal/lark/im.go | 20 +++++++ app/zeus/service/lark/internal/lark/lark.go | 18 +++---- app/zeus/service/lark/internal/lark/user.go | 14 +++++ 5 files changed, 126 insertions(+), 16 deletions(-) create mode 100644 app/zeus/api/lark/internal/user/user.go create mode 100644 app/zeus/service/lark/internal/lark/im.go create mode 100644 app/zeus/service/lark/internal/lark/user.go diff --git a/app/zeus/api/lark/internal/user/user.go b/app/zeus/api/lark/internal/user/user.go new file mode 100644 index 0000000..24cc681 --- /dev/null +++ b/app/zeus/api/lark/internal/user/user.go @@ -0,0 +1,31 @@ +package user + +import ( + "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" +) + +var ErrorUser = fiber.NewError(0, "错误") + +func LoginHandle(c *fiber.Ctx) error { + var p = struct { + Code string `json:"code"` + }{} + err := c.BodyParser(&p) + if err != nil { + return err + } + + response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Login(c.Context(), &lark.LoginRequest{ + Code: p.Code, + }) + if err != nil { + return err + } + + ctxLogger.FInfo(c, "response", zap.Any("resp", response)) + return c.JSON(response) +} diff --git a/app/zeus/api/lark/main.go b/app/zeus/api/lark/main.go index 6a21d34..afbcc80 100755 --- a/app/zeus/api/lark/main.go +++ b/app/zeus/api/lark/main.go @@ -1,11 +1,60 @@ package main -import "github.com/gofiber/fiber/v2" +import ( + "encoding/json" + "git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user" + "github.com/gofiber/fiber/v2" + ctxLogger "github.com/luizsuper/ctxLoggers" +) func main() { - router := fiber.New() - router.Get("/user", func(ctx *fiber.Ctx) error { - return ctx.SendString("user") + router := fiber.New(fiber.Config{ + AppName: "api-lark", + ErrorHandler: middlewareError, + }) + //router.Use(middlewareResp) + + userRouter := router.Group("/user") + userRouter.Post("/login", user.LoginHandle) + + err := router.Listen(":8080") + if err != nil { + ctxLogger.Error(nil, err.Error()) + } +} + +func middlewareResp(c *fiber.Ctx) error { + err := c.Next() + if err != nil { + return err + } + + resp := Response{ + Code: 0, + Message: "", + Data: c.Response().Body(), + } + return c.JSON(resp) +} + +type Response struct { + Code int `json:"code"` + Message string `json:"message"` + Data json.RawMessage `json:"data"` +} + +func middlewareError(ctx *fiber.Ctx, err error) error { + code := fiber.StatusInternalServerError + msg := "" + + if e, ok := err.(*fiber.Error); ok { + code = e.Code + msg = e.Message + } + + return ctx.Status(code).JSON(Response{ + Code: code, + Message: msg, + Data: nil, }) - router.Listen(":8080") } diff --git a/app/zeus/service/lark/internal/lark/im.go b/app/zeus/service/lark/internal/lark/im.go new file mode 100644 index 0000000..9c086ed --- /dev/null +++ b/app/zeus/service/lark/internal/lark/im.go @@ -0,0 +1,20 @@ +package lark + +import ( + "context" + "github.com/larksuite/oapi-sdk-go/core" + im "github.com/larksuite/oapi-sdk-go/service/im/v1" +) + +func (l Lark) SendMessage(code string) error { + coreCtx := core.WrapContext(context.Background()) + + reqCall := l.imService.Messages.Create(coreCtx, &im.MessageCreateReqBody{ + ReceiveId: "ou_a11d2bcc7d852afbcaf37e5b3ad01f7e", + Content: `{"text":"Tom test content"}`, + MsgType: "text", + }) + reqCall.SetReceiveIdType("user_id") + _, err := reqCall.Do() + return err +} diff --git a/app/zeus/service/lark/internal/lark/lark.go b/app/zeus/service/lark/internal/lark/lark.go index 3e0252a..990b8fe 100644 --- a/app/zeus/service/lark/internal/lark/lark.go +++ b/app/zeus/service/lark/internal/lark/lark.go @@ -1,32 +1,28 @@ package lark import ( - "context" cfg "git.icechen.cn/monorepo/backend/app/zeus/service/lark/internal/config" "github.com/larksuite/oapi-sdk-go/core" "github.com/larksuite/oapi-sdk-go/core/config" authen "github.com/larksuite/oapi-sdk-go/service/authen/v1" + im "github.com/larksuite/oapi-sdk-go/service/im/v1" ) type Lark struct { - conf *config.Config + conf *config.Config + userService *authen.Service + imService *im.Service } var lark Lark func Client() Lark { - if lark.conf == nil { + if lark.conf == nil || lark.userService == nil { appSettings := core.NewInternalAppSettings( core.SetAppCredentials(cfg.Config.LarkAppID, cfg.Config.LarkAppSecret)) lark.conf = core.NewConfig(core.DomainFeiShu, appSettings, core.SetLoggerLevel(core.LoggerLevelError)) + lark.userService = authen.NewService(lark.conf) + lark.imService = im.NewService(lark.conf) } return lark } - -func (l Lark) Auth(code string) (*authen.UserAccessTokenInfo, error) { - s := authen.NewService(l.conf) - return s.Authens.AccessToken(core.WrapContext(context.Background()), &authen.AuthenAccessTokenReqBody{ - GrantType: "", - Code: code, - }).Do() -} diff --git a/app/zeus/service/lark/internal/lark/user.go b/app/zeus/service/lark/internal/lark/user.go new file mode 100644 index 0000000..6668acc --- /dev/null +++ b/app/zeus/service/lark/internal/lark/user.go @@ -0,0 +1,14 @@ +package lark + +import ( + "context" + "github.com/larksuite/oapi-sdk-go/core" + authen "github.com/larksuite/oapi-sdk-go/service/authen/v1" +) + +func (l Lark) Auth(code string) (*authen.UserAccessTokenInfo, error) { + return l.userService.Authens.AccessToken(core.WrapContext(context.Background()), &authen.AuthenAccessTokenReqBody{ + GrantType: "", + Code: code, + }).Do() +}