自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

微信企業(yè)號的通訊錄管理開發(fā)之成員管理

開發(fā) 后端
本篇主要介紹成員的管理操作,包括創(chuàng)建、刪除、更新、獲取、獲取部門成員幾個操作要點。

在上篇隨筆《微信企業(yè)號的通訊錄管理開發(fā)之部門管理》介紹了通訊錄的部門的相關操作管理,通訊錄管理包括部門管理、成員管理、標簽管理三個部分,本篇主要介紹成員的管理操作,包括創(chuàng)建、刪除、更新、獲取、獲取部門成員幾個操作要點。

1、成員的創(chuàng)建操作

為了方便,我們可以創(chuàng)建一個部門組織結構,這是開發(fā)的前提,因為我們通訊錄管理,也是基于一個組織機構下的,如上篇介紹的組織結構層次一樣。我這里創(chuàng)建一個廣州愛奇迪的根結構,然后在其中在創(chuàng)建一些組織機構,如下圖所示。

在后臺可以通過功能操作添加人員,本篇主要介紹如何調用微信企業(yè)號API進行人員管理的操作。

創(chuàng)建人員的API定義如下所示。

  • 請求說明

Https請求方式: POST

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

請求包結構體為:

  1. {  
  2.    "userid""zhangsan",  
  3.    "name""張三",  
  4.    "department": [1, 2],  
  5.    "position""產(chǎn)品經(jīng)理",  
  6.    "mobile""15913215421",  
  7.    "gender": 1,  
  8.    "tel""62394",  
  9.    "email""zhangsan@gzdev.com",  
  10.    "weixinid""zhangsan4dev" 
  11. }  
  • 參數(shù)說明

參數(shù) 必須 說明
access_token 調用接口憑證
userid 員工UserID。對應管理端的帳號,企業(yè)內必須***。長度為1~64個字符
name 成員名稱。長度為1~64個字符
department 成員所屬部門id列表。注意,每個部門的直屬員工上限為1000個
position 職位信息。長度為0~64個字符
mobile 手機號碼。企業(yè)內必須***,mobile/weixinid/email三者不能同時為空
gender 性別。gender=0表示男,=1表示女。默認gender=0
tel 辦公電話。長度為0~64個字符
email 郵箱。長度為0~64個字符。企業(yè)內必須***
weixinid 微信號。企業(yè)內必須***

  • 權限說明

管理員須擁有“操作通訊錄”的接口權限,以及指定部門的管理權限。

  • 返回結果

 

  1. {  
  2.    "errcode": 0,  
  3.    "errmsg""created" 
  4. }  

 

 我們在C#里面,需要定義對應給的接口,然后根據(jù)需要構造對應的傳遞實體信息。

這里我把人員管理的接口全部定義好,接口定義如下所示。

  1. #region 部門成員管理  
  2.         /// <summary>  
  3.         /// 創(chuàng)建成員  
  4.         /// </summary>  
  5.         CommonResult CreateUser(string accessToken, CorpUserJson user);  
  6.  
  7.         /// <summary>  
  8.         /// 更新成員  
  9.         /// </summary>  
  10.         CommonResult UpdateUser(string accessToken, CorpUserUpdateJson user);  
  11.  
  12.         /// <summary>  
  13.         /// 刪除成員  
  14.         /// </summary>  
  15.         CommonResult DeleteUser(string accessToken, string userid);  
  16.  
  17.         /// <summary>  
  18.         /// 根據(jù)成員id獲取成員信息  
  19.         /// </summary>  
  20.         CorpUserGetJson GetUser(string accessToken, string userid);  
  21.  
  22.         /// <summary>  
  23.         /// 獲取部門成員  
  24.         /// </summary>  
  25.         CorpUserListJson GetDeptUser(string accessToken, int department_id, int fetch_child = 0, int status = 0);  
  26.         #endregion 

然后根據(jù)信息定義,創(chuàng)建一個承載人員信息的CorpUserJson實體對象,創(chuàng)建人員的實現(xiàn)操作代碼如下所示。

  1. /// <summary>  
  2.         /// 創(chuàng)建成員  
  3.         /// </summary>  
  4.         public CommonResult CreateUser(string accessToken, CorpUserJson user)  
  5.         {  
  6.             string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={0}";  
  7.             var data = new 
  8.             {  
  9.                 userid = user.userid,  
  10.                 name = user.name,  
  11.                 department = user.department,  
  12.                 position = user.position,  
  13.                 mobile = user.mobile,  
  14.                 gender = user.gender,  
  15.                 tel = user.tel,  
  16.                 email = user.email,  
  17.                 weixinid = user.weixinid  
  18.             };  
  19.             var url = string.Format(urlFormat, accessToken);  
  20.             var postData = data.ToJson();  
  21.  
  22.             return Helper.GetCorpExecuteResult(url, postData);  
  23.         } 

2、成員的更新操作

成員的數(shù)據(jù)更新和創(chuàng)建操作類似,它的企業(yè)號定義如下所示。

  • 請求說明

Https請求方式: POST

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

請求包示例如下(如果非必須的字段未指定,則不更新該字段之前的設置值):

 

  1. {  
  2.    "userid""zhangsan",  
  3.    "name""李四",  
  4.    "department": [1],  
  5.    "position""后臺工程師",  
  6.    "mobile""15913215421",  
  7.    "gender": 1,  
  8.    "tel""62394",  
  9.    "email""zhangsan@gzdev.com",  
  10.    "weixinid""lisifordev",  
  11.    "enable": 1  

 

由于它的操作數(shù)據(jù)類似,因此它的實現(xiàn)代碼也差不多,如下所示就是。

  1. /// <summary>  
  2.         /// 更新成員  
  3.         /// </summary>  
  4.         public CommonResult UpdateUser(string accessToken, CorpUserUpdateJson user)  
  5.         {  
  6.             string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={0}";  
  7.             //string postData = user.ToJson();  
  8.             var data = new 
  9.             {  
  10.                 userid = user.userid,  
  11.                 name = user.name,  
  12.                 department = user.department,  
  13.                 position = user.position,  
  14.                 mobile = user.mobile,  
  15.                 gender = user.gender,  
  16.                 tel = user.tel,  
  17.                 email = user.email,  
  18.                 weixinid = user.weixinid,  
  19.                 enable = user.enable  
  20.             };  
  21.             var url = string.Format(urlFormat, accessToken);  
  22.             var postData = data.ToJson();  
  23.  
  24.             return Helper.GetCorpExecuteResult(url, postData);  
  25.         } 

3、成員的刪除、成員的獲取、部門成員的獲取操作

這些操作和上面的類似,不在贅述,主要就是根據(jù)需要定義他們對應的返回數(shù)據(jù)信息,然后解析Json數(shù)據(jù)即可轉換為對應的實體。

1)刪除人員的定義如下:

  • 請求說明

Https請求方式: GET

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

  • 參數(shù)說明

參數(shù) 必須 說明
access_token 調用接口憑證
userid 員工UserID。對應管理端的帳號

  • 返回結果
{
   "errcode": 0,
   "errmsg": "deleted"
}

2)成員的獲取定義如下:

  • 請求說明

Https請求方式: GET

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

  • 參數(shù)說明

參數(shù) 必須 說明
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
}

3)部門成員的獲取定義如下:

  • 請求說明

Https請求方式: GET

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

  • 參數(shù)說明

參數(shù) 必須 說明
access_token 調用接口憑證
department_id 獲取的部門id
fetch_child 1/0:是否遞歸獲取子部門下面的成員
status 0獲取全部員工,1獲取已關注成員列表,2獲取禁用成員列表,4獲取未關注成員列表。status可疊加

  • 權限說明

管理員須擁有’獲取部門成員’的接口權限,以及指定部門的查看權限。

  • 返回結果

 

  1. {  
  2.    "errcode": 0,  
  3.    "errmsg""ok",  
  4.    "userlist": [  
  5.            {  
  6.                   "userid""zhangsan",  
  7.                   "name""李四" 
  8.            }  
  9.      ]  

 

這個返回值我們定義一個實體對象用來存儲數(shù)據(jù)即可。

  1. /// <summary>  
  2.     /// 獲取部門成員返回的數(shù)據(jù)  
  3.     /// </summary>  
  4.     public class CorpUserListJson : BaseJsonResult  
  5.     {  
  6.         public CorpUserListJson()  
  7.         {  
  8.             this.userlist = new List<CorpUserSimpleJson>();  
  9.         }  
  10.  
  11.         /// <summary>  
  12.         /// 返回的錯誤消息  
  13.         /// </summary>  
  14.         public CorpReturnCode errcode { getset; }  
  15.  
  16.         /// <summary>  
  17.         /// 對返回碼的文本描述內容  
  18.         /// </summary>  
  19.         public string errmsg { getset; }  
  20.  
  21.         /// <summary>  
  22.         /// 成員列表  
  23.         /// </summary>  
  24.         public List<CorpUserSimpleJson> userlist { getset; }  
  25.     } 

7、綜合例子調用代碼

上面介紹了一些企業(yè)號的接口定義和我對API的C#封裝接口和部分實現(xiàn)代碼,實現(xiàn)了功能后,我們就可以在代碼中對它進行測試,確信是否正常使用。

  1. /// <summary>  
  2.         /// 人員管理綜合性操作(創(chuàng)建、修改、獲取信息、刪除)  
  3.         /// </summary>  
  4.         /// <param name="sender"></param>  
  5.         /// <param name="e"></param>  
  6.         private void btnCorpUser_Click(object sender, EventArgs e)  
  7.         {  
  8.             CorpUserJson user = new CorpUserJson();  
  9.             user.userid = "test";  
  10.             user.name ="測試用戶";  
  11.             user.department = new List<int>(){2};  
  12.             user.email = "test@163.com";  
  13.  
  14.             ICorpAddressBookApi bll = new CorpAddressBookApi();  
  15.             CommonResult result = bll.CreateUser(token, user);  
  16.             if (result != null)  
  17.             {  
  18.                 Console.WriteLine("創(chuàng)建成員:{0} {1} {2}", user.name, (result.Success ? "成功" : "失敗"), result.ErrorMessage);  
  19.  
  20.                 string name = "修改測試";  
  21.                 user.name = name;  
  22.                 CorpUserUpdateJson userUpdate = new CorpUserUpdateJson(user);  
  23.                 result = bll.UpdateUser(token, userUpdate);  
  24.                 if (result != null)  
  25.                 {  
  26.                     Console.WriteLine("修改名稱:{0} {1} {2}", name, (result.Success ? "成功" : "失敗"), result.ErrorMessage);  
  27.                 }  
  28.  
  29.                 CorpUserGetJson userGet = bll.GetUser(token, user.userid);  
  30.                 if (userGet != null)  
  31.                 {  
  32.                     Console.WriteLine("成員名稱:{0} ({1} {2})", userGet.name, user.userid, user.email);  
  33.                 }  
  34.  
  35.                 result = bll.DeleteUser(token, user.userid);  
  36.                 if (result != null)  
  37.                 {  
  38.                     Console.WriteLine("刪除成員:{0} {1} {2}", name, (result.Success ? "成功" : "失敗"), result.ErrorMessage);  
  39.                 }  
  40.             }  
  41.         } 

獲取部門人員的操作代碼如下所示。

  1. /// <summary>  
  2.         /// 獲取部門人員  
  3.         /// </summary>  
  4.         private void btnCorpUserList_Click(object sender, EventArgs e)  
  5.         {  
  6.             int deptId = 1;  
  7.             ICorpAddressBookApi bll = new CorpAddressBookApi();  
  8.             CorpUserListJson result = bll.GetDeptUser(token, deptId);  
  9.             if (result != null)  
  10.             {  
  11.                 foreach(CorpUserSimpleJson item in result.userlist)  
  12.                 {  
  13.                     Console.WriteLine("成員名稱:{0} {1}", item.name, item.userid);  
  14.                 }  
  15.             }  
  16.         } 

人員的管理,相對來說比較簡單,主要是在一定的部門下創(chuàng)建人員,然后也可以給標簽增加相應的人員,基本上就是這些了,不過一定需要確保有相應的權限進行操作。

原文出自:http://www.cnblogs.com/wuhuacong/p/3995484.html

責任編輯:林師授 來源: 伍華聰?shù)牟┛?/span>
相關推薦

2014-09-28 22:26:11

微信企業(yè)號

2014-09-24 10:29:14

微信企業(yè)號開發(fā)

2011-09-05 14:08:21

微信Andriod安卓

2014-09-28 22:38:21

微信企業(yè)號

2014-09-24 11:04:31

微信企業(yè)號開發(fā)

2014-09-25 13:19:35

微信企業(yè)號

2014-09-24 11:32:21

微信企業(yè)號開發(fā)

2014-09-24 09:59:23

微信企業(yè)號開發(fā)

2014-09-24 11:11:08

微信企業(yè)號開發(fā)

2015-07-30 15:58:15

EC企信企業(yè)即時通訊

2022-01-04 15:34:31

鴻蒙HarmonyOS應用

2010-09-08 23:11:01

2021-12-10 10:12:44

鴻蒙HarmonyOS應用

2010-05-12 14:42:20

2014-11-26 17:56:44

BQ企業(yè)即時通

2014-09-24 11:45:15

微信企業(yè)號開發(fā)

2014-09-25 14:17:31

微信企業(yè)號案例

2014-09-25 14:13:11

微信企業(yè)號案例

2014-09-24 11:52:37

微信企業(yè)號開發(fā)

2014-09-24 11:47:41

微信企業(yè)號開發(fā)
點贊
收藏

51CTO技術棧公眾號