From edc2466eee987c8f8725bbeb231f4b101d53a545 Mon Sep 17 00:00:00 2001 From: jiangyong Date: Thu, 3 Jul 2025 22:54:39 +0800 Subject: [PATCH] getdepartment --- qyweixin/app_hr.go | 60 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/qyweixin/app_hr.go b/qyweixin/app_hr.go index 7256811..e16b83b 100644 --- a/qyweixin/app_hr.go +++ b/qyweixin/app_hr.go @@ -10,8 +10,10 @@ import ( ) var ( - urlQyWeixinHrGetAllField = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_fields" - urlQyWeixinHrGetStaffInfo = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_staff_info" + urlQyWeixinHrGetAllField = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_fields" + 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 { @@ -19,6 +21,13 @@ type AppHr struct { config *AppConfig } +type Department struct { + Id int64 `json:"id"` + Pid int64 `json:"pid"` + Name string `json:"name"` + Leader []string `json:"leader"` +} + type StaffInfo struct { UserName string RealName string @@ -85,6 +94,53 @@ func (h *AppHr) GetStaffInfo(userId string) (*StaffInfo, error) { 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 { valueType := cast.ToInt(fieldInfo["value_type"]) if valueType == 1 {