fix
This commit is contained in:
144
server/feishu-sync/internal/logic/user_webhook.go
Normal file
144
server/feishu-sync/internal/logic/user_webhook.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fusenapi/model/gmodel"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserWebhookMsg struct {
|
||||
Schema string `json:"schema"`
|
||||
Header struct {
|
||||
EventId string `json:"event_id"`
|
||||
EventType string `json:"event_type"`
|
||||
CreateTime string `json:"create_time"`
|
||||
Token string `json:"token"`
|
||||
AppId string `json:"app_id"`
|
||||
TenantKey string `json:"tenant_key"`
|
||||
} `json:"header"`
|
||||
Event struct {
|
||||
Object struct {
|
||||
OpenId string `json:"open_id"`
|
||||
UnionId string `json:"union_id"`
|
||||
UserId string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
EnName string `json:"en_name"`
|
||||
Nickname string `json:"nickname"`
|
||||
Email string `json:"email"`
|
||||
EnterpriseEmail string `json:"enterprise_email"`
|
||||
JobTitle string `json:"job_title"`
|
||||
Mobile string `json:"mobile"`
|
||||
Gender int64 `json:"gender"`
|
||||
Avatar struct {
|
||||
Avatar72 string `json:"avatar_72"`
|
||||
Avatar240 string `json:"avatar_240"`
|
||||
Avatar640 string `json:"avatar_640"`
|
||||
AvatarOrigin string `json:"avatar_origin"`
|
||||
} `json:"avatar"`
|
||||
Status struct {
|
||||
IsFrozen bool `json:"is_frozen"`
|
||||
IsResigned bool `json:"is_resigned"`
|
||||
IsActivated bool `json:"is_activated"`
|
||||
IsExited bool `json:"is_exited"`
|
||||
IsUnjoin bool `json:"is_unjoin"`
|
||||
} `json:"status"`
|
||||
DepartmentIds []string `json:"department_ids"`
|
||||
LeaderUserId string `json:"leader_user_id"`
|
||||
City string `json:"city"`
|
||||
Country string `json:"country"`
|
||||
WorkStation string `json:"work_station"`
|
||||
Joint64ime int64 `json:"join_time"`
|
||||
EmployeeNo string `json:"employee_no"`
|
||||
EmployeeType int64 `json:"employee_type"`
|
||||
Orders []struct {
|
||||
DepartmentId string `json:"department_id"`
|
||||
UserOrder int64 `json:"user_order"`
|
||||
DepartmentOrder int64 `json:"department_order"`
|
||||
IsPrimaryDept bool `json:"is_primary_dept"`
|
||||
} `json:"orders"`
|
||||
CustomAttrs []struct {
|
||||
Type string `json:"type"`
|
||||
Id string `json:"id"`
|
||||
Value struct {
|
||||
Text string `json:"text"`
|
||||
Url string `json:"url"`
|
||||
PcUrl string `json:"pc_url"`
|
||||
OptionId string `json:"option_id"`
|
||||
OptionValue string `json:"option_value"`
|
||||
Name string `json:"name"`
|
||||
PictureUrl string `json:"picture_url"`
|
||||
GenericUser struct {
|
||||
Id string `json:"id"`
|
||||
Type int64 `json:"type"`
|
||||
} `json:"generic_user"`
|
||||
} `json:"value"`
|
||||
} `json:"custom_attrs"`
|
||||
JobLevelId string `json:"job_level_id"`
|
||||
JobFamilyId string `json:"job_family_id"`
|
||||
DottedLineLeaderUserIds []string `json:"dotted_line_leader_user_ids"`
|
||||
} `json:"object"`
|
||||
} `json:"event"`
|
||||
}
|
||||
|
||||
// 员工增删改信息
|
||||
func (l *WebhookLogic) OnUserChange(data []byte) error {
|
||||
var msg UserWebhookMsg
|
||||
if err := json.Unmarshal(data, &msg); err != nil {
|
||||
return err
|
||||
}
|
||||
avatar, _ := json.Marshal(msg.Event.Object.Avatar)
|
||||
isFrozen := int64(0)
|
||||
if msg.Event.Object.Status.IsFrozen {
|
||||
isFrozen = 1
|
||||
}
|
||||
isResigned := int64(0)
|
||||
if msg.Event.Object.Status.IsResigned {
|
||||
isResigned = 1
|
||||
}
|
||||
isActivated := int64(0)
|
||||
if msg.Event.Object.Status.IsActivated {
|
||||
isActivated = 1
|
||||
}
|
||||
isExited := int64(0)
|
||||
if msg.Event.Object.Status.IsExited {
|
||||
isExited = 1
|
||||
}
|
||||
isUnjoin := int64(0)
|
||||
if msg.Event.Object.Status.IsUnjoin {
|
||||
isUnjoin = 1
|
||||
}
|
||||
departmentIds, _ := json.Marshal(msg.Event.Object.DepartmentIds)
|
||||
orders, _ := json.Marshal(msg.Event.Object.Orders)
|
||||
feiShuMsgCreateTimeInt64, err := strconv.ParseInt(msg.Header.CreateTime, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
feiShuMsgCreateTime := time.UnixMilli(feiShuMsgCreateTimeInt64)
|
||||
return l.svcCtx.AllModels.FsFeishuUser.CreateOrUpdate(l.ctx, msg.Header.AppId, msg.Event.Object.OpenId, &gmodel.FsFeishuUser{
|
||||
AppId: &msg.Header.AppId,
|
||||
OpenId: &msg.Event.Object.OpenId,
|
||||
UnionId: &msg.Event.Object.UnionId,
|
||||
Name: &msg.Event.Object.Name,
|
||||
EnName: &msg.Event.Object.EnName,
|
||||
Nickname: &msg.Event.Object.Nickname,
|
||||
Email: &msg.Event.Object.Email,
|
||||
EnterpriseEmail: &msg.Event.Object.EnterpriseEmail,
|
||||
JobTitle: &msg.Event.Object.JobTitle,
|
||||
Mobile: &msg.Event.Object.Mobile,
|
||||
Gender: &msg.Event.Object.Gender,
|
||||
Avatar: &avatar,
|
||||
IsFrozen: &isFrozen,
|
||||
IsResigned: &isResigned,
|
||||
IsActivated: &isActivated,
|
||||
IsExited: &isExited,
|
||||
IsUnjoin: &isUnjoin,
|
||||
DepartmentIds: &departmentIds,
|
||||
WorkStation: &msg.Event.Object.WorkStation,
|
||||
EmployeeNo: &msg.Event.Object.EmployeeNo,
|
||||
EmployeeType: &msg.Event.Object.EmployeeType,
|
||||
Orders: &orders,
|
||||
Ctime: &feiShuMsgCreateTime,
|
||||
Utime: &feiShuMsgCreateTime,
|
||||
})
|
||||
}
|
||||
@@ -34,7 +34,6 @@ type WebhookMsg struct {
|
||||
Type string `json:"type"`
|
||||
Challenge string `json:"challenge"`
|
||||
Header map[string]interface{} `json:"header"`
|
||||
Event map[string]interface{} `json:"event"`
|
||||
}
|
||||
|
||||
// webhook消息事件header(body参数)基础信息
|
||||
@@ -115,7 +114,7 @@ func (l *WebhookLogic) Webhook(w http.ResponseWriter, r *http.Request) {
|
||||
feiShuMsgCreateTime := time.UnixMilli(feiShuMsgCreateTimeInt64)
|
||||
now := time.Now().UTC()
|
||||
//把事件加入日志
|
||||
err = l.svcCtx.AllModels.FsFeishuWebhookLog.Create(&gmodel.FsFeishuWebhookLog{
|
||||
err = l.svcCtx.AllModels.FsFeishuWebhookLog.Create(l.ctx, &gmodel.FsFeishuWebhookLog{
|
||||
AppId: &msgHeader.AppId,
|
||||
EventId: &msgHeader.EventId,
|
||||
EventType: &msgHeader.EventType,
|
||||
@@ -138,9 +137,14 @@ func (l *WebhookLogic) Webhook(w http.ResponseWriter, r *http.Request) {
|
||||
case "contact.employee_type_enum.deleted_v3": //删除人员类型事件
|
||||
case "contact.employee_type_enum.updated_v3": //修改人员类型名称事件
|
||||
case "contact.user.created_v3": //员工入职
|
||||
err = l.OnUserChange(realMsgBytes)
|
||||
case "contact.user.deleted_v3": //员工离职
|
||||
err = l.OnUserChange(realMsgBytes)
|
||||
case "contact.user.updated_v3": //员工信息变化
|
||||
|
||||
err = l.OnUserChange(realMsgBytes)
|
||||
}
|
||||
if err != nil {
|
||||
logx.Error("处理事件错误:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user