1. Member creation operation
For convenience, we can create a departmental organizational structure, which is a prerequisite for development, because our address book management is also based on an organizational structure, as above The level of organizational structure introduced in this article is the same. Here I create a root structure of Guangzhou Aiqidi, and then create some organizational structures in it, as shown in the figure below.
You can add people through functional operations in the background. This article mainly introduces how to call the WeChat Enterprise Account API for personnel management operations.
The API definition of the creator is as follows.
Request instructions
Https request method: POST
https://qyapi.weixin.qq. com/cgi-bin/user/create?access_token=ACCESS_TOKEN
The request package structure is:
{ "userid": "zhangsan", "name": "张三", "department": [1, 2], "position": "产品经理", "mobile": "15913215421", "gender": 1, "tel": "62394", "email": "zhangsan@gzdev.com", "weixinid": "zhangsan4dev" }
Parameter description
Parameters | must | Description |
---|---|---|
access_token | is | Call interface credentials |
userid | is the | employee UserID. The account number corresponding to the management end must be unique within the enterprise. The length is 1~64 characters |
name | is the name of the | member. The length is 1~64 characters |
department | No | List of department IDs to which members belong. Note that the upper limit for direct employees in each department is 1,000 |
position | No | position information. The length is 0~64 characters |
mobile | No | Mobile phone number. It must be unique within the enterprise, mobile/weixinid/email cannot be empty at the same time |
gender | No | Gender. gender=0 means male, =1 means female. Default gender=0 |
tel | No | Office phone number. The length is 0~64 characters |
No | email. The length is 0~64 characters. The company must have a unique | |
weixinid | No | WeChat ID. Must be the only one in the enterprise |
Permission description
The administrator must have the interface of "operating address book" permissions, as well as the management permissions of designated departments.
Return result
{ "errcode": 0, "errmsg": "created" }
In C#, we need to define the corresponding interface, and then construct the corresponding transfer entity information as needed.
Here I have defined all the interfaces for personnel management. The interface definitions are as follows.
#region 部门成员管理 /// <summary> /// 创建成员 /// </summary> CommonResult CreateUser(string accessToken, CorpUserJson user); /// <summary> /// 更新成员 /// </summary> CommonResult UpdateUser(string accessToken, CorpUserUpdateJson user); /// <summary> /// 删除成员 /// </summary> CommonResult DeleteUser(string accessToken, string userid); /// <summary> /// 根据成员id获取成员信息 /// </summary> CorpUserGetJson GetUser(string accessToken, string userid); /// <summary> /// 获取部门成员 /// </summary> CorpUserListJson GetDeptUser(string accessToken, int department_id, int fetch_child = 0, int status = 0); #endregion
Then create a CorpUserJson entity object that carries personnel information based on the information definition. The implementation operation code for creating personnel is as follows.
/// <summary> /// 创建成员 /// </summary> public CommonResult CreateUser(string accessToken, CorpUserJson user) { string urlFormat = "http://www.php.cn/{0}"; var data = new { userid = user.userid, name = user.name, department = user.department, position = user.position, mobile = user.mobile, gender = user.gender, tel = user.tel, email = user.email, weixinid = user.weixinid }; var url = string.Format(urlFormat, accessToken); var postData = data.ToJson(); return Helper.GetCorpExecuteResult(url, postData); }
Member data update Similar to the creation operation, its enterprise number is defined as follows.
Request instructions
Https request method: POST
https://qyapi.weixin.qq. com/cgi-bin/user/update?access_token=ACCESS_TOKEN
The request package example is as follows (if a non-required field is not specified, the previous setting value of the field will not be updated):
{ "userid": "zhangsan", "name": "李四", "department": [1], "position": "后台工程师", "mobile": "15913215421", "gender": 1, "tel": "62394", "email": "zhangsan@gzdev.com", "weixinid": "lisifordev", "enable": 1 }
Since its operation data is similar, its implementation code is also similar, as shown below.
/// <summary> /// 更新成员 /// </summary> public CommonResult UpdateUser(string accessToken, CorpUserUpdateJson user) { string urlFormat = "http://www.php.cn/{0}"; //string postData = user.ToJson(); var data = new { userid = user.userid, name = user.name, department = user.department, position = user.position, mobile = user.mobile, gender = user.gender, tel = user.tel, email = user.email, weixinid = user.weixinid, enable = user.enable }; var url = string.Format(urlFormat, accessToken); var postData = data.ToJson(); return Helper.GetCorpExecuteResult(url, postData); }
1) The definition of deletion personnel is as follows:
https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=lisi
Required | Description | |
---|---|---|
is the | calling interface credential | |
is the | employee UserID. Corresponding account number on the management side |
参数 | 必须 | 说明 |
---|---|---|
access_token | 是 | 调用接口凭证 |
userid | 是 | 员工UserID |
返回结果
{ "errcode": 0, "errmsg": "ok", "userid": "zhangsan", "name": "李四", "department": [1, 2], "position": "后台工程师", "mobile": "15913215421", "gender": 1, "tel": "62394", "email": "zhangsan@gzdev.com", "weixinid": "lisifordev", "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/0", "status": 1 }
请求说明
Https请求方式: GET
https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=1&fetch_child=0&status=0
参数说明
参数 | 必须 | 说明 |
---|---|---|
access_token | 是 | 调用接口凭证 |
department_id | 是 | 获取的部门id |
fetch_child | 否 | 1/0:是否递归获取子部门下面的成员 |
status | 否 | 0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 |
权限说明
管理员须拥有’获取部门成员’的接口权限,以及指定部门的查看权限。
返回结果
{ "errcode": 0, "errmsg": "ok", "userlist": [ { "userid": "zhangsan", "name": "李四" } ] }
这个返回值我们定义一个实体对象用来存储数据即可。
/// <summary> /// 获取部门成员返回的数据 /// </summary> public class CorpUserListJson : BaseJsonResult { public CorpUserListJson() { this.userlist = new List<corpusersimplejson>(); } /// <summary> /// 返回的错误消息 /// </summary> public CorpReturnCode errcode { get; set; } /// <summary> /// 对返回码的文本描述内容 /// </summary> public string errmsg { get; set; } /// <summary> /// 成员列表 /// </summary> public List<corpusersimplejson> userlist { get; set; } }</corpusersimplejson></corpusersimplejson>
上面介绍了一些企业号的接口定义和我对API的C#封装接口和部分实现代码,实现了功能后,我们就可以在代码中对它进行测试,确信是否正常使用。
/// <summary> /// 人员管理综合性操作(创建、修改、获取信息、删除) /// </summary> /// <param> /// <param> private void btnCorpUser_Click(object sender, EventArgs e) { CorpUserJson user = new CorpUserJson(); user.userid = "test"; user.name ="测试用户"; user.department = new List<int>(){2}; user.email = "test@163.com"; ICorpAddressBookApi bll = new CorpAddressBookApi(); CommonResult result = bll.CreateUser(token, user); if (result != null) { Console.WriteLine("创建成员:{0} {1} {2}", user.name, (result.Success ? "成功" : "失败"), result.ErrorMessage); string name = "修改测试"; user.name = name; CorpUserUpdateJson userUpdate = new CorpUserUpdateJson(user); result = bll.UpdateUser(token, userUpdate); if (result != null) { Console.WriteLine("修改名称:{0} {1} {2}", name, (result.Success ? "成功" : "失败"), result.ErrorMessage); } CorpUserGetJson userGet = bll.GetUser(token, user.userid); if (userGet != null) { Console.WriteLine("成员名称:{0} ({1} {2})", userGet.name, user.userid, user.email); } result = bll.DeleteUser(token, user.userid); if (result != null) { Console.WriteLine("删除成员:{0} {1} {2}", name, (result.Success ? "成功" : "失败"), result.ErrorMessage); } } }</int>
获取部门人员的操作代码如下所示。
/// <summary> /// 获取部门人员 /// </summary> private void btnCorpUserList_Click(object sender, EventArgs e) { int deptId = 1; ICorpAddressBookApi bll = new CorpAddressBookApi(); CorpUserListJson result = bll.GetDeptUser(token, deptId); if (result != null) { foreach(CorpUserSimpleJson item in result.userlist) { Console.WriteLine("成员名称:{0} {1}", item.name, item.userid); } } }
人员的管理,相对来说比较简单,主要是在一定的部门下创建人员,然后也可以给标签增加相应的人员,基本上就是这些了,不过一定需要确保有相应的权限进行操作。
更多C# development of WeChat portal and application of WeChat enterprise account address book management development and member management相关文章请关注PHP中文网!