Home > WeChat Applet > WeChat Development > C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

高洛峰
Release: 2017-03-02 09:45:20
Original
1706 people have browsed it

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.

1. Creation and configuration of enterprise 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.

C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

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.

C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

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.

C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

# 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.

C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

2. Obtaining AccessToken, the globally unique ticket for API access

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>企业Id
        /// <param>管理组的凭证密钥
        /// <returns></returns>
        public string GetAccessTokenNoCache(string corpid, string corpsecret)
        {            var url = string.Format("//m.sbmmt.com/{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;
        }</token>
Copy after login


The description of the WeChat enterprise account is as follows:

When the enterprise application calls When using the Enterprise Account interface, the Enterprise Account background verifies the legality of the access and the management permissions 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.

2. Maintenance of department information in address book management

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.

  • Request instructions

Https请求方式: POST

https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN

请求包结构体为:

{
   "name": "邮箱产品组",
   "parentid": "1"
}
Copy after login
  • 参数说明

参数 必须 说明
access_token 调用接口凭证
name 部门名称。长度限制为1~64个字符
parentid 父亲部门id。根部门id为1

 

  • 返回结果

{
   "errcode": 0,
   "errmsg": "created",
   "id": 2
}
Copy after login

根据上面的一些类似的接口定义说明,我们先来定义下组织机构部门数据的维护接口,然后在逐步实现和调用。


        #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
Copy after login


如创建部门的接口实现如下所示,主要就是构建URL和POST的数据包,然后统一调用并获取返回数据,转换为具体的Json对象实体即可。其他接口的实现方式类似,不在赘述。


        /// <summary>
        /// 创建部门。        /// 管理员须拥有“操作通讯录”的接口权限,以及父部门的管理权限。        /// </summary>
        public CorpDeptCreateJson CreateDept(string accessToken, string name, string parentId)
        {            string urlFormat = "//m.sbmmt.com/{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>
Copy after login


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; }
    }
Copy after login


 

 3、部门管理的API调用

 上面小节介绍了如何封装部门管理的API,那么我们封装好了对应的接口和接口实现,怎么样在实际环境里面进行调用处理的呢,为了方便我创建一个小的Winform程序来测试对应API的功能,如下所示。

C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

下面我们来介绍一下调用的代码和效果展示。


        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);
                }
            }
            
        }
Copy after login



        /// <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);
            }
        }
Copy after login


C# development of WeChat portal and department management of address book management and development using WeChat enterprise account

 

 更多C# development of WeChat portal and department management of address book management and development using WeChat enterprise account相关文章请关注PHP中文网!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template