用表管理菜单
直接为企业号的众多应用编辑JSON格式的菜单,是比较繁琐的,我们可以自己开发一个用表管理菜单的小功能。
1、首先设计一个表,结构如下:
2、AgentID列是整数型,用于输入应用ID;parent列用于输入上级菜单名称,如果不输入,表示此按钮为一级菜单按钮;type为view的按钮,必须输入url值;其它类型的按钮,必须输入key值,否则会出错。
例如:
3、最后在Foxtable的菜单或窗口中增加一个按钮,用于根据上面的表生成应用菜单,按钮代码为:
For
Each AgentID
As String
In DataTables("Menus").GetValues("AgentId")
Dim FRows
As List(of
DataRow) =
DataTables("Menus").Select("AgentId
= " &
AgentID & "
And [parent] Is null","_SortKey")
Dim nms
As String()
= {"type","name","url","key"}
'""
Dim
FButtons As
New Jarray
For Each
FRow As
DataRow In
FRows
Dim
SRows As
List(of
DataRow)
SRows =
DataTables("Menus").Select(CExp("AgentId='{0}'
And [parent] = '{1}'",AgentID,
FRow("name")),"_SortKey")
Dim FButton
As New
JObject
If
sRows.Count =
0 Then
For
Each nm
As String
In nms
If
FRow.IsNull(nm)
= False Then
FButton(nm)
= FRow(nm).ToString()
End
If
Next
Else
Dim
SButtons As
New JArray
For
Each SRow
As DataRow
In SRows
Dim
SButton As
New JObject
For
Each nm
As String
In nms
If
SRow.IsNull(nm)
= False Then
SButton(nm)
= SRow(nm).ToString()
End
If
Next
SButtons.Add(SButton)
Next
FButton("name")
= FRow("name").ToString()
FButton("sub_button")
= SButtons
End If
FButtons.Add(FButton)
Next
Dim MO
As New
JObject
MO("button")
= FButtons
Dim ul
As String =
"https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token={0}&agentid={1}"
Dim
AccessToken = Functions.Execute("GetQYAccessToken")
'AccessTiken
Dim hc
As New
HttpClient(CExp(ul,
AccessToken,
AgentId))
hc.Content
= Mo.ToSTring
Dim jo
As JObject =
JObject.Parse(hc.GetData)
If jo("errcode")
<> "0" Then
MessageBox.Show(jo.ToString)
End
If
Next