获取用户信息
获取用户信息的接口参考:
https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId
首先设计结构如下图所示的表,表名为WXUsers,用于保存用户的信息:
建议列名和接口返回信息的键名保持一致(包括大小写),以方便编码。
获取单个用户信息
获取单个用户的信息,可以参考:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN"
url =
CExp(url,
Functions.Execute("GetAccessToken"),
"ojDlHuAjZ8Z3eSm0gW1g0Lxbz54s")
Dim
hc As
New HttpClient(url)
Dim
jo As
JObject = Jobject.Parse(hc.GetDaTa)
If
jo("errcode")
Is Nothing
Then
Dim dr
As DataRow =
DataTables("WXUsers").AddNew()
Dim nms()
As String =
{"openid","nickname","sex","city","country","province","headimgurl","groupid","remark","language"}
For Each
nm As
String In
nms
dr(nm)
= jo(nm)
Next
If jo("tagid_list")
IsNot Nothing
Then
dr("tagid_list")
= CompressJson(jo("tagid_list")).Trim("[","]")
End
If
dr.Save()
Else
MessageBox.Show(jo.ToString)
End
If
获取全部用户信息
如果希望批量刷新所有用户的信息,可以参考以下代码。
代码首先要获取所有用户的OpenID,且每次只能获取1万个,然后获取用户详情,且每次只能获取100个用户的详情,所以存多个嵌套的循环,是本文档关于微信开发部分最复杂的一段代码。
学习以下代码的时候,请务必结合接口说明来理解。
对于部分用户来说,这段代码可能过于复杂了,如果你确实理解不了,也没有关系,直接使用即可,但如果你真的消化吸收了这段代码,估计今后的第三方接口再也难不倒你了。
具体代码:
DataTables("WXUsers").DataRows.Clear()
Dim
ul As
String = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}"
'获取用户OpenID列表接口
Dim
il = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={0}"
'批量获取用户信息接口
Dim
nms() As
String = {"openid","nickname","sex","city","country","province","headimgurl","groupid","remark","language"}
'列名
Dim
hc As
New HttpClient(CExp(ul,
Functions.Execute("GetAccessToken"),""))
Dim
ids As
New List(of
String)
'OpenId集合,每次获取10000个
Dim
ba As
New Jarray
'准备用来获取用户信息的OpenId列表,一次不能超过100个
Dim
jo As
JObject = JObject.Parse(hc.GetData)
Do
'循环获取,一次只能获取10000个OpenID
If jo("errcode")
Is Nothing
Then
Dim cnt
As Integer =
jo("count")
If cnt
= 0 Then
'如果已经获取完所有用户'
Exit Do
End If
For Each
jk As
JToken In
jo("data")("openid")
ids.Add(jk)
Next
For i
As Integer =
0 To
ids.Count -
1
'循环获取用户详情,一次不能超过100个用户
Dim uo
As New
JObject
uo("openid")
= ids(i)
ba.Add(uo)
If ba.Count
= 100 OrElse
i = ids.Count
- 1 Then
'每满100个就获取一次
Dim
bo As
New Jobject
bo("user_list")
= ba
hc =
New HttpClient(CExp(il,
Functions.Execute("GetAccessToken")))
hc.Content
= bo.ToString()
Dim ro
As JObject =
JObject.Parse(hc.GetData)
If ro("errcode")
Is Nothing
Then
Dim
ia As
JArray = ro("user_info_list")
'获取的用户信息列表
For
Each jt As
JToken In
ia
Dim
dr As
DataRow = DataTables("WXUsers").Find("openid
= '" & jt("openid").ToString()
& "'")
If
dr Is
Nothing Then
dr =
DataTables("WXUsers").AddNew()
End
If
For
Each nm
As String
In nms
dr(nm)
= jt(nm)
Next
dr("tagid_list")
= CompressJSON(jt("tagid_list"))
Next
Else
MessageBox.Show(ro.ToString)
Exit
For
End
If
ba.Clear()
'清除集合,准备获取下一批用户详情,每批只能100个.
End If
Next
ids.Clear
'清除已经获取的OpenID.,准备接收下一批OpenID
hc = New
HttpClient(CExp(ul,
Functions.Execute("GetAccessToken"),jo("next_openid").Tostring))
'获取下一批OpenID
jo = JObject.Parse(hc.GetData)
Else
MessageBox.Show(jo.ToString)
Exit Do
End
If
Loop
DataTables("WXUsers").Save()
获取全部用户信息是一项比较耗时的操作,建议实际开发的时候,用一个进度条显示获取进度。