feat: service-lark
continuous-integration/drone/push Build is passing Details

master
icechen 2022-01-12 01:44:10 +08:00
parent c048909a9d
commit d247aded23
5 changed files with 126 additions and 16 deletions

View File

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

View File

@ -1,11 +1,60 @@
package main 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() { func main() {
router := fiber.New() router := fiber.New(fiber.Config{
router.Get("/user", func(ctx *fiber.Ctx) error { AppName: "api-lark",
return ctx.SendString("user") 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")
} }

View File

@ -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":"<at user_id="ou_a11d2bcc7d852afbcaf37e5b3ad01f7e">Tom</at> test content"}`,
MsgType: "text",
})
reqCall.SetReceiveIdType("user_id")
_, err := reqCall.Do()
return err
}

View File

@ -1,32 +1,28 @@
package lark package lark
import ( import (
"context"
cfg "git.icechen.cn/monorepo/backend/app/zeus/service/lark/internal/config" 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"
"github.com/larksuite/oapi-sdk-go/core/config" "github.com/larksuite/oapi-sdk-go/core/config"
authen "github.com/larksuite/oapi-sdk-go/service/authen/v1" authen "github.com/larksuite/oapi-sdk-go/service/authen/v1"
im "github.com/larksuite/oapi-sdk-go/service/im/v1"
) )
type Lark struct { type Lark struct {
conf *config.Config conf *config.Config
userService *authen.Service
imService *im.Service
} }
var lark Lark var lark Lark
func Client() Lark { func Client() Lark {
if lark.conf == nil { if lark.conf == nil || lark.userService == nil {
appSettings := core.NewInternalAppSettings( appSettings := core.NewInternalAppSettings(
core.SetAppCredentials(cfg.Config.LarkAppID, cfg.Config.LarkAppSecret)) core.SetAppCredentials(cfg.Config.LarkAppID, cfg.Config.LarkAppSecret))
lark.conf = core.NewConfig(core.DomainFeiShu, appSettings, core.SetLoggerLevel(core.LoggerLevelError)) 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 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()
}

View File

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