CommonApi.Menu.Conditional.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:CommonApi.Menu.Conditional
  17. 文件功能描述:个性化自定义菜单接口
  18. 创建标识:Senparc - 20151222
  19. 修改标识:Senparc - 20151222
  20. 修改描述:v13.5.1 添加个性化菜单接口
  21. 修改标识:Senparc - 20170317
  22. 修改描述:v14.3.133 修复CommonApi.CreateMenuConditional()方法调用出现“invalid button size”错误的问题
  23. ----------------------------------------------------------------*/
  24. /*
  25. API:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
  26. */
  27. using Senparc.CO2NET.Extensions;
  28. using Senparc.CO2NET.Helpers.Serializers;
  29. using Senparc.NeuChar;
  30. using Senparc.Weixin.CommonAPIs;
  31. using Senparc.Weixin.Entities;
  32. using Senparc.Weixin.Helpers;
  33. using Senparc.Weixin.HttpUtility;
  34. using Senparc.Weixin.MP.Entities;
  35. using Senparc.Weixin.MP.Entities.Menu;
  36. namespace Senparc.Weixin.MP.CommonAPIs
  37. {
  38. public partial class CommonApi
  39. {
  40. /// <summary>
  41. /// 创建个性化菜单
  42. /// </summary>
  43. /// <param name="accessTokenOrAppId">AccessToken或AppId。当为AppId时,如果AccessToken错误将自动获取一次。当为null时,获取当前注册的第一个AppId。</param>
  44. /// <param name="buttonData">菜单内容</param>
  45. /// <returns></returns>
  46. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.CreateMenuConditional", true)]
  47. public static CreateMenuConditionalResult CreateMenuConditional(string accessTokenOrAppId, ConditionalButtonGroup buttonData, int timeOut = Config.TIME_OUT)
  48. {
  49. return ApiHandlerWapper.TryCommonApi(accessToken =>
  50. {
  51. var urlFormat = Config.ApiMpHost + "/cgi-bin/menu/addconditional?access_token={0}";
  52. //var jsonSetting = new JsonSetting(true);//设置成true会导致发布失败
  53. var jsonSetting = new JsonSetting(false);
  54. return CommonJsonSend.Send<CreateMenuConditionalResult>(accessToken, urlFormat, buttonData, timeOut: timeOut, jsonSetting: jsonSetting);
  55. }, accessTokenOrAppId);
  56. }
  57. #region GetMenu
  58. /* 使用普通自定义菜单查询接口可以获取默认菜单和全部个性化菜单信息,请见自定义菜单查询接口的说明 */
  59. /// <summary>
  60. /// 测试个性化菜单匹配结果
  61. /// </summary>
  62. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  63. /// <param name="userId">可以是粉丝的OpenID,也可以是粉丝的微信号。</param>
  64. /// <returns></returns>
  65. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.TryMatch", true)]
  66. public static MenuTryMatchResult TryMatch(string accessTokenOrAppId, string userId)
  67. {
  68. return ApiHandlerWapper.TryCommonApi(accessToken =>
  69. {
  70. var url = string.Format(Config.ApiMpHost + "/cgi-bin/menu/trymatch?access_token={0}", accessToken.AsUrlData());
  71. var data = new
  72. {
  73. user_id = userId
  74. };
  75. return CommonJsonSend.Send<MenuTryMatchResult>(accessToken, url, data, CommonJsonSendType.POST);
  76. }, accessTokenOrAppId);
  77. }
  78. #endregion
  79. /// <summary>
  80. /// 删除个性化菜单
  81. /// </summary>
  82. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  83. /// <param name="menuId">菜单Id</param>
  84. /// <returns></returns>
  85. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "CommonApi.DeleteMenuConditional", true)]
  86. public static WxJsonResult DeleteMenuConditional(string accessTokenOrAppId, string menuId)
  87. {
  88. return ApiHandlerWapper.TryCommonApi(accessToken =>
  89. {
  90. var url = string.Format(Config.ApiMpHost + "/cgi-bin/menu/delconditional?access_token={0}", accessToken.AsUrlData());
  91. var data = new
  92. {
  93. menuid = menuId
  94. };
  95. return CommonJsonSend.Send(accessToken, url, data, CommonJsonSendType.POST);
  96. }, accessTokenOrAppId);
  97. }
  98. /* 使用普通自定义菜单删除接口可以删除所有自定义菜单(包括默认菜单和全部个性化菜单),请见自定义菜单删除接口的说明。 */
  99. }
  100. }