From 21f62035cfd9316f07a219e3288e036340eb9743 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 6 Nov 2023 15:19:14 +0800 Subject: [PATCH] fix --- model/gmodel/fs_feishu_config_gen.go | 25 +++++++++++ model/gmodel/fs_feishu_config_logic.go | 2 + .../internal/handler/webhookhandler.go | 44 +++++++++++++++++++ .../internal/logic/webhooklogic.go | 43 ++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 model/gmodel/fs_feishu_config_gen.go create mode 100644 model/gmodel/fs_feishu_config_logic.go create mode 100644 server/feishu-sync/internal/handler/webhookhandler.go create mode 100644 server/feishu-sync/internal/logic/webhooklogic.go diff --git a/model/gmodel/fs_feishu_config_gen.go b/model/gmodel/fs_feishu_config_gen.go new file mode 100644 index 00000000..8a866bb8 --- /dev/null +++ b/model/gmodel/fs_feishu_config_gen.go @@ -0,0 +1,25 @@ +package gmodel + +import ( + "gorm.io/gorm" + "time" +) + +// fs_feishu_config 飞书app配置表 +type FsFeishuConfig struct { + Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID + AppId *string `gorm:"default:'';" json:"app_id"` // app_id + AppSecret *string `gorm:"default:'';" json:"app_secret"` // app密钥 + EncryptKey *string `gorm:"default:'';" json:"encrypt_key"` // + TicketMetdata *string `gorm:"default:'';" json:"ticket_metdata"` // ticket元数据 + Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // + Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // +} +type FsFeishuConfigModel struct { + db *gorm.DB + name string +} + +func NewFsFeishuConfigModel(db *gorm.DB) *FsFeishuConfigModel { + return &FsFeishuConfigModel{db: db, name: "fs_feishu_config"} +} diff --git a/model/gmodel/fs_feishu_config_logic.go b/model/gmodel/fs_feishu_config_logic.go new file mode 100644 index 00000000..e68225aa --- /dev/null +++ b/model/gmodel/fs_feishu_config_logic.go @@ -0,0 +1,2 @@ +package gmodel +// TODO: 使用model的属性做你想做的 \ No newline at end of file diff --git a/server/feishu-sync/internal/handler/webhookhandler.go b/server/feishu-sync/internal/handler/webhookhandler.go new file mode 100644 index 00000000..cfddae9d --- /dev/null +++ b/server/feishu-sync/internal/handler/webhookhandler.go @@ -0,0 +1,44 @@ +package handler + +import ( + "encoding/json" + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/feishu-sync/internal/logic" + "fusenapi/server/feishu-sync/internal/svc" + "fusenapi/server/feishu-sync/internal/types" +) + +func WebhookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.WebhookReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + //验证连接 + if req.Type == "url_verification" { + challengeRsp := map[string]string{ + "challenge": req.Challenge, + } + b, _ := json.Marshal(challengeRsp) + w.Write(b) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewWebhookLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.Webhook(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/feishu-sync/internal/logic/webhooklogic.go b/server/feishu-sync/internal/logic/webhooklogic.go new file mode 100644 index 00000000..3e85dedf --- /dev/null +++ b/server/feishu-sync/internal/logic/webhooklogic.go @@ -0,0 +1,43 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/feishu-sync/internal/svc" + "fusenapi/server/feishu-sync/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type WebhookLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebhookLogic { + return &WebhookLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *WebhookLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *WebhookLogic) Webhook(req *types.WebhookReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *WebhookLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// }