2022-06-18 17:09:14 +08:00
|
|
|
package wechat
|
|
|
|
|
|
|
|
import (
|
2022-07-01 12:35:02 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-06-18 17:09:14 +08:00
|
|
|
|
2022-07-01 12:35:02 +08:00
|
|
|
"github.com/eryajf/go-ldap-admin/config"
|
2022-07-15 17:56:45 +08:00
|
|
|
"github.com/eryajf/go-ldap-admin/public/tools"
|
2022-06-18 17:09:14 +08:00
|
|
|
"github.com/wenerme/go-wecom/wecom"
|
|
|
|
)
|
|
|
|
|
2022-06-22 10:27:52 +08:00
|
|
|
// 官方文档: https://developer.work.weixin.qq.com/document/path/90208
|
2022-06-18 17:09:14 +08:00
|
|
|
// GetAllDepts 获取所有部门
|
2022-07-01 12:35:02 +08:00
|
|
|
func GetAllDepts() (ret []map[string]interface{}, err error) {
|
2022-06-18 17:09:14 +08:00
|
|
|
depts, err := InitWeComClient().ListDepartment(
|
|
|
|
&wecom.ListDepartmentRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-01 12:35:02 +08:00
|
|
|
for _, dept := range depts.Department {
|
|
|
|
ele := make(map[string]interface{})
|
|
|
|
ele["name"] = dept.Name
|
2022-07-15 17:56:45 +08:00
|
|
|
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
|
2022-07-01 12:35:02 +08:00
|
|
|
ele["id"] = dept.ID
|
|
|
|
ele["name_en"] = dept.NameEn
|
|
|
|
ele["parentid"] = dept.ParentID
|
|
|
|
ret = append(ret, ele)
|
|
|
|
}
|
|
|
|
return ret, nil
|
2022-06-18 17:09:14 +08:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:27:52 +08:00
|
|
|
// 官方文档: https://developer.work.weixin.qq.com/document/path/90201
|
2022-06-21 20:50:38 +08:00
|
|
|
// GetAllUsers 获取所有员工信息
|
2022-07-01 12:35:02 +08:00
|
|
|
func GetAllUsers() (ret []map[string]interface{}, err error) {
|
2022-06-18 17:09:14 +08:00
|
|
|
depts, err := GetAllDepts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, dept := range depts {
|
|
|
|
users, err := InitWeComClient().ListUser(
|
|
|
|
&wecom.ListUserRequest{
|
2022-07-01 12:35:02 +08:00
|
|
|
DepartmentID: fmt.Sprintf("%d", dept["id"].(int)),
|
2022-06-18 17:09:14 +08:00
|
|
|
FetchChild: "1",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-01 12:35:02 +08:00
|
|
|
for _, user := range users.UserList {
|
|
|
|
ele := make(map[string]interface{})
|
|
|
|
ele["name"] = user.Name
|
2022-07-15 17:56:45 +08:00
|
|
|
ele["custom_name_pinyin"] = tools.ConvertToPinYin(user.Name)
|
2022-07-01 12:35:02 +08:00
|
|
|
ele["userid"] = user.UserID
|
|
|
|
ele["mobile"] = user.Mobile
|
|
|
|
ele["position"] = user.Position
|
|
|
|
ele["gender"] = user.Gender
|
|
|
|
ele["email"] = user.Email
|
2022-07-04 17:35:20 +08:00
|
|
|
if user.Email != "" {
|
|
|
|
ele["custom_nickname_email"] = strings.Split(user.Email, "@")[0]
|
|
|
|
}
|
2022-07-01 12:35:02 +08:00
|
|
|
ele["biz_email"] = user.BizMail
|
2022-07-04 17:35:20 +08:00
|
|
|
if user.BizMail != "" {
|
|
|
|
ele["custom_nickname_biz_email"] = strings.Split(user.BizMail, "@")[0]
|
|
|
|
}
|
2022-07-01 12:35:02 +08:00
|
|
|
ele["avatar"] = user.Avatar
|
|
|
|
ele["telephone"] = user.Telephone
|
|
|
|
ele["alias"] = user.Alias
|
|
|
|
ele["external_position"] = user.ExternalPosition
|
|
|
|
ele["address"] = user.Address
|
|
|
|
ele["open_userid"] = user.OpenUserID
|
|
|
|
ele["main_department"] = user.MainDepartment
|
|
|
|
ele["english_name"] = user.EnglishName
|
|
|
|
// 部门ids
|
|
|
|
var sourceDeptIds []string
|
|
|
|
for _, deptId := range user.Department {
|
|
|
|
sourceDeptIds = append(sourceDeptIds, fmt.Sprintf("%s_%d", config.Conf.FeiShu.Flag, deptId))
|
|
|
|
}
|
|
|
|
ele["department_ids"] = sourceDeptIds
|
|
|
|
ret = append(ret, ele)
|
|
|
|
}
|
2022-06-18 17:09:14 +08:00
|
|
|
}
|
2022-07-01 12:35:02 +08:00
|
|
|
return ret, nil
|
2022-06-18 17:09:14 +08:00
|
|
|
}
|