Skip to content

Commit

Permalink
feat: 企业微信-通讯录管理,新增更新成员、更新部门、删除部门方法 (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwang1992 authored Dec 20, 2024
1 parent 35af33f commit 3bd886d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
49 changes: 48 additions & 1 deletion work/addresslist/department.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
const (
// departmentCreateURL 创建部门
departmentCreateURL = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=%s"
// departmentUpdateURL 更新部门
departmentUpdateURL = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=%s"
// departmentDeleteURL 删除部门
departmentDeleteURL = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=%s&id=%d"
// departmentSimpleListURL 获取子部门ID列表
departmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d"
// departmentListURL 获取部门列表
departmentListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s"
departmentListByIDURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s&id=%d"
// departmentGetURL 获取单个部门详情 https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=ACCESS_TOKEN&id=ID
// departmentGetURL 获取单个部门详情
departmentGetURL = "https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=%s&id=%d"
)

Expand Down Expand Up @@ -85,6 +89,49 @@ func (r *Client) DepartmentCreate(req *DepartmentCreateRequest) (*DepartmentCrea
return result, err
}

// DepartmentUpdateRequest 更新部门请求
type DepartmentUpdateRequest struct {
ID int `json:"id"`
Name string `json:"name,omitempty"`
NameEn string `json:"name_en,omitempty"`
ParentID int `json:"parentid,omitempty"`
Order int `json:"order,omitempty"`
}

// DepartmentUpdate 更新部门
// see https://developer.work.weixin.qq.com/document/path/90206
func (r *Client) DepartmentUpdate(req *DepartmentUpdateRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(departmentUpdateURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "DepartmentUpdate")
}

// DepartmentDelete 删除部门
// @see https://developer.work.weixin.qq.com/document/path/90207
func (r *Client) DepartmentDelete(departmentID int) error {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.HTTPGet(fmt.Sprintf(departmentDeleteURL, accessToken, departmentID)); err != nil {
return err
}
return util.DecodeWithCommonError(response, "DepartmentDelete")
}

// DepartmentSimpleList 获取子部门ID列表
// see https://developer.work.weixin.qq.com/document/path/95350
func (r *Client) DepartmentSimpleList(departmentID int) ([]*DepartmentID, error) {
Expand Down
47 changes: 47 additions & 0 deletions work/addresslist/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const (
userSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist"
// userCreateURL 创建成员
userCreateURL = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=%s"
// userUpdateURL 更新成员
userUpdateURL = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=%s"
// userGetURL 读取成员
userGetURL = "https://qyapi.weixin.qq.com/cgi-bin/user/get"
// userDeleteURL 删除成员
Expand Down Expand Up @@ -154,6 +156,51 @@ func (r *Client) UserCreate(req *UserCreateRequest) (*UserCreateResponse, error)
return result, err
}

// UserUpdateRequest 更新成员请求
type UserUpdateRequest struct {
UserID string `json:"userid"`
NewUserID string `json:"new_userid"`
Name string `json:"name"`
Alias string `json:"alias"`
Mobile string `json:"mobile"`
Department []int `json:"department"`
Order []int `json:"order"`
Position string `json:"position"`
Gender int `json:"gender"`
Email string `json:"email"`
BizMail string `json:"biz_mail"`
IsLeaderInDept []int `json:"is_leader_in_dept"`
DirectLeader []string `json:"direct_leader"`
Enable int `json:"enable"`
AvatarMediaid string `json:"avatar_mediaid"`
Telephone string `json:"telephone"`
Address string `json:"address"`
MainDepartment int `json:"main_department"`
Extattr struct {
Attrs []ExtraAttr `json:"attrs"`
} `json:"extattr"`
ToInvite bool `json:"to_invite"`
ExternalPosition string `json:"external_position"`
ExternalProfile ExternalProfile `json:"external_profile"`
}

// UserUpdate 更新成员
// see https://developer.work.weixin.qq.com/document/path/90197
func (r *Client) UserUpdate(req *UserUpdateRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(userUpdateURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "UserUpdate")
}

// UserGetResponse 获取部门成员响应
type UserGetResponse struct {
util.CommonError
Expand Down

0 comments on commit 3bd886d

Please sign in to comment.