In the previous essay "C# Development of WeChat Portal and Application (17) - Department Management of Address Book Management and Development of WeChat Enterprise Account", the relevant operation management of the address book department was introduced. Address book management includes department management and member management. , three parts of tag management. This article mainly introduces the management operations of members, including the key points of creating, deleting, updating, obtaining, and obtaining department members.
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, just like the organizational structure level introduced in the previous article. 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 unique within the enterprise |
权限说明
管理员须拥有“操作通讯录”的接口权限,以及指定部门的管理权限。
返回结果
{ "errcode": 0, "errmsg": "created" }
我们在C#里面,需要定义对应给的接口,然后根据需要构造对应的传递实体信息。
这里我把人员管理的接口全部定义好,接口定义如下所示。
#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
然后根据信息定义,创建一个承载人员信息的CorpUserJson实体对象,创建人员的实现操作代码如下所示。
/// <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); }
成员的数据更新和创建操作类似,它的企业号定义如下所示。
请求说明
Https请求方式: POST
https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=ACCESS_TOKEN
请求包示例如下(如果非必须的字段未指定,则不更新该字段之前的设置值):
{ "userid": "zhangsan", "name": "李四", "department": [1], "position": "后台工程师", "mobile": "15913215421", "gender": 1, "tel": "62394", "email": "zhangsan@gzdev.com", "weixinid": "lisifordev", "enable": 1 }
由于它的操作数据类似,因此它的实现代码也差不多,如下所示就是。
/// <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); }
这些操作和上面的类似,不在赘述,主要就是根据需要定义他们对应的返回数据信息,然后解析Json数据即可转换为对应的实体。
请求说明
Https请求方式: GET
https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=lisi
参数说明
参数 | 必须 | 说明 |
---|---|---|
access_token | 是 | 调用接口凭证 |
userid | 是 | 员工UserID。对应管理端的帐号 |
返回结果
{ "errcode": 0, "errmsg": "deleted" }
请求说明
Https请求方式: GET
https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=lisi
参数说明
参数 | 必须 | 说明 |
---|---|---|
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-Member management of address book management and development of WeChat enterprise account相关文章请关注PHP中文网!