From 58c3bcc0f321eec71ba6cb42ce4c3ba34d78bb29 Mon Sep 17 00:00:00 2001 From: jianweie Date: Fri, 5 May 2023 15:58:40 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=85=AC=E4=BC=97=E5=8F=B7=E7=AE=A1=E7=90=86=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E4=BA=8C=E7=BA=A7=E8=8F=9C=E5=8D=95=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreCms.Net.Model/CoreCms.Net.Model.xml | 55 +++++++++++++++++++ .../MenuFull_ButtonGroupForSkit.cs | 8 ++- .../WeChatEntites/MenuFull_RootButton.cs | 54 ++++++++++++++++++ .../WeChat/WeChatMenuController.cs | 50 ++++++++++++++++- .../wechatoffiaccount/menusetting/index.html | 11 +++- 5 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 CoreCms.Net.Model/WeChatEntites/MenuFull_RootButton.cs diff --git a/CoreCms.Net.Model/CoreCms.Net.Model.xml b/CoreCms.Net.Model/CoreCms.Net.Model.xml index 12274dd0..90b5e8ef 100644 --- a/CoreCms.Net.Model/CoreCms.Net.Model.xml +++ b/CoreCms.Net.Model/CoreCms.Net.Model.xml @@ -13450,6 +13450,61 @@ 订单号 + + + 公众号菜单按钮提交参数类 + + + + + 按钮列表 + + + + + 按钮具体类型 + + + + + 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型 + + + + + 菜单KEY值,用于消息接口推送,不超过128字节 + + + + + 菜单标题,不超过16个字节,子菜单不超过60个字节 + + + + + 网页 链接,用户点击菜单可打开链接,不超过1024字节。 type为miniprogram时,不支持小程序的老版本客户端将打开本url。 + + + + + 小程序的appid(仅认证公众号可配置) + + + + + 小程序的页面路径 + + + + + 调用新增永久素材接口返回的合法media_id + + + + + 二级菜单数组,个数应为1~5个 + + 扩展 表示 [POST] /wxa/business/getliveinfo 接口的请求。 diff --git a/CoreCms.Net.Model/WeChatEntites/MenuFull_ButtonGroupForSkit.cs b/CoreCms.Net.Model/WeChatEntites/MenuFull_ButtonGroupForSkit.cs index cb87ca85..f0cc7f81 100644 --- a/CoreCms.Net.Model/WeChatEntites/MenuFull_ButtonGroupForSkit.cs +++ b/CoreCms.Net.Model/WeChatEntites/MenuFull_ButtonGroupForSkit.cs @@ -7,8 +7,14 @@ using SKIT.FlurlHttpClient.Wechat.Api.Models; namespace CoreCms.Net.Model.WeChatEntites { + /// + /// 公众号菜单按钮提交参数类 + /// public class MenuFull_ButtonGroupForSkit { - public List button { get; set; } + /// + /// 按钮列表 + /// + public List button { get; set; } } } diff --git a/CoreCms.Net.Model/WeChatEntites/MenuFull_RootButton.cs b/CoreCms.Net.Model/WeChatEntites/MenuFull_RootButton.cs new file mode 100644 index 00000000..3d603683 --- /dev/null +++ b/CoreCms.Net.Model/WeChatEntites/MenuFull_RootButton.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CoreCms.Net.Model.WeChatEntites +{ + /// + /// 按钮具体类型 + /// + public class MenuFull_RootButton + { + /// + /// 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型 + /// + public string type { get; set; } + + /// + /// 菜单KEY值,用于消息接口推送,不超过128字节 + /// + public string key { get; set; } + + /// + /// 菜单标题,不超过16个字节,子菜单不超过60个字节 + /// + public string name { get; set; } + + /// + /// 网页 链接,用户点击菜单可打开链接,不超过1024字节。 type为miniprogram时,不支持小程序的老版本客户端将打开本url。 + /// + public string url { get; set; } + + /// + /// 小程序的appid(仅认证公众号可配置) + /// + public string appid { get; set; } + + /// + /// 小程序的页面路径 + /// + public string pagepath { get; set; } + + /// + /// 调用新增永久素材接口返回的合法media_id + /// + public string media_id { get; set; } + + /// + /// 二级菜单数组,个数应为1~5个 + /// + public List sub_button { get; set; } + } +} diff --git a/CoreCms.Net.Web.Admin/Controllers/WeChat/WeChatMenuController.cs b/CoreCms.Net.Web.Admin/Controllers/WeChat/WeChatMenuController.cs index 095decf5..1d87dc0b 100644 --- a/CoreCms.Net.Web.Admin/Controllers/WeChat/WeChatMenuController.cs +++ b/CoreCms.Net.Web.Admin/Controllers/WeChat/WeChatMenuController.cs @@ -18,6 +18,7 @@ using SKIT.FlurlHttpClient.Wechat.Api.Models; using CoreCms.Net.WeChat.Service.HttpClients; using CoreCms.Net.WeChat.Service.Enums; using SKIT.FlurlHttpClient.Wechat.Api; +using Microsoft.IdentityModel.Tokens; namespace CoreCms.Net.Web.Admin.Controllers.Api.WeChat { @@ -70,6 +71,53 @@ namespace CoreCms.Net.Web.Admin.Controllers.Api.WeChat public async Task CreateMenu(MenuFull_ButtonGroupForSkit menu) { var jm = new AdminUiCallBack(); + + if (!menu.button.Any()) + { + jm.msg = "请不要提交空的菜单数据"; + return jm; + } + + var skitButtons = new List(); + + foreach (var itemParent in menu.button) + { + if (string.IsNullOrEmpty(itemParent.name) && string.IsNullOrEmpty(itemParent.name)) continue; + + var bt = new CgibinMenuCreateRequest.Types.Button + { + Type = itemParent.type, + Key = itemParent.key, + Name = itemParent.name, + Url = itemParent.url, + MiniProgramAppId = itemParent.appid, + MiniProgramPagePath = itemParent.pagepath, + MediaId = itemParent.media_id, + ArticleId = "", + SubButtonList = new List() + }; + + if (!itemParent.sub_button.Any()) continue; + + foreach (var btChild in from itemChild in itemParent.sub_button + where !string.IsNullOrEmpty(itemChild.key) || !string.IsNullOrEmpty(itemChild.name) + select new CgibinMenuCreateRequest.Types.Button + { + Type = itemChild.type, + Key = itemChild.key, + Name = itemChild.name, + Url = itemChild.url, + MiniProgramAppId = itemChild.appid, + MiniProgramPagePath = itemChild.pagepath, + MediaId = itemChild.media_id, + ArticleId = "" + }) + { + bt.SubButtonList.Add(btChild); + } + skitButtons.Add(bt); + } + try { var client = _weChatApiHttpClientFactory.CreateWeXinClient(); @@ -77,7 +125,7 @@ namespace CoreCms.Net.Web.Admin.Controllers.Api.WeChat var request = new CgibinMenuCreateRequest() { AccessToken = accessToken, - ButtonList = menu.button + ButtonList = skitButtons }; var response = await client.ExecuteCgibinMenuCreateAsync(request, HttpContext.RequestAborted); diff --git a/CoreCms.Net.Web.Admin/wwwroot/views/wechatoffiaccount/menusetting/index.html b/CoreCms.Net.Web.Admin/wwwroot/views/wechatoffiaccount/menusetting/index.html index 8ab983dd..99ba0d0d 100644 --- a/CoreCms.Net.Web.Admin/wwwroot/views/wechatoffiaccount/menusetting/index.html +++ b/CoreCms.Net.Web.Admin/wwwroot/views/wechatoffiaccount/menusetting/index.html @@ -9,8 +9,15 @@