Exemplo n.º 1
0
        public async Task <IActionResult> CreateAppMenu([FromBody] List <WxMenu> menus, [FromServices] AppData appData, [FromServices] IUtilRepository util)
        {
            var business = HttpContext.Session.Get <Business>(appData.Session);

            if (business == null)
            {
                throw new Exception("请先登录");
            }
            var token = await util.GetTokenAsync(business.WeChatAppId, business.WeChatSecret);

            await WxHelper.DeleteAppMenuAsync(token);

            if (menus == null || menus.Count == 0)
            {
                return(Ok());
            }
            var list = new List <object>();

            menus.ForEach(menu =>
            {
                if (menu.type == WxMenuCategory.none)
                {
                    var subList = new List <object>();
                    if (menu.sub_button != null && menu.sub_button.Count > 0)
                    {
                        menu.sub_button.ForEach(a => subList.Add(new { type = a.type.ToString(), a.name, a.key, url = FilterUrl(a.url), a.appid, a.pagepath, a.media_id }));
                    }
                    ;
                    list.Add(new
                    {
                        menu.name,
                        sub_button = subList
                    });
                    return;
                }
                list.Add(new { type = menu.type.ToString(), menu.name, menu.key, url = FilterUrl(menu.url), menu.appid, menu.pagepath, menu.media_id });
            });
            var result = await WxHelper.CreateAppMenuAsync(list, token);

            return(Content(result));
        }