The previous article provides some basic information about the Enterprise Account, and introduces how to configure the callback method of the Enterprise Account to achieve a bridge of communication with the Enterprise Account server. This article mainly continues to introduce the development work of the enterprise account, introduces the WeChat enterprise account address book management and development function, and introduces how to obtain and manage department information in the organization.
First of all, we can create an organization in the management background of the enterprise account, and create some departments and personnel lists in it to facilitate our development and use.
For example, create a root structure of Guangzhou Aiqidi, and then create some organizational structures in it, as shown in the figure below.
Then add an administrator permission to the root node of the organizational structure "Guangzhou Aiqidi". In the future, you can use the permission Secret value of this administrator in the interface development. A call was made.
CorpID is the identification of the enterprise number. Each enterprise number has a unique CorpID; Secret is the management group credential key.
The system administrator can create a management group through the permission management function of the management terminal and assign the management group access rights to applications, address books, and interfaces. Once completed, the management group can obtain a unique secret. System administrators can view the secrets of all management groups through permission management, and other administrators can view them through the developer credentials in settings.
The creator of my enterprise account and the administrator of the "Guangzhou Iqidi" organizational structure are different. Since Secret is the management group credential key, if the administrator is responsible for the management of different organizational structures, he or she The management Secret value may be different. If we need to call the interface, we need to use the Secret value belonging to our own permission level, as shown in the figure below.
# If you are not the creator of the enterprise account, you may not be able to modify some permission assignments inside, you can only view them.
Like the public account, the first step when we call the enterprise account API is to obtain it first Access ticket AccessToken. This ticket is global and has certain timeliness and frequency control, so it needs to be cached appropriately and cannot be refreshed every time it is called.
The main logic code for the enterprise account to obtain the access ticket is as follows. The main thing is to use the administrator's Secret value to obtain the corresponding password, so that it can know which organizational structure it is managing. .
/// <summary> /// 获取每次操作微信API的Token访问令牌 /// </summary> /// <param name="corpid">企业Id</param> /// <param name="corpsecret">管理组的凭证密钥</param> /// <returns></returns> public string GetAccessTokenNoCache(string corpid, string corpsecret) { var url = string.Format("http://www.php.cn/{0}&corpsecret={1}", corpid, corpsecret); HttpHelper helper = new HttpHelper(); string result = helper.GetHtml(url); string regex = "\"access_token\":\"(?<token>.*?)\""; string token = CRegex.GetText(result, regex, "token"); return token; }
The description of the WeChat enterprise account is as follows:
When the enterprise application calls the enterprise account interface, the enterprise The backend of the account verifies the legality of the access and the management rights of the corresponding management group based on the AccessToken of this access to return the corresponding results.
Note: You should carefully configure the permissions of the management group, which is enough. If the permissions are too high, the permissions will increase. Possibility of misuse and information security risks.
AccessToken is the globally unique ticket of the enterprise account. AccessToken needs to be carried when calling the interface. AccessToken needs to be exchanged for CorpID and Secret. Different Secrets will return different AccessTokens. Under normal circumstances, the AccessToken is valid for 7200 seconds. Repeated acquisitions within the validity period will return the same results and be automatically renewed. Since the number of api calls to obtain access_token is very limited, it is recommended that enterprises store and update access_token globally. Frequent refresh of access_token will limit api calls and affect its own business.
With the access tickets in the first section, we can use the API to do many things, including Functions such as obtaining, creating, deleting, etc. of organizational structure.
The official interface definition of the creation department is as follows.
请求说明
Https请求方式: POST
http://www.php.cn/
请求包结构体为:
{ "name": "邮箱产品组", "parentid": "1" }
参数说明
参数 | 必须 | 说明 |
---|---|---|
access_token | 是 | 调用接口凭证 |
name | 是 | 部门名称。长度限制为1~64个字符 |
parentid | 是 | 父亲部门id。根部门id为1 |
返回结果
{ "errcode": 0, "errmsg": "created", "id": 2 }
根据上面的一些类似的接口定义说明,我们先来定义下组织机构部门数据的维护接口,然后在逐步实现和调用。
#region 部门管理 /// <summary> /// 创建部门。 /// 管理员须拥有“操作通讯录”的接口权限,以及父部门的管理权限。 /// </summary> CorpDeptCreateJson CreateDept(string accessToken, string name, string parentId); /// <summary> /// 更新部门。 /// 管理员须拥有“操作通讯录”的接口权限,以及该部门的管理权限。 /// </summary> CommonResult DeleteDept(string accessToken, int id); /// <summary> /// 删除部门. /// 管理员须拥有“操作通讯录”的接口权限,以及该部门的管理权限。 /// </summary> CorpDeptListJson ListDept(string accessToken); /// <summary> /// 获取部门列表. /// 管理员须拥有’获取部门列表’的接口权限,以及对部门的查看权限。 /// </summary> CommonResult UpdateDept(string accessToken, int id, string name); #endregion
如创建部门的接口实现如下所示,主要就是构建URL和POST的数据包,然后统一调用并获取返回数据,转换为具体的Json对象实体即可。其他接口的实现方式类似,不在赘述。
/// <summary> /// 创建部门。 /// 管理员须拥有“操作通讯录”的接口权限,以及父部门的管理权限。 /// </summary> public CorpDeptCreateJson CreateDept(string accessToken, string name, string parentId) { string urlFormat = "http://www.php.cn/{0}"; var data = new { name = name, parentId = parentId }; var url = string.Format(urlFormat, accessToken); var postData = data.ToJson(); CorpDeptCreateJson result = CorpJsonHelper<CorpDeptCreateJson>.ConvertJson(url, postData); return result; }
CorpDeptCreateJson 对象实体类的定义如下所示,我们主要是根据返回结果进行定义的。
/// <summary> /// 创建部门的返回结果 /// </summary> public class CorpDeptCreateJson : BaseJsonResult { /// <summary> /// 返回的错误消息 /// </summary> public CorpReturnCode errcode { get; set; } /// <summary> /// 对返回码的文本描述内容 /// </summary> public string errmsg { get; set; } /// <summary> /// 创建的部门id。 /// </summary> public int id { get; set; } }
上面小节介绍了如何封装部门管理的API,那么我们封装好了对应的接口和接口实现,怎么样在实际环境里面进行调用处理的呢,为了方便我创建一个小的Winform程序来测试对应API的功能,如下所示。
下面我们来介绍一下调用的代码和效果展示。
private void btnCreateDeleteDept_Click(object sender, EventArgs e) { ICorpAddressBookApi bll = new CorpAddressBookApi(); string name = "测试部门"; CorpDeptCreateJson json = bll.CreateDept(token, name, "2"); if (json != null) { Console.WriteLine("创建了部门:{0}, ID:{1}", name, json.id); //更新部门信息 name = "测试部门修改名称"; CommonResult result = bll.UpdateDept(token, json.id, name); if(result != null) { Console.WriteLine("修改部门名称:{0} {1}", (result.Success ? "成功" : "失败"), result.ErrorMessage); } //删除部门 result = bll.DeleteDept(token, json.id); if (result != null) { Console.WriteLine("删除部门名称:{0} {1}", (result.Success ? "成功" : "失败"), result.ErrorMessage); } } }
/// <summary> /// 获取部门列表 /// </summary> private void btnListDept_Click(object sender, EventArgs e) { ICorpAddressBookApi bll = new CorpAddressBookApi(); CorpDeptListJson list = bll.ListDept(token); foreach (CorpDeptJson info in list.department) { string tips = string.Format("{0}:{1}", info.name, info.id); Console.WriteLine(tips); } }
更多C#开发微信门户及应用(17)-微信企业号的通讯录管理开发之部门管理 相关文章请关注PHP中文网!