#region Apache License Version 2.0
/*----------------------------------------------------------------
Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
----------------------------------------------------------------*/
#endregion Apache License Version 2.0
/*----------------------------------------------------------------
Copyright (C) 2019 Senparc
文件名:CommonApi.Menu.Custom.cs
文件功能描述:通用自定义菜单接口(自定义接口)
创建标识:Senparc - 20150211
修改标识:Senparc - 20150303
修改描述:整理接口
修改标识:Senparc - 20150312
修改描述:开放代理请求超时时间
修改标识:Senparc - 201503232
修改描述:修改字符串是否为空判断方式(感谢dusdong)
修改标识:Senparc - 20150703
修改描述:改用accessTokenOrAppId参数
修改标识:IsaacXu - 20151222
修改描述:添加CreateMenu重写方法
修改标识:Senparc - 20180928
修改描述:添加GetCurrentSelfMenuInfo方法
----------------------------------------------------------------*/
/*
API:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html
*/
using System;
using System.Collections.Generic;
using System.Text;
using Senparc.CO2NET.Extensions;
using Senparc.CO2NET.HttpUtility;
using Senparc.Weixin.Entities;
using Senparc.Weixin.Exceptions;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Menu;
using Senparc.NeuChar;
using System.Threading.Tasks;
using Senparc.Weixin.CommonAPIs;
using Senparc.CO2NET.Helpers;
#if NET35 || NET40 || NET45
using System.Web.Script.Serialization;
#endif
namespace Senparc.Weixin.MP.CommonAPIs
{
public partial class CommonApi
{
/////
///// 特殊符号转义
/////
/////
/////
//private static string ButtonNameEncode(string name)
//{
// //直接用UrlEncode不行,显示内容超长
// return name.Replace("&", "%26");
//}
///
/// 创建菜单
///
/// AccessToken或AppId。当为AppId时,如果AccessToken错误将自动获取一次。当为null时,获取当前注册的第一个AppId。
/// 菜单内容
///
///
[ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.CreateMenu", true)]
public static WxJsonResult CreateMenu(string accessTokenOrAppId, ButtonGroup buttonData, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
var urlFormat = Config.ApiMpHost + "/cgi-bin/menu/create?access_token={0}";
////对特殊符号进行URL转义
//foreach (var button in buttonData.button)
//{
// button.name = ButtonNameEncode(button.name);//button.name.UrlEncode();
// if (button is SubButton)
// {
// var subButtonList = button as SubButton;
// foreach (var subButton in subButtonList.sub_button)
// {
// subButton.name = ButtonNameEncode(button.name);//button.name.UrlEncode();
// }
// }
//}
return CommonJsonSend.Send(accessToken, urlFormat, buttonData, timeOut: timeOut);
}, accessTokenOrAppId);
}
///
/// 创建菜单
///
/// AccessToken或AppId。当为AppId时,如果AccessToken错误将自动获取一次。当为null时,获取当前注册的第一个AppId。
/// 菜单内容
///
///
[ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.CreateMenu", true)]
public static WxJsonResult CreateMenu(string accessTokenOrAppId, object buttonData, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
var urlFormat = Config.ApiMpHost + "/cgi-bin/menu/create?access_token={0}";
return CommonJsonSend.Send(accessToken, urlFormat, buttonData, timeOut: timeOut);
}, accessTokenOrAppId);
}
#region GetMenu
///
/// 获取单击按钮
///
///
///
[Obsolete("配合GetMenuFromJson方法使用")]
[ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetSingleButtonFromJsonObject", true)]
private static SingleClickButton GetSingleButtonFromJsonObject(Dictionary objs)
{
var sb = new SingleClickButton()
{
key = objs["key"] as string,
name = objs["name"] as string,
type = objs["type"] as string
};
return sb;
}
///
/// 从JSON字符串获取菜单对象
///
///
///
[Obsolete("此方法通过判断GetMenuResult并结合object类型转换得到结果。结果准确。但更推荐使用GetMenuFromJsonResult方法。")]
[ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.GetMenuFromJson", true)]
public static GetMenuResult GetMenuFromJson(string jsonString)
{
var finalResult = new GetMenuResult(new ButtonGroup());
try
{
//@"{""menu"":{""button"":[{""type"":""click"",""name"":""单击测试"",""key"":""OneClick"",""sub_button"":[]},{""name"":""二级菜单"",""sub_button"":[{""type"":""click"",""name"":""返回文本"",""key"":""SubClickRoot_Text"",""sub_button"":[]},{""type"":""click"",""name"":""返回图文"",""key"":""SubClickRoot_News"",""sub_button"":[]},{""type"":""click"",""name"":""返回音乐"",""key"":""SubClickRoot_Music"",""sub_button"":[]}]}]}}"
object jsonResult = SerializerHelper.GetObject