天天看點

微信企業号 擷取使用者資訊2. 代碼示例

企業号的網頁開發,說白了就是移動端web開發,特殊點在于如何擷取微信使用者的身份資訊。

在企業号中可以進行如下步驟擷取微信使用者資訊:

通路一個業務頁面時,可通過OAuth驗證接口擷取此使用者資訊 → 根據code擷取userId → 根據userId擷取微信資訊。

① 擷取code

API:http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3#.E4.BC.81.E4.B8.9A.E8.8E.B7.E5.8F.96code

說明:網頁經過OAuth2.0驗證後,重定向到原來網頁并在url後面添加code資訊。

如:http://akmsg.com/a.html => OAhth2.0 => http://akmsg.com/a.html?code=CODE&state=STATE

② 根據code擷取userId

API:http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3#.E6.A0.B9.E6.8D.AEcode.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98.E4.BF.A1.E6.81.AF

說明:調用此接口後将會獲得 userId;注:userId為加密後的微信賬号。

③ 根據userId擷取微信資訊

API:http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98

說明:調用此接口後将會獲得此通路者在企業号登記的具體資訊;如:姓名、微信号、手機号、郵箱、職位等等。

④ 根據微信資訊擷取邏輯使用者資訊

說明:從上一步驟擷取的微信資訊,可以用來跟業務邏輯進行比對擷取此使用者在業務層中的使用者資訊。

1.3 流程圖

微信企業号 擷取使用者資訊2. 代碼示例

2. 代碼示例

2.1 代碼(C#)

邏輯:Asp.net對用戶端發送的請求進行判斷,符合微信企業号頁面規則的将進行微信企業号使用者身份認證操作。

此功能對通路請求的三種情況進行分别判斷:

1.第一次通路,沒code :進行OAuth驗證

2.有code,沒cookie :擷取code對應的資訊

3.有code,有cookie :驗證cookie

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

/// <summary>

/// 驗證微信通路

/// </summary>

public

static

void

Auth(HttpContext webContext)

{

string

requestURL = webContext.Request.Url.AbsoluteUri;

try

{

// 使用者通路微信頁面有3種情況:

// 1.第一次通路,沒code

// 2.有code,沒cookie;

// 3.有code,有cookie

// 1.第一次通路,沒code,沒cookie:跳轉到Oauth2.0認證

if

(

string

.IsNullOrEmpty(webContext.Request[

"code"

]))

{

string

url = 

string

.Format(

"https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"

, CORPID, webContext.Server.UrlEncode(requestURL));

webContext.Response.Redirect(url, 

false

);

}

else

if

(!

string

.IsNullOrEmpty(webContext.Request[

"code"

]) && 

string

.IsNullOrEmpty(CookieHelper.GetCookie(

"WXToken"

)))

{

// 2.有code,沒cookie:根據code擷取userID

string

code = webContext.Request[

"code"

];

string

userId = 

""

;

string

userInfo = 

""

;

#region 1)根據code擷取userId

string

url = 

string

.Format(

"https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}"

, GetAccessToken(), code);

string

responseText = HttpHelper.Instance.

get

(url);

WeChatUserCodeEntity codeEn = JsonHelper.GetEntity<WeChatUserCodeEntity>(responseText);

if

(codeEn.errcode > 0)

{

throw

new

Exception(codeEn.errmsg);

}

else

if

(

string

.IsNullOrEmpty(codeEn.UserId))

{

throw

new

Exception(

"請先關注企業号!"

);

}

userId = codeEn.UserId;

#endregion

#region 2)根據userId擷取使用者資訊

url = 

string

.Format(

"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}"

, GetAccessToken(), userId);

responseText = HttpHelper.Instance.

get

(url);

WeChatUserInfoEntity userInfoEn = JsonHelper.GetEntity<WeChatUserInfoEntity>(responseText);

if

(userInfoEn.errcode > 0)

{

throw

new

Exception(userInfoEn.errmsg);

}

userInfo = responseText;

#endregion

// 3.把userInfo傳入到cookie裡

CookieHelper.SetCookie(

"WXToken"

, userInfo, -1);

}

else

if

(!

string

.IsNullOrEmpty(webContext.Request[

"code"

]) && !

string

.IsNullOrEmpty(CookieHelper.GetCookie(

"WXToken"

)))

{

#region 3.有code,有cookie:校驗cookie

// TODO:在上面進行存入cookie時可采用AES加密,在這部進行解密校驗

// CookieHelper.SetCookie("WXToken", "", -1);

#endregion

}

else

{

throw

new

Exception(

"非授權通路!"

);

}

}

catch

(Exception ex)

{

throw

ex;

}

}

  

2.2 運作圖

1) 使用者已關注通路時

微信企業号 擷取使用者資訊2. 代碼示例

2) 使用者不屬于企業通訊錄通路時

微信企業号 擷取使用者資訊2. 代碼示例

2.3 Dmeo下載下傳(C#)

下載下傳位址:http://files.cnblogs.com/files/polk6/Wechat.QYH.zip

==================================系列文章==========================================

本篇文章:1.3 微信企業号 擷取使用者資訊

微信開發文章導航

1.微信企業号

  1.1 微信企業号 介紹

  1.2 微信企業号 擷取AccessToken

  1.3 微信企業号 擷取使用者資訊

  1.4 微信企業号 JS-SDK:上傳圖檔

2.微信公衆号

  2.1 微信公衆号 訂閱号與服務号的差別

  2.2 微信公衆号 幾種移動端UI架構介紹