232 lines
7.0 KiB
Go
232 lines
7.0 KiB
Go
package feishu
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fusenapi/utils/curl"
|
|
"time"
|
|
)
|
|
|
|
type FeiShuApi struct {
|
|
AppId string
|
|
AppSecret string
|
|
ApiHost string
|
|
}
|
|
|
|
// 获取tenant_access_token
|
|
type GetTenantAccessTokenRsp struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
TenantAccessToken string `json:"tenant_access_token"`
|
|
Expire int `json:"expire"`
|
|
}
|
|
|
|
func (f *FeiShuApi) GetTenantAccessToken() (resp GetTenantAccessTokenRsp, err error) {
|
|
url := f.ApiHost + "/open-apis/auth/v3/tenant_access_token/internal"
|
|
header := map[string]string{"Content-Type": "application/json; charset=utf-8"}
|
|
req := map[string]interface{}{
|
|
"app_secret": f.AppSecret,
|
|
"app_id": f.AppId,
|
|
}
|
|
postData, _ := json.Marshal(req)
|
|
rsp, err := curl.ApiCall2(url, "POST", header, bytes.NewReader(postData), time.Second*15)
|
|
if err != nil {
|
|
return GetTenantAccessTokenRsp{}, err
|
|
}
|
|
if err = json.Unmarshal(rsp, &resp); err != nil {
|
|
return GetTenantAccessTokenRsp{}, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// 获取部门列表
|
|
type GetDepartmentsFeiShuRsp struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data struct {
|
|
HasMore bool `json:"has_more"`
|
|
PageToken string `json:"page_token"`
|
|
Items []DeptItem `json:"items"`
|
|
} `json:"data"`
|
|
}
|
|
type DeptItem struct {
|
|
Name string `json:"name"`
|
|
I18NName struct {
|
|
ZhCn string `json:"zh_cn"`
|
|
JaJp string `json:"ja_jp"`
|
|
EnUs string `json:"en_us"`
|
|
} `json:"i18n_name"`
|
|
ParentDepartmentId string `json:"parent_department_id"`
|
|
DepartmentId string `json:"department_id"`
|
|
OpenDepartmentId string `json:"open_department_id"`
|
|
LeaderUserId string `json:"leader_user_id"`
|
|
ChatId string `json:"chat_id"`
|
|
Order string `json:"order"`
|
|
UnitIds []string `json:"unit_ids"`
|
|
MemberCount int `json:"member_count"`
|
|
Status struct {
|
|
IsDeleted bool `json:"is_deleted"`
|
|
} `json:"status"`
|
|
}
|
|
|
|
func (f *FeiShuApi) GetDepartments(tenantAccessToken string) (resp []DeptItem, err error) {
|
|
url := f.ApiHost + "/open-apis/contact/v3/departments"
|
|
header := map[string]string{
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
"Authorization": "Bearer " + tenantAccessToken,
|
|
}
|
|
req := map[string]interface{}{
|
|
"fetch_child": true,
|
|
"page_token": "",
|
|
"page_size": 50,
|
|
"department_id": "0",
|
|
}
|
|
for {
|
|
postData, _ := json.Marshal(req)
|
|
//先不循环
|
|
requestRsp, err := curl.ApiCall2(url, "GET", header, bytes.NewReader(postData), time.Second*15)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var parseData GetDepartmentsFeiShuRsp
|
|
if err = json.Unmarshal(requestRsp, &parseData); err != nil {
|
|
return nil, err
|
|
}
|
|
resp = append(resp, parseData.Data.Items...)
|
|
if !parseData.Data.HasMore {
|
|
break
|
|
}
|
|
req["page_token"] = parseData.Data.PageToken
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// 获取部门直属用户列表
|
|
type FindUsersByDepartmentFeiShuRsp struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data struct {
|
|
HasMore bool `json:"has_more"`
|
|
PageToken string `json:"page_token"`
|
|
Items []UserItems `json:"items"`
|
|
} `json:"data"`
|
|
}
|
|
type UserItems struct {
|
|
UnionId string `json:"union_id"`
|
|
UserId string `json:"user_id"`
|
|
OpenId string `json:"open_id"`
|
|
Name string `json:"name"`
|
|
EnName string `json:"en_name"`
|
|
Nickname string `json:"nickname"`
|
|
Email string `json:"email"`
|
|
Mobile string `json:"mobile"`
|
|
MobileVisible bool `json:"mobile_visible"`
|
|
Gender int `json:"gender"`
|
|
AvatarKey string `json:"avatar_key"`
|
|
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"`
|
|
JoinTime int `json:"join_time"`
|
|
IsTenantManager bool `json:"is_tenant_manager"`
|
|
EmployeeNo string `json:"employee_no"`
|
|
EmployeeType int `json:"employee_type"`
|
|
Orders []struct {
|
|
DepartmentId string `json:"department_id"`
|
|
UserOrder int `json:"user_order"`
|
|
DepartmentOrder int `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 int `json:"type"`
|
|
} `json:"generic_user"`
|
|
} `json:"value"`
|
|
} `json:"custom_attrs"`
|
|
EnterpriseEmail string `json:"enterprise_email"`
|
|
JobTitle string `json:"job_title"`
|
|
IsFrozen bool `json:"is_frozen"`
|
|
Geo string `json:"geo"`
|
|
JobLevelId string `json:"job_level_id"`
|
|
JobFamilyId string `json:"job_family_id"`
|
|
DepartmentPath []struct {
|
|
DepartmentId string `json:"department_id"`
|
|
DepartmentName struct {
|
|
Name string `json:"name"`
|
|
I18NName struct {
|
|
ZhCn string `json:"zh_cn"`
|
|
JaJp string `json:"ja_jp"`
|
|
EnUs string `json:"en_us"`
|
|
} `json:"i18n_name"`
|
|
} `json:"department_name"`
|
|
DepartmentPath struct {
|
|
DepartmentIds []string `json:"department_ids"`
|
|
DepartmentPathName struct {
|
|
Name string `json:"name"`
|
|
I18NName struct {
|
|
ZhCn string `json:"zh_cn"`
|
|
JaJp string `json:"ja_jp"`
|
|
EnUs string `json:"en_us"`
|
|
} `json:"i18n_name"`
|
|
} `json:"department_path_name"`
|
|
} `json:"department_path"`
|
|
} `json:"department_path"`
|
|
DottedLineLeaderUserIds []string `json:"dotted_line_leader_user_ids"`
|
|
}
|
|
|
|
func (f *FeiShuApi) FindUsersByDepartment(tenantAccessToken string) (resp []UserItems, err error) {
|
|
url := f.ApiHost + "/open-apis/contact/v3/users/find_by_department"
|
|
header := map[string]string{
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
"Authorization": "Bearer " + tenantAccessToken,
|
|
}
|
|
req := map[string]interface{}{
|
|
"page_token": "",
|
|
"page_size": 50,
|
|
"department_id": "0",
|
|
}
|
|
for {
|
|
postData, _ := json.Marshal(req)
|
|
//先不循环
|
|
requestRsp, err := curl.ApiCall2(url, "GET", header, bytes.NewReader(postData), time.Second*15)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var parseData FindUsersByDepartmentFeiShuRsp
|
|
if err = json.Unmarshal(requestRsp, &parseData); err != nil {
|
|
return nil, err
|
|
}
|
|
resp = append(resp, parseData.Data.Items...)
|
|
if !parseData.Data.HasMore {
|
|
break
|
|
}
|
|
req["page_token"] = parseData.Data.PageToken
|
|
}
|
|
return resp, nil
|
|
}
|