C#開發(fā)微信門戶及應用(18)-微信企業(yè)號的通訊錄管理開發(fā)之成員管理

在上篇隨筆《C#開發(fā)微信門戶及應用(17)-微信企業(yè)號的通訊錄管理開發(fā)之部門管理》介紹了通訊錄的部門的相關操作管理难礼,通訊錄管理包括部門管理挣郭、成員管理、標簽管理三個部分蟀架,本篇主要介紹成員的管理操作,包括創(chuàng)建榆骚、刪除片拍、更新、獲取妓肢、獲取部門成員幾個操作要點捌省。

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

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



在后臺可以通過功能操作添加人員,本篇主要介紹如何調(diào)用微信企業(yè)號API進行人員管理的操作陆蟆。
創(chuàng)建人員的API定義如下所示雷厂。
請求說明

Https請求方式: POST
https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN

請求包結構體為:

{
   "userid": "zhangsan",
   "name": "張三",
   "department": [1, 2],
   "position": "產(chǎn)品經(jīng)理",
   "mobile": "15913215421",
   "gender": 1,
   "tel": "62394",
   "email": "zhangsan@gzdev.com",
   "weixinid": "zhangsan4dev"
}

參數(shù)說明


權限說明

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

返回結果

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

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

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

#region 部門成員管理
/// <summary>
/// 創(chuàng)建成員
/// </summary>
CommonResult CreateUser(string accessToken, CorpUserJson user);

/// <summary>
/// 更新成員
/// </summary>
CommonResult UpdateUser(string accessToken, CorpUserUpdateJson user);

/// <summary>
/// 刪除成員
/// </summary>
CommonResult DeleteUser(string accessToken, string userid);

/// <summary>
/// 根據(jù)成員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

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

/// <summary>
/// 創(chuàng)建成員
/// </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依痊、成員的更新操作
成員的數(shù)據(jù)更新和創(chuàng)建操作類似避除,它的企業(yè)號定義如下所示。
請求說明

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
}

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

/// <summary>
/// 更新成員
/// </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群井、成員的刪除、成員的獲取毫胜、部門成員的獲取操作

這些操作和上面的類似书斜,不在贅述,主要就是根據(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ù)說明


返回結果

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

2)成員的獲取定義如下:
請求說明

Https請求方式: GET
https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=lisi

參數(shù)說明


返回結果

{
   "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ù)說明

權限說明

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

返回結果

{
   "errcode": 0,
   "errmsg": "ok",
   "userlist": [
       {
          "userid": "zhangsan",
          "name": "李四"
       }
     ]
}

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

/// <summary>
/// 獲取部門成員返回的數(shù)據(jù)
/// </summary>
public class CorpUserListJson : BaseJsonResult
{
    public CorpUserListJson()
    {
        this.userlist = new List<CorpUserSimpleJson>();
    }

    /// <summary>
    /// 返回的錯誤消息
    /// </summary>
    public CorpReturnCode errcode { get; set; }

    /// <summary>
    /// 對返回碼的文本描述內(nèi)容
    /// </summary>
    public string errmsg { get; set; }

    /// <summary>
    /// 成員列表
    /// </summary>
    public List<CorpUserSimpleJson> userlist { get; set; }
}

4、綜合例子調(diào)用代碼

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

/// <summary>
/// 人員管理綜合性操作(創(chuàng)建、修改咧党、獲取信息秘蛔、刪除)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></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("創(chuàng)建成員:{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);
        }
    }
}

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

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

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

如果對這個《C#開發(fā)微信門戶及應用》系列感興趣,可以關注我的其他文章.

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末叠赐,一起剝皮案震驚了整個濱河市欲账,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌芭概,老刑警劉巖赛不,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異罢洲,居然都是意外死亡踢故,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門惹苗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來殿较,“玉大人,你說我怎么就攤上這事桩蓉×芨伲” “怎么了?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵触机,是天一觀的道長帚戳。 經(jīng)常有香客問我,道長儡首,這世上最難降的妖魔是什么片任? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮蔬胯,結果婚禮上对供,老公的妹妹穿的比我還像新娘。我一直安慰自己氛濒,他們只是感情好产场,可當我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著舞竿,像睡著了一般京景。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上骗奖,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天确徙,我揣著相機與錄音醒串,去河邊找鬼。 笑死鄙皇,一個胖子當著我的面吹牛芜赌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播伴逸,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼缠沈,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了错蝴?” 一聲冷哼從身側響起洲愤,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎漱竖,沒想到半個月后禽篱,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡馍惹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年躺率,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片万矾。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡悼吱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出良狈,到底是詐尸還是另有隱情后添,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布薪丁,位于F島的核電站遇西,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏严嗜。R本人自食惡果不足惜粱檀,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望漫玄。 院中可真熱鬧茄蚯,春花似錦、人聲如沸睦优。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汗盘。三九已至皱碘,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間隐孽,已是汗流浹背尸执。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工家凯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人如失。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像送粱,于是被迫代替她去往敵國和親褪贵。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容