This commit is contained in:
laodaming 2023-11-07 16:26:29 +08:00
parent 3429f61ebc
commit c61043ad65
4 changed files with 63 additions and 3 deletions

View File

@ -0,0 +1,26 @@
package gmodel
import (
"gorm.io/gorm"
"time"
)
// fs_feishu_webhook_log 飞书webhook记录表
type FsFeishuWebhookLog struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID
AppId *string `gorm:"default:'';" json:"app_id"` // app_id
EventId *string `gorm:"index;default:'';" json:"event_id"` // 事件唯一id
EventType *string `gorm:"default:'';" json:"event_type"` // 事件名
HttpHeader *string `gorm:"default:'';" json:"http_header"` // http响应头信息
HttpBody *string `gorm:"default:'';" json:"http_body"` // http body信息
MsgCtime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"msg_ctime"` //
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` //
}
type FsFeishuWebhookLogModel struct {
db *gorm.DB
name string
}
func NewFsFeishuWebhookLogModel(db *gorm.DB) *FsFeishuWebhookLogModel {
return &FsFeishuWebhookLogModel{db: db, name: "fs_feishu_webhook_log"}
}

View File

@ -0,0 +1,7 @@
package gmodel
// TODO: 使用model的属性做你想做的
func (w *FsFeishuWebhookLogModel) Create(data *FsFeishuWebhookLog) error {
return w.db.Model(&FsFeishuWebhookLog{}).Create(&data).Error
}

View File

@ -50,6 +50,7 @@ type AllModelsGen struct {
FsFactoryShipTmp *FsFactoryShipTmpModel // fs_factory_ship_tmp
FsFaq *FsFaqModel // fs_faq 常见问题
FsFeishuConfig *FsFeishuConfigModel // fs_feishu_config 飞书app配置表
FsFeishuWebhookLog *FsFeishuWebhookLogModel // fs_feishu_webhook_log 飞书webhook记录表
FsFont *FsFontModel // fs_font 字体配置
FsGerent *FsGerentModel // fs_gerent 管理员表
FsGuest *FsGuestModel // fs_guest 游客表
@ -169,6 +170,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
FsFactoryShipTmp: NewFsFactoryShipTmpModel(gdb),
FsFaq: NewFsFaqModel(gdb),
FsFeishuConfig: NewFsFeishuConfigModel(gdb),
FsFeishuWebhookLog: NewFsFeishuWebhookLogModel(gdb),
FsFont: NewFsFontModel(gdb),
FsGerent: NewFsGerentModel(gdb),
FsGuest: NewFsGuestModel(gdb),

View File

@ -3,11 +3,14 @@ package logic
import (
"context"
"encoding/json"
"fusenapi/model/gmodel"
"fusenapi/server/feishu-sync/internal/svc"
"fusenapi/utils/feishu"
"github.com/zeromicro/go-zero/core/logx"
"io"
"net/http"
"strconv"
"time"
)
type WebhookLogic struct {
@ -90,17 +93,39 @@ func (l *WebhookLogic) Webhook(w http.ResponseWriter, r *http.Request) {
w.Write(b)
return
}
headerByte, err := json.Marshal(webhookMsg.Header)
bodyHeaderByte, err := json.Marshal(webhookMsg.Header)
if err != nil {
logx.Error("序列化请求体header失败:", err)
return
}
var msgHeader BaseWebhookMsgHeaderType
if err = json.Unmarshal(headerByte, &msgHeader); err != nil {
if err = json.Unmarshal(bodyHeaderByte, &msgHeader); err != nil {
logx.Error("反序列化请求体中的header失败", err)
return
}
logx.Info("触发webhook:", msgHeader.EventType, "数据:", string(realMsgBytes))
httpHeaderBytes, _ := json.Marshal(r.Header)
httpHeaderStr := string(httpHeaderBytes)
httpBodyStr := string(bodyBytes)
feiShuMsgCreateTimeInt64, err := strconv.ParseInt(msgHeader.CreateTime, 10, 64)
if err != nil {
logx.Error("解析消息时间错误:", err)
return
}
feiShuMsgCreateTime := time.Unix(feiShuMsgCreateTimeInt64, 0)
now := time.Now().UTC()
//把事件加入日志
err = l.svcCtx.AllModels.FsFeishuWebhookLog.Create(&gmodel.FsFeishuWebhookLog{
AppId: &msgHeader.AppId,
EventId: &msgHeader.EventId,
EventType: &msgHeader.EventType,
HttpHeader: &httpHeaderStr,
HttpBody: &httpBodyStr,
MsgCtime: &feiShuMsgCreateTime,
Ctime: &now,
})
if err != nil {
logx.Error("保存webhook消息日志失败:", err)
}
switch msgHeader.EventType {
case "contact.custom_attr_event.updated_v3": //成员字段管理属性变更事件
case "contact.department.created_v3": //部门新建