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

@@ -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": //部门新建