using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin.Open.Containers;
using System;
using System.Threading.Tasks;
using YBDevice.Core;

namespace YBDevice.WX.Controllers
{
    public class OpenAuthorizerController : BaseController
    {
        private readonly ILoggerService _loggerService;
        public OpenAuthorizerController(ILoggerService loggerService)
        {
            _loggerService = loggerService;
        }
        /// <summary>
        /// 获取公众号授权信息
        /// </summary>
        /// <param name="authorizerAppid">公众号id</param>
        /// <returns></returns>
        public async Task<ActionResult> GetParam(string authorizerAppid)
        {
            try
            {

                var AuthorizationInfo = await AuthorizerContainer.GetAuthorizationInfoAsync(component_AppId, authorizerAppid);
                var JsApiTicket = await AuthorizerContainer.GetJsApiTicketAsync(component_AppId, authorizerAppid);

                return Json(new
                {
                    state = "success",
                    data = new
                    {
                        AuthorizationInfo = AuthorizationInfo,
                        JsApiTicket = JsApiTicket
                    }
                });

            }
            catch (Exception ex)
            {
                var msg = $"authorizerAppid={authorizerAppid}";
                _loggerService.AddErrorLogger(ex, msg, "获取公众号授权信息");
                return Json(new
                {
                    state = "error",
                    message = ex.Message
                });
            }
        }
    }
}
