| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System.IO;
- using System.Linq;
- using System.Text;
- using MadRunFabric.Common;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- using NLog;
- using Senparc.Weixin.MP.CommonAPIs;
- using Senparc.Weixin.MP.Entities.Menu;
- using WechatApi.Model;
- using MadRunFabric.Model;
- using Microsoft.Extensions.Logging;
- // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
- namespace WechatApi.Controllers
- {
- [ApiVersion("6.0")]
- [Produces("application/json")]
- [Route("api/Menu/[action]")]
- public class MenuController : BaseController
- {
- private readonly string Token;
- private readonly string AppId;
- private readonly string AppSecret;
- private readonly string EncodingAESKey;
- private readonly string MenuJson;
- private readonly ILogger<MenuController> _logger;
- public MenuController(ILogger<MenuController> logger, IConfiguration configuration)
- {
- _logger = logger;
- Token = configuration["WechatStatic:Token"];
- AppId = configuration["WechatStatic:AppId"];
- AppSecret = configuration["WechatStatic:AppSecret"];
- EncodingAESKey = configuration["WechatStatic:EncodingAESKey"];
- MenuJson= configuration["WechatStatic:MenuJson"];
- }
- /// <summary>
- /// 获取自定义菜单
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public IActionResult GetMenus()
- {
- var result = CommonApi.GetMenu(AppId);
- return Success("", result);
- }
- /// <summary>
- /// 创建/更新自定义菜单
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public IActionResult CreateMenus(string btnStr = "")
- {
- if (string.IsNullOrEmpty(btnStr))
- {
- btnStr = MenuJson;
- }
- var buttons = btnStr.ToList<MenuButton>();
- ButtonGroup btnGroup = new ButtonGroup();
- foreach (var button in buttons)
- {
- BaseButton baseButton = null;
- SubButton subButton = null;
- SingleButton singleButton = null;
- if (button.sub_button.Count > 0)
- {
- subButton = new SubButton { name = button.Name };
- foreach (var sub_button in button.sub_button)
- {
- if (sub_button.Type == "view")
- {
- singleButton = new SingleViewButton()
- {
- url = sub_button.Url,
- name = sub_button.Name
- };
- }
- else
- {
- singleButton = new SingleClickButton()
- {
- name = sub_button.Name,
- key = sub_button.Key,
- type = sub_button.Type,
- };
- }
- subButton.sub_button.Add(singleButton);
- }
- }
- else
- {
- if (button.Type == "view")
- {
- baseButton = new SingleViewButton()
- {
- url = button.Url,
- name = button.Name
- };
- }
- else
- {
- baseButton = new SingleClickButton()
- {
- name = button.Name,
- key = button.Key,
- type = button.Type,
- };
- }
- }
- if (subButton != null && subButton.sub_button.Count > 0)
- {
- btnGroup.button.Add(subButton);
- }
- else
- {
- btnGroup.button.Add(baseButton);
- }
- }
- var result = CommonApi.CreateMenu(AppId, btnGroup);
- return Success("设置成功");
- }
- /// <summary>
- /// 删除自定义菜单
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public IActionResult Delete()
- {
- var result = CommonApi.DeleteMenu(AppId);
- return Success("", result);
- }
- }
- }
|