成员接口
和公众号人人都能关注不同,企业微信只有在通讯录中的用户才能关注,本接口用于管理通讯录中的成员。
创建成员
创建成员的代码可以参考:
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={0}"
Dim
hc As
New HttpClient(CExp(ul,Functions.Execute("GetQYAccessToken")))
Dim
jo As
New
JObject
jo("userid")
= "zhangsan"
jo("name")
= "赵六"
jo("department")
= New Jarray(3,9)
'部门ID为3和9
jo("position")
= "产品经理"
jo("mobile")=
"15900000000"
jo("gender")
= "1"
jo("email")
= "zhangsan@gzdev.com"
jo("address")
= "广州市海珠区新港中路"
jo("avatar_mediaid")
=
"2ajDqSlzedZ5A3eFCAb73lbjkSkkbliE0u0oq6xXNtM49xvyJ7O6k7JBC10V5wfR5IONGxoiqG9EOs5H9LB_dzw"
hc.Content
= Jo.ToString
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("用户创建成功")
Else
MessageBox.Show(jo.ToString)
End
If
提示:
1、创建成员可以指定的属性很多,但是只有userid、name和department三个属性是必须指定的。
2、mobile、email和wexindid三者必须至少指定一个。
更新成员
例如要将用户"zhaoliu"的职务改为"总经理",可以参考代码:
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={0}"
Dim
hc As
New HttpClient(CExp(ul,Functions.Execute("GetQYAccessToken")))
Dim
jo As
New
JObject
jo("userid")
= "zhaoliu"
jo("position")
= "总经理"
hc.Content
= jo.ToString
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("用户更新成功")
Else
MessageBox.Show(jo.ToString)
End
If
提示:只需指定要修改的属性值即可,未指定值的属性将保持不变。
删除成员
例如要删除userid为"zhaoliu"的成员,可以参考代码:
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={0}&userid={1}"
Dim
hc As
New HttpClient(CExp(ul,Functions.Execute("GetQYAccessToken"),"zhaoliu"))
Dim
jo As
JObject = JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("用户删除成功")
Else
MessageBox.Show(jo.ToString)
End
If
批量删除成员
可以一次删除多个成员,代码参考:
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token={0}"
Dim
hc As
New HttpClient(CExp(ul,Functions.Execute("GetQYAccessToken")))
Dim
jo As
New
JObject
jo("useridlist")
= New Jarray("laosan","zhaoliu",
"yanggang")
hc.Content=
jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("用户删除成功")
Else
MessageBox.Show(jo.ToString)
End
If
获取指定成员信息
要获取指定成员信息,可参考代码:
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}"
ul=
CExp(ul,Functions.Execute("GetQYAccessToken"),"yanggang")
Dim
hc As
new HttpClient(ul)
Dim
jo As
JObject = JObject.Parse(hc.GetData)
MessageBox.Show(jo.ToString)
获取成员列表
首先我们新建一个名为Users的表,用于管理成员,表结构为:
实际上我们只能获取指定部门的成员列表,如果要获取所有成员列表,只需将部门id设置为1,因为1表示根部门,根部门包括所有成员。
成员列表只返回成员的userid、name、department和open_userid四个字段的信息。
获取成员列表保存在Users表中,可以参考代码:
Dim
ur As
String =
Dim
hc As
New HttpClient(CExp(ur,Functions.Execute("GetQYAccessToken")))
Dim
jo As
JObject = JObject.Parse(hc.GetData())
If
jo("errcode")
= "0" Then
DataTables("Users").DataRows.Clear
For Each
jt As
JToken In
jo("userlist")
Dim dr
As DataRow =
DataTables("Users").AddNew()
dr("userid")
= jt("userid")
dr("name")
= jt("name")
dr("open_userid")
= jt("open_userid")
If jt("department")
IsNot Nothing
Then
dr("department")
= CompressJSon(jt("department")).Trim("[","]")
End If
Next
DataTables("Users").Save()
Else
MessageBox.Show(jo.ToString)
End
If
获取成员详情列表
同样要获取所有成员的详情列表,需要将部门id参数设置为1,详情略表能和获取成员的全部信息。
获取成员详情列表保存在Users表中,可参考代码:
Dim
ur As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={0}&department_id=1&fetch_child=1&status=0"
Dim
hc As
New HttpClient(CExp(ur,Functions.Execute("GetQYAccessToken")))
Dim
jo As
JObject = JObject.Parse(hc.GetData())
If
jo("errcode")
= "0" Then
DataTables("Users").DataRows.Clear
Dim nms()
As String =
{"userid","name","position","mobile","gender","email","open_userid","avatar","status"}
'""
For Each
jt As
JToken In
jo("userlist")
Dim dr
As DataRow =
DataTables("Users").AddNew()
For Each
nm As
String In
nms
dr(nm)
= jt(nm)
Next
If dr("department")
IsNot Nothing
dr("department")
= CompressJson(jt("department")).Trim("[","]")
End If
If jt("extattr")
IsNot Nothing
Then
dr("extattr")
= ComPressJson(jt("extattr")("attrs")).Trim("[","]")
End If
Next
DataTables("Users").Save()
Else
MessageBox.Show(jo.ToString)
End
If