Compare commits

...

1 Commits

Author SHA1 Message Date
jiangyong edc2466eee getdepartment 2025-07-03 22:54:39 +08:00
1 changed files with 58 additions and 2 deletions

View File

@ -10,8 +10,10 @@ import (
) )
var ( var (
urlQyWeixinHrGetAllField = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_fields" urlQyWeixinHrGetAllField = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_fields"
urlQyWeixinHrGetStaffInfo = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_staff_info" urlQyWeixinHrGetStaffInfo = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_staff_info"
urlQyWeixinHrGetDepartment = "https://qyapi.weixin.qq.com/cgi-bin/department/list"
urlQyWeixinHrGetDepartmentUser = "https://qyapi.weixin.qq.com/cgi-bin/user/list"
) )
type AppHr struct { type AppHr struct {
@ -19,6 +21,13 @@ type AppHr struct {
config *AppConfig config *AppConfig
} }
type Department struct {
Id int64 `json:"id"`
Pid int64 `json:"pid"`
Name string `json:"name"`
Leader []string `json:"leader"`
}
type StaffInfo struct { type StaffInfo struct {
UserName string UserName string
RealName string RealName string
@ -85,6 +94,53 @@ func (h *AppHr) GetStaffInfo(userId string) (*StaffInfo, error) {
return staff, nil return staff, nil
} }
func (h *AppHr) GetDepartment(id int64) ([]*Department, error) {
reqUrl := fmt.Sprintf("%s?access_token=%s&id=%d", urlQyWeixinHrGetDepartment, h.GetToken(), id)
rspBody, err := util.HttpGet(reqUrl, nil)
if err != nil {
return nil, err
}
resp, err := h.GetResult(rspBody)
if err != nil {
return nil, err
}
result := make([]*Department, 0)
departments := cast.ToSlice(resp["department"])
for _, dd := range departments {
d := cast.ToStringMap(dd)
r := new(Department)
r.Name = cast.ToString(d["name"])
r.Leader = cast.ToStringSlice(d["department_leader"])
r.Id = cast.ToInt64(d["id"])
r.Pid = cast.ToInt64(d["parentid"])
result = append(result, r)
}
return result, nil
}
func (h *AppHr) GetDepartmentUserId(id int64) ([]string, error) {
reqUrl := fmt.Sprintf("%s?access_token=%s&department_id=%d", urlQyWeixinHrGetDepartmentUser, h.GetToken(), id)
rspBody, err := util.HttpGet(reqUrl, nil)
if err != nil {
return nil, err
}
resp, err := h.GetResult(rspBody)
if err != nil {
return nil, err
}
result := make([]string, 0)
userlist := cast.ToSlice(resp["userlist"])
for _, dd := range userlist {
d := cast.ToStringMap(dd)
result = append(result, cast.ToString(d["userid"]))
}
return result, nil
}
func (h *AppHr) getFieldValue(fieldInfo map[string]interface{}) string { func (h *AppHr) getFieldValue(fieldInfo map[string]interface{}) string {
valueType := cast.ToInt(fieldInfo["value_type"]) valueType := cast.ToInt(fieldInfo["value_type"])
if valueType == 1 { if valueType == 1 {