77 lines
2.7 KiB
Go
77 lines
2.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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) {
|
|
// }
|
|
// webhook消息事件基础信息
|
|
type BaseWebhookMsgHeaderType struct {
|
|
EventId string `json:"event_id"` //事件id(可作为消息唯一性确认)
|
|
EventType string `json:"event_type"` //事件类型
|
|
CreateTime string `json:"create_time"` //创建时间
|
|
Token string `json:"token"` //事件token
|
|
AppId string `json:"app_id"` //app id
|
|
TenantKey string `json:"tenant_key"` //租户key
|
|
}
|
|
|
|
func (l *WebhookLogic) Webhook(req *types.WebhookReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
var msgHeader BaseWebhookMsgHeaderType
|
|
if req.Event["header"] == nil {
|
|
logx.Error("invalid request")
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid request")
|
|
}
|
|
if err := json.Unmarshal([]byte(req.Event["header"].(string)), &msgHeader); err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusAddMessage(basic.CodeJsonErr, "failed to parse params")
|
|
}
|
|
logx.Info("收到事件:" + msgHeader.EventType)
|
|
switch msgHeader.EventType {
|
|
case "contact.custom_attr_event.updated_v3": //成员字段管理属性变更事件
|
|
case "contact.department.created_v3": //部门新建
|
|
case "contact.department.deleted_v3": //部门删除
|
|
case "contact.department.updated_v3": //部门信息变化
|
|
case "contact.employee_type_enum.actived_v3": //启动人员类型事件
|
|
case "contact.employee_type_enum.created_v3": //新建人员类型事件
|
|
case "contact.employee_type_enum.deactivated_v3": //停用人员类型事件
|
|
case "contact.employee_type_enum.deleted_v3": //删除人员类型事件
|
|
case "contact.employee_type_enum.updated_v3": //修改人员类型名称事件
|
|
case "contact.scope.updated_v3": //通讯录范围权限被更新
|
|
case "contact.user.created_v3": //员工入职
|
|
case "contact.user.deleted_v3": //员工离职
|
|
case "contact.user.updated_v3": //员工信息变化
|
|
|
|
}
|
|
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)
|
|
// }
|