fix
This commit is contained in:
parent
be7823cfba
commit
975469e190
|
@ -14,30 +14,8 @@ import (
|
||||||
|
|
||||||
func WebhookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
func WebhookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
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)
|
l := logic.NewWebhookLogic(r.Context(), svcCtx)
|
||||||
|
l.Webhook(w, r)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,12 @@ package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fusenapi/utils/auth"
|
"io"
|
||||||
"fusenapi/utils/basic"
|
"net/http"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"fusenapi/server/feishu-sync/internal/svc"
|
"fusenapi/server/feishu-sync/internal/svc"
|
||||||
"fusenapi/server/feishu-sync/internal/types"
|
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,10 +25,14 @@ func NewWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebhookLo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理进入前逻辑w,r
|
type WebhookMsg struct {
|
||||||
// func (l *WebhookLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
Type string `json:"type"`
|
||||||
// }
|
Challenge string `json:"challenge"`
|
||||||
// webhook消息事件基础信息
|
Header map[string]interface{} `json:"header"`
|
||||||
|
Event map[string]interface{} `json:"event"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// webhook消息事件header(body参数)基础信息
|
||||||
type BaseWebhookMsgHeaderType struct {
|
type BaseWebhookMsgHeaderType struct {
|
||||||
EventId string `json:"event_id"` //事件id(可作为消息唯一性确认)
|
EventId string `json:"event_id"` //事件id(可作为消息唯一性确认)
|
||||||
EventType string `json:"event_type"` //事件类型
|
EventType string `json:"event_type"` //事件类型
|
||||||
|
@ -40,16 +42,39 @@ type BaseWebhookMsgHeaderType struct {
|
||||||
TenantKey string `json:"tenant_key"` //租户key
|
TenantKey string `json:"tenant_key"` //租户key
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *WebhookLogic) Webhook(req *types.WebhookReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
func (l *WebhookLogic) Webhook(w http.ResponseWriter, r *http.Request) {
|
||||||
logx.Info("收到事件:", req)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
var msgHeader BaseWebhookMsgHeaderType
|
if err != nil {
|
||||||
if req.Event["header"] == nil {
|
logx.Error("读取请求body失败", err)
|
||||||
logx.Error("invalid request")
|
return
|
||||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid request")
|
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal([]byte(req.Event["header"].(string)), &msgHeader); err != nil {
|
defer r.Body.Close()
|
||||||
logx.Error(err)
|
//如果只是验证http连接的消息
|
||||||
return resp.SetStatusAddMessage(basic.CodeJsonErr, "failed to parse params")
|
var webhookMsg WebhookMsg
|
||||||
|
if err = json.Unmarshal(bodyBytes, &webhookMsg); err != nil {
|
||||||
|
logx.Error("反序列化请求body失败", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logx.Info("收到消息:", webhookMsg)
|
||||||
|
//验证连接(直接返回)
|
||||||
|
if webhookMsg.Type == "url_verification" {
|
||||||
|
challengeRsp := map[string]string{
|
||||||
|
"challenge": webhookMsg.Challenge,
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(challengeRsp)
|
||||||
|
w.Write(b)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
headerByte, err := json.Marshal(webhookMsg.Header)
|
||||||
|
if err != nil {
|
||||||
|
logx.Error("序列化请求体header失败:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var msgHeader BaseWebhookMsgHeaderType
|
||||||
|
if err = json.Unmarshal(headerByte, &msgHeader); err != nil {
|
||||||
|
logx.Error("反序列化请求体中的header失败", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
switch msgHeader.EventType {
|
switch msgHeader.EventType {
|
||||||
case "contact.custom_attr_event.updated_v3": //成员字段管理属性变更事件
|
case "contact.custom_attr_event.updated_v3": //成员字段管理属性变更事件
|
||||||
|
@ -67,7 +92,7 @@ func (l *WebhookLogic) Webhook(req *types.WebhookReq, userinfo *auth.UserInfo) (
|
||||||
case "contact.user.updated_v3": //员工信息变化
|
case "contact.user.updated_v3": //员工信息变化
|
||||||
|
|
||||||
}
|
}
|
||||||
return resp.SetStatus(basic.CodeOK)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||||
|
|
|
@ -5,14 +5,6 @@ import (
|
||||||
"fusenapi/utils/basic"
|
"fusenapi/utils/basic"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebhookReq struct {
|
|
||||||
Schema string `json:"schema,optional"`
|
|
||||||
Header map[string]interface{} `json:"header,optional"`
|
|
||||||
Event map[string]interface{} `json:"event,optional"`
|
|
||||||
Challenge string `json:"challenge,optional"` //设置飞书通知接口验证用
|
|
||||||
Type string `json:"type,optional"` //设置飞书通知接口验证用
|
|
||||||
}
|
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,13 +12,5 @@ import "basic.api"
|
||||||
service feishu-sync {
|
service feishu-sync {
|
||||||
//飞书ticket webhook事件接口
|
//飞书ticket webhook事件接口
|
||||||
@handler WebhookHandler
|
@handler WebhookHandler
|
||||||
post /api/feishu/webhook(WebhookReq) returns (response);
|
post /api/feishu/webhook(request) returns (response);
|
||||||
}
|
|
||||||
|
|
||||||
type WebhookReq {
|
|
||||||
Schema string `json:"schema,optional"`
|
|
||||||
Header map[string]interface{} `json:"header,optional"`
|
|
||||||
Event map[string]interface{} `json:"event,optional"`
|
|
||||||
Challenge string `json:"challenge,optional"` //设置飞书通知接口验证用
|
|
||||||
Type string `json:"type,optional"` //设置飞书通知接口验证用
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user