using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Nirvana.Common.ApiBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YBDevice.NApi.Application.ThirdOpen
{
    /// <summary>
    /// 接口权限验证
    /// </summary>
    public class ThirdAuthorizeFilter : Attribute, IAsyncAuthorizationFilter
    {
        /// <summary>
        /// 权限校检
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            var token = context.HttpContext.Request.Headers["token"].ToString();
            if (!string.IsNullOrEmpty(token))
            {
                string ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
                //if (await BaseService.DecodeTokenAsync(token, ip))
                //{
                //    return;
                //}
            }
            var result = new ResultInfo { code = ResultState.NOAUTH, message = "没有访问权限" };
            context.Result = new JsonResult(result);
            return Task.CompletedTask;
        }
    }
}
