C # WeChat portal and application development (18)-member management of Address Book Management and Development on WeChat enterprise account

Source: Internet
Author: User
Tags tojson
1. Create 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, as the organizational structure level introduced in the previous chapter. Here I create a root structure of Guangzhou Aiqidi, and then create some organizations in it, as shown below.



In the background, you can add personnel through functional operations. This article mainly introduces how to call WeChat Enterprise ID API for personnel management operations.
The API definition of the creator is shown below.
Request description

Https request method: POST
https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN

The request packet structure is:

{
   "userid": "zhangsan",
   "name": "Zhang San",
   "department": [1, 2],
   "position": "Product Manager",
   "mobile": "15913215421",
   "gender": 1,
   "tel": "62394",
   "email": "zhangsan@gzdev.com",
   "weixinid": "zhangsan4dev"
}
Parameter Description


Privilege description

The administrator must have the interface rights to "operate the address book" and the management rights of the designated department.

Return result

{
"errcode": 0,
"errmsg": "created"
}

In C #, we need to define the corresponding interface, and then construct the corresponding delivery entity information as needed.

Here I have defined all the interfaces for personnel management. The interface definition is shown below.

#region Member Management
/// <summary>
/// create member
/// </ summary>
CommonResult CreateUser (string accessToken, CorpUserJson user);

/// <summary>
/// update member
/// </ summary>
CommonResult UpdateUser (string accessToken, CorpUserUpdateJson user);

/// <summary>
/// delete member
/// </ summary>
CommonResult DeleteUser (string accessToken, string userid);

/// <summary>
/// get member information based on member id
/// </ summary>
CorpUserGetJson GetUser (string accessToken, string userid);

/// <summary>
/// Get department members
/// </ summary>
CorpUserListJson GetDeptUser (string accessToken, int department_id, int fetch_child = 0, int status = 0);
#endregion
Then according to the information definition, create a CorpUserJson entity object that carries the personnel information, and the implementation operation code of the creator is shown below.

/// <summary>
/// create member
/// </ summary>
public CommonResult CreateUser (string accessToken, CorpUserJson user)
{
    string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={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);
}
2. Update operations for members
Member data update and create operations are similar, its enterprise number is defined as follows.
Request description

Https request method: POST
https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=ACCESS_TOKEN

An example of a request packet is as follows (if a non-required field is not specified, the previous setting value of the field is not updated):

{
   "userid": "zhangsan",
   "name": "lisi",
   "department": [1],
   "position": "Backstage Engineer",
   "mobile": "15913215421",
   "gender": 1,
   "tel": "62394",
   "email": "zhangsan@gzdev.com",
   "weixinid": "lisifordev",
   "enable": 1
}
Since its operation data is similar, its implementation code is similar, as shown below.

/// <summary>
/// update member
/// </ summary>
public CommonResult UpdateUser (string accessToken, CorpUserUpdateJson user)
{
    string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={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);
}
3.Removal of members, acquisition of members, acquisition of department members
These operations are similar to the above, and will not be described in detail. The main thing is to define their corresponding returned data information as needed, and then parse the Json data to convert them into corresponding entities.
1) The definition of deleted person is as follows:
Request description

Https request method: GET
https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=lisi

Parameter Description



Return result

{"errcode": 0, "errmsg": "deleted"}

2) Member acquisition is defined as follows:
Request description

Https request method: GET
https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=lisi

Parameter Description



Return result

{
   "errcode": 0,
   "errmsg": "ok",
   "userid": "zhangsan",
   "name": "lisi",
   "department": [1, 2],
   "position": "Backstage Engineer",
   "mobile": "15913215421",
   "gender": 1,
   "tel": "62394",
   "email": "zhangsan@gzdev.com",
   "weixinid": "lisifordev",
   "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/0,
   "status": 1
}
3) Acquisition of department members is defined as follows:
Request description

Https request method: GET
https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=1&fetch_child=0&status=0

Parameter Description


Privilege description

The administrator must have the interface authority of 'Get department members' and the viewing authority of the designated department.

Return result

{
   "errcode": 0,
   "errmsg": "ok",
   "userlist": [
       {
          "userid": "zhangsan",
          "name": "Li Si"
       }
     ]
}
This return value we can define an entity object to store data.

/// <summary>
/// Get data returned by department members
/// </ summary>
public class CorpUserListJson: BaseJsonResult
{
    public CorpUserListJson ()
    {
        this.userlist = new List <CorpUserSimpleJson> ();
    }

    /// <summary>
    /// error message returned
    /// </ summary>
    public CorpReturnCode errcode {get; set;}

    /// <summary>
    /// Text description of the return code
    /// </ summary>
    public string errmsg {get; set;}

    /// <summary>
    /// member list
    /// </ summary>
    public List <CorpUserSimpleJson> userlist {get; set;}
}
4, comprehensive example call code
The above introduces the interface definition of some enterprise numbers and my C # package interface and part of the implementation code of the API. After the function is implemented, we can test it in the code to make sure it is used normally.

/// <summary>
/// Personnel management comprehensive operations (create, modify, obtain information, delete)
/// </ summary>
/// <param name = "sender"> </ param>
/// <param name = "e"> </ param>
pr
ivate void btnCorpUser_Click (object sender, EventArgs e)
{
    CorpUserJson user = new CorpUserJson ();
    user.userid = "test";
    user.name = "test user";
    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 ("Create member: {0} {1} {2}", user.name, (result.Success? "Success": "Failure"), result.ErrorMessage);

        string name = "Modify Test";
        user.name = name;
        CorpUserUpdateJson userUpdate = new CorpUserUpdateJson (user);
        result = bll.UpdateUser (token, userUpdate);
        if (result! = null)
        {
            Console.WriteLine ("Modified name: {0} {1} {2}", name, (result.Success? "Success": "Failed"), result.ErrorMessage);
        }

        CorpUserGetJson userGet = bll.GetUser (token, user.userid);
        if (userGet! = null)
        {
            Console.WriteLine ("Member name: {0} ({1} {2})", userGet.name, user.userid, user.email);
        }

        result = bll.DeleteUser (token, user.userid);
        if (result! = null)
        {
            Console.WriteLine ("Remove member: {0} {1} {2}", name, (result.Success? "Success": "Fail"), result.ErrorMessage);
        }
    }
}

The operation code for obtaining department personnel is shown below.

/// <summary>
/// Get department staff
/// </ 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 ("Member name: {0} {1}", item.name, item.userid);
        }
    }
}
The management of employees is relatively simple. It is mainly to create personnel in a certain department, and then you can add corresponding personnel to the label. This is basically all, but you must ensure that you have the appropriate permissions to operate.

If you are interested in this "C # Development WeChat Portal and Application" series, you can follow my other articles

Author: Wu Huacong _ Development Framework
Link: https://www.jianshu.com/p/8aedb5ccb553
Source: Jianshu
Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.