Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add APIs for User and Department #712

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions work/addresslist/department.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@ import (
)

const (
// departmentCreateURL 创建部门
departmentCreateURL = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=%s"
// 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"
)

type (
// DepartmentCreateRequest 创建部门数据请求
DepartmentCreateRequest struct {
Name string `json:"name"`
NameEn string `json:"name_en,omitempty"`
ParentID int `json:"parentid"`
Order int `json:"order,omitempty"`
ID int `json:"id,omitempty"`
}
// DepartmentCreateResponse 创建部门数据响应
DepartmentCreateResponse struct {
util.CommonError
ID int `json:"id"`
}

// DepartmentSimpleListResponse 获取子部门ID列表响应
DepartmentSimpleListResponse struct {
util.CommonError
Expand Down Expand Up @@ -42,6 +58,27 @@ type (
}
)

// DepartmentCreate 创建部门
// see https://developer.work.weixin.qq.com/document/path/90205
func (r *Client) DepartmentCreate(req *DepartmentCreateRequest) (*DepartmentCreateResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(departmentCreateURL, accessToken), req); err != nil {
return nil, err
}
result := &DepartmentCreateResponse{}
if err = util.DecodeWithError(response, result, "DepartmentCreate"); err != nil {
return nil, err
}
return result, nil
}

// DepartmentSimpleList 获取子部门ID列表
// see https://developer.work.weixin.qq.com/document/path/95350
func (r *Client) DepartmentSimpleList(departmentID int) ([]*DepartmentID, error) {
Expand Down
124 changes: 124 additions & 0 deletions work/addresslist/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package addresslist

import (
"fmt"
"strings"

"github.com/silenceper/wechat/v2/util"
Expand All @@ -9,8 +10,12 @@ import (
const (
// userSimpleListURL 获取部门成员
userSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist"
// userCreateURL 创建成员
userCreateURL = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=%s"
// userGetURL 读取成员
userGetURL = "https://qyapi.weixin.qq.com/cgi-bin/user/get"
// userDeleteURL 删除成员
userDeleteURL = "https://qyapi.weixin.qq.com/cgi-bin/user/delete"
// userListIDURL 获取成员ID列表
userListIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/list_id"
// convertToOpenIDURL userID转openID
Expand Down Expand Up @@ -62,6 +67,91 @@ func (r *Client) UserSimpleList(departmentID int) ([]*UserList, error) {
return result.UserList, nil
}

type (
// UserCreateRequest 创建成员数据请求
UserCreateRequest struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个结构体是不是可以拆分一下

UserID string `json:"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 []struct {
Type int `json:"type"`
Name string `json:"name"`
Text struct {
Value string `json:"value"`
} `json:"text,omitempty"`
Web struct {
URL string `json:"url"`
Title string `json:"title"`
} `json:"web,omitempty"`
} `json:"attrs"`
} `json:"extattr"`
ToInvite bool `json:"to_invite"`
ExternalPosition string `json:"external_position"`
ExternalProfile struct {
ExternalCorpName string `json:"external_corp_name"`
WechatChannels struct {
Nickname string `json:"nickname"`
} `json:"wechat_channels"`
ExternalAttr []struct {
Type int `json:"type"`
Name string `json:"name"`
Text struct {
Value string `json:"value"`
} `json:"text,omitempty"`
Web struct {
URL string `json:"url"`
Title string `json:"title"`
} `json:"web,omitempty"`
Miniprogram struct {
Appid string `json:"appid"`
Pagepath string `json:"pagepath"`
Title string `json:"title"`
} `json:"miniprogram,omitempty"`
} `json:"external_attr"`
} `json:"external_profile"`
}
// UserCreateResponse 创建成员数据响应
UserCreateResponse struct {
util.CommonError
}
)

// UserCreate 创建成员
// @see https://developer.work.weixin.qq.com/document/path/90195
func (r *Client) UserCreate(req *UserCreateRequest) (*UserCreateResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(userCreateURL, accessToken), req); err != nil {
return nil, err
}
result := &UserCreateResponse{}
if err = util.DecodeWithError(response, result, "UserCreate"); err != nil {
return nil, err
}
return result, nil
}

// UserGetResponse 获取部门成员响应
type UserGetResponse struct {
util.CommonError
Expand Down Expand Up @@ -154,6 +244,40 @@ func (r *Client) UserGet(UserID string) (*UserGetResponse, error) {
return result, nil
}

type (
// UserDeleteResponse 删除成员数据响应
UserDeleteResponse struct {
util.CommonError
}
)

// UserDelete 删除成员
// @see https://developer.work.weixin.qq.com/document/path/90334
func (r *Client) UserDelete(userID string) (*UserDeleteResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.HTTPGet(strings.Join([]string{
userDeleteURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
"userid": userID,
}),
}, "?")); err != nil {
return nil, err
}
result := &UserDeleteResponse{}
if err = util.DecodeWithError(response, result, "UserDelete"); err != nil {
return nil, err
}
return result, nil
}

// UserListIDRequest 获取成员ID列表请求
type UserListIDRequest struct {
Cursor string `json:"cursor"`
Expand Down
Loading