1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

feat: Add APIs for User and Department (#712)

This commit is contained in:
Feng
2023-08-25 18:27:30 +08:00
committed by GitHub
parent cc201fcece
commit da80430065
2 changed files with 168 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ 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 获取部门列表
@@ -14,6 +16,20 @@ const (
)
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
@@ -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) {

View File

@@ -1,6 +1,7 @@
package addresslist
import (
"fmt"
"strings"
"github.com/silenceper/wechat/v2/util"
@@ -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
@@ -62,6 +67,98 @@ func (r *Client) UserSimpleList(departmentID int) ([]*UserList, error) {
return result.UserList, nil
}
type (
// UserCreateRequest 创建成员数据请求
UserCreateRequest struct {
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 []ExtraAttr `json:"attrs"`
} `json:"extattr"`
ToInvite bool `json:"to_invite"`
ExternalPosition string `json:"external_position"`
ExternalProfile ExternalProfile `json:"external_profile"`
}
// ExtraAttr 扩展属性
ExtraAttr 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"`
}
// ExternalProfile 成员对外信息
ExternalProfile struct {
ExternalCorpName string `json:"external_corp_name"`
WechatChannels struct {
Nickname string `json:"nickname"`
Status int `json:"status"`
} `json:"wechat_channels"`
ExternalAttr []ExternalProfileAttr `json:"external_attr"`
}
// ExternalProfileAttr 成员对外信息属性
ExternalProfileAttr 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"`
}
// 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
@@ -154,6 +251,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"`