然后我们在Foxtable设计一个按钮用于生成菜单,按钮代码如下:
Dim mrs
As
List(of DataRow) = DataTables("Menu").Select("[parent] is
null","_SortKey") \'删选出一级按钮对应的行
Dim mo As New JObject \'一级菜单对象
Dim ma As New Jarray \'一级菜单按钮数组
mo("button") =
ma \'一级菜单属性就一个button属性,这是一个数组属性
For
Each mr As
DataRow
In mrs
Dim so As New JObject
so("name") =
mr("name").Tostring
Dim
srs As
List(of DataRow) = DataTables("Menu").Select("[parent] =
\'" & mr("name") & "\'", "_SortKey") \'筛选出二级按钮
Dim nms As
String() =
{"type","name","url","key","media_id"} \'注意列名和菜单属性名一直,方便编码,注意大是区分大小写的
If srs.Count = 0
Then
\'如果此一级菜单按钮对应的二级按钮数为0,那么次一级菜单按钮就是一个普通的命令按钮
For
Each nm As
String
In nms \'设置一级菜单按钮的户型
If mr.IsNull(nm) = False
Then
so(nm) = mr(nm).ToString()
End
If
Next
Else
\'如果此一级菜单按钮有对应的二级菜单按钮,那么此一级菜单按钮是一个菜单按钮
Dim sa As New JArray \'定义一个数组,包括此一级按钮下的所有二级按钮
For
Each sr As
DataRow
In srs \'逐个生成二级按钮
Dim jo As New JObject \'定义二级按钮对象
For
Each nm As
String
In nms \'设置二级按钮属性
If
sr.IsNull(nm) = False
Then
jo(nm) = sr(nm).ToString()
End
If
Next
sa.Add(jo)
Next
so("sub_button") = sa \'一级菜单按钮的sub_button属性是一个数组,包括所有二菜单按钮
End
If
ma.Add(so)
Next
Dim
URL
As
String =
"
https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}"
Dim AccessToken As
String = Functions.Execute("GetAccessToken")
Dim hc As New HttpClient(CExp(URL,AccessToken))
hc.Content = mo.Tostring()
Dim ro As JObject = JObject.Parse(hc.GetData)
If ro("errcode") = "0"
Then
MessageBox.Show("菜单设置成功!")
Else
MessageBox.Show(ro("errcode"))
End
If
显然,通过数据表来编写菜单,要轻松很多。