颐和api

MenuController.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.IO;
  2. using System.Linq;
  3. using System.Text;
  4. using MadRunFabric.Common;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using Newtonsoft.Json;
  8. using NLog;
  9. using Senparc.Weixin.MP.CommonAPIs;
  10. using Senparc.Weixin.MP.Entities.Menu;
  11. using WechatApi.Model;
  12. using MadRunFabric.Model;
  13. using Microsoft.Extensions.Logging;
  14. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  15. namespace WechatApi.Controllers
  16. {
  17. [ApiVersion("6.0")]
  18. [Produces("application/json")]
  19. [Route("api/Menu/[action]")]
  20. public class MenuController : BaseController
  21. {
  22. private readonly string Token;
  23. private readonly string AppId;
  24. private readonly string AppSecret;
  25. private readonly string EncodingAESKey;
  26. private readonly string MenuJson;
  27. private readonly ILogger<MenuController> _logger;
  28. public MenuController(ILogger<MenuController> logger, IConfiguration configuration)
  29. {
  30. _logger = logger;
  31. Token = configuration["WechatStatic:Token"];
  32. AppId = configuration["WechatStatic:AppId"];
  33. AppSecret = configuration["WechatStatic:AppSecret"];
  34. EncodingAESKey = configuration["WechatStatic:EncodingAESKey"];
  35. MenuJson= configuration["WechatStatic:MenuJson"];
  36. }
  37. /// <summary>
  38. /// 获取自定义菜单
  39. /// </summary>
  40. /// <returns></returns>
  41. [HttpGet]
  42. public IActionResult GetMenus()
  43. {
  44. var result = CommonApi.GetMenu(AppId);
  45. return Success("", result);
  46. }
  47. /// <summary>
  48. /// 创建/更新自定义菜单
  49. /// </summary>
  50. /// <returns></returns>
  51. [HttpPost]
  52. public IActionResult CreateMenus(string btnStr = "")
  53. {
  54. if (string.IsNullOrEmpty(btnStr))
  55. {
  56. btnStr = MenuJson;
  57. }
  58. var buttons = btnStr.ToList<MenuButton>();
  59. ButtonGroup btnGroup = new ButtonGroup();
  60. foreach (var button in buttons)
  61. {
  62. BaseButton baseButton = null;
  63. SubButton subButton = null;
  64. SingleButton singleButton = null;
  65. if (button.sub_button.Count > 0)
  66. {
  67. subButton = new SubButton { name = button.Name };
  68. foreach (var sub_button in button.sub_button)
  69. {
  70. if (sub_button.Type == "view")
  71. {
  72. singleButton = new SingleViewButton()
  73. {
  74. url = sub_button.Url,
  75. name = sub_button.Name
  76. };
  77. }
  78. else
  79. {
  80. singleButton = new SingleClickButton()
  81. {
  82. name = sub_button.Name,
  83. key = sub_button.Key,
  84. type = sub_button.Type,
  85. };
  86. }
  87. subButton.sub_button.Add(singleButton);
  88. }
  89. }
  90. else
  91. {
  92. if (button.Type == "view")
  93. {
  94. baseButton = new SingleViewButton()
  95. {
  96. url = button.Url,
  97. name = button.Name
  98. };
  99. }
  100. else
  101. {
  102. baseButton = new SingleClickButton()
  103. {
  104. name = button.Name,
  105. key = button.Key,
  106. type = button.Type,
  107. };
  108. }
  109. }
  110. if (subButton != null && subButton.sub_button.Count > 0)
  111. {
  112. btnGroup.button.Add(subButton);
  113. }
  114. else
  115. {
  116. btnGroup.button.Add(baseButton);
  117. }
  118. }
  119. var result = CommonApi.CreateMenu(AppId, btnGroup);
  120. return Success("设置成功");
  121. }
  122. /// <summary>
  123. /// 删除自定义菜单
  124. /// </summary>
  125. /// <returns></returns>
  126. [HttpGet]
  127. public IActionResult Delete()
  128. {
  129. var result = CommonApi.DeleteMenu(AppId);
  130. return Success("", result);
  131. }
  132. }
  133. }