一个成员管理系统
前面介绍了企业微信成员管理接口的使用方法,但是真正基于Foxtable设计一个实用的成员管理系统,估计不少用户会感到迷茫。
本节的任务是设计一个简单实用的成员管理系统,你可以通过这个系统成员的增加、删除、修改和批量更新,可以反向操作,从后台下载单个或所有成员信息到系统中。
本系统使用到的表包括Users(成员信息)、Materials(素材信息)、BatchJob(异步任务)三个表,这些表我们已经在前面的章节设计后,如果你忘记了,请回头复习。
其中最重要的是Users表,用于编辑成员信息(你再也不用为生成CSV格式的通讯录文件而烦恼了),建议将department列设置为多值字段,方便编辑:
本节的任务是设计一个下图所示的窗口,完成日常的成员管理工作:
下载当前用户信息
你可以在Users表中选定一行,然后单击按钮“下载但前用户信息”,即可从微信后台下载此用户的信息覆盖当前行。
代码为:
Dim
r As
Row = Tables("Users").Current
Dim
nms() As
String = {"userid","name","position","mobile","gender","email","weixinid","avatar","status"}
'""
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}"
ul=
CExp(ul,Functions.Execute("GetQYAccessToken"),r("userid"))
Dim
hc As
new HttpClient(ul)
Dim
jo As
JObject = JObject.Parse(hc.GetData)
For
Each nm
As String
In nms
r(nm)
= jo(nm)
Next
If
jo("department")
IsNot Nothing
r("department")
= CompressJson(jo("department")).Trim("[","]")
End
If
If
jo("extattr")
IsNot Nothing
Then
r("extattr")
= ComPressJson(jo("extattr")("attrs")).Trim("[","]")
End
If
下载所有用户信息按钮
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
Dim nms()
As String =
{"userid","name","position","mobile","gender","email","weixinid","avatar","status"}
'""
For Each
jt As
JToken In
jo("userlist")
Dim dr
As DataRow =
DataTables("Users").Find("userid
= '" & jt("userid").ToString
& "'")
If dr
Is Nothing
Then
dr =
DataTables("Users").AddNew()
End If
For Each
nm As
String In
nms
dr(nm)
= jt(nm)
Next
If jt("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
更新\创建用户
此按钮兼具创建用户和更新用户的功能。
单击此按钮,首先检查后台是否存在此用户,如果存在,则用当前行数据更新此用户信息,如果不存在,则根据当前行数据创建一个新的用户。
按钮代码:
Dim
r As
Row = Tables("Users").Current
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}"
ul=
CExp(ul,Functions.Execute("GetQYAccessToken"),r("userid"))
Dim
hc As
new HttpClient(ul)
Dim
jo As
JObject = JObject.Parse(hc.GetData)
If
jo("errcode")=
"60111" Then
'如果不存在此成员
ul =
"https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={0}"
'使用新增成员接口
ElseIf
jo("errcode")
= "0" Then
'如果存在此用户
ul =
"https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={0}"
'使用更新成员接口
Else
'如果发生错误
MessageBox.Show(jo.ToString)'
Return
End
If
hc =
New HttpClient(CExp(ul,Functions.Execute("GetQYAccessToken")))
jo =
New
JObject
Dim
nms() As
String =
{"userid","name","position","mobile","gender","email","weixinid"}
For
Each nm
As String
In nms
jo(nm)
= r(nm).ToString
Next
jo("department")
= New Jarray(r("department").Split(","))
hc.Content
= jo.Tostring
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("用户更新/更新成功")
Else
MessageBox.Show(jo.ToString)
End If
更新所有用户信息
单击此按钮会用Users表中的数据更i性能后台通讯录,对于后台已经存在的用户,会用User表中的数据更新其信息,对于后台不存在的用户,会基于User表中的数据创建新用户。
其实这个一个按钮完成了三项任务:
1、基于Users表的数据生成一个csv格式的通讯略文件,此文件必须严格按照腾讯官方的模版生成,且编码必须为UTF8。
2、上传生成好的通讯录文件到微信服务器。
3、通知微信服务器执行一个异步任务,基于此通讯录文件更新后台通讯录。
具体代码:
'第一步:按照腾讯官方提供的模板生成CSV格式的通讯录文件
Dim
sb As
New StringBuilder
sb.AppendLine("姓名,帐号,微信号,手机号,邮箱,所在部门,职位")
Dim
nms() As
String = {"name","userid","weixinid","mobile","email","department","position"}
'""
For
Each r
As Row
In Tables("Users").Rows
For i
As Integer =
0 To
nms.Length -1
If nms(i)
= "department" Then
sb.Append(r(nms(i)).Replace(",",
";")) '注意CSV文件中的部门要用分号隔开的.
Else
sb.Append(r(nms(i)))
End If
If i
= nms.Length-1
Then
sb.AppendLine()
Else
sb.Append(",")
End If
Next
Next
Dim
fln As
String =
"c:\data\newuers.csv"
FileSys.WriteAllText(fln,sb.ToString,False,Encoding.utf8)
'第二步:上传生成好的模板文件,并获取media_id
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/material/add_material?type=file&access_token={0}"
Dim
hc As
new HttpClient(CExp(ul,
Functions.Execute("GetQYAccessToken")))
Dim
mid As
String '
hc.Files.Add("media",fln)
'指定要上传的普通文件
Dim
jo As
JObject = JObject.Parse(
hc.getdata)
If
jo("errcode")
= "0" Then
Dim dr
As DataRow =
DataTables("Materials").AddNew()
dr("type")=
"file"
dr("mediaID")
= jo("media_id")
dr("UpdateTime")
= Date.Now
dr("FileName")=
FileSys.GetName(fln)
dr.Save()
mid = jo("media_id")
Else
MessageBox.Show(jo.ToString)
Return
End
If
'第三步:向服务器提交异步更新通讯录任务
ul =
"https://qyapi.weixin.qq.com/cgi-bin/batch/syncuser?access_token={0}"
hc =
New HttpClient(CExp(ul,
Functions.Execute("GetQYAccessToken")))
jo =
New JObject
jo("media_id")
= mid '上一步获取的上传后的通讯录文件的media_id
jo("callback")
= New JObject
jo("callback")("url")
= "http://120.198.123.19/wefox/"
jo("callback")("token")
= "foxtable"
jo("callback")("encodingaeskey")
= "ilsmyivvRPNj0qxSiSzWCnqm7cy1w1RcS6w2LBhsh7J"
hc.Content
= jo.Tostring()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
Dim dr
As DataRow =
DataTables("BatchJob").AddNew()
dr("JobId")
= jo("jobid")
dr.Save()
Else
MessageBox.Show(jo.ToString)
End
If
关于接收获取异步任务执行完毕通知,以及获取异步任务执行结果,请参考上一节《异步接口》。
删除当前用户
删除当前用户按钮的代码很简单:
Dim
r As
Row = Tables("Users").Current
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"),r("userid")))
Dim
jo As
JObject = JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
r.Delete
MessageBox.Show("用户删除成功")
DataTables("Users").Save()
Else
MessageBox.Show(jo.ToString)
End
If