
using PageAdmin.BLL;
using PageAdmin.Utils;
using System;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Member.Controllers.FindPassword
{

    /// <summary>
    /// 找回密码
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class FirstValidateAttribute: ActionFilterAttribute
    {
        private TipsInfo _TipsInfo = null;
        private dynamic _ConfigData = null;

        //检测验证码，判断登录等等
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _TipsInfo = new TipsInfo();
            ReadConfig();//读取配置文件
            CheckOpen(filterContext);
            ValidateVCode(filterContext);//检测验证码
        }

        /// <summary>
        /// 获取配置文件
        /// </summary>
        private void ReadConfig()
        {
            _ConfigData= XmlHelper.AllNodeValue("configuration", "/Config/MemberFindPassword.config", true);
        }

        /// <summary>
        /// 检测开关
        /// </summary>
        /// <param name="filterContext"></param>
        private void CheckOpen(ActionExecutingContext filterContext)
        {
            string open = _ConfigData.Open;
            if (open == "0")
            {
                _TipsInfo.Msg = "对不起，找回密码功能已关闭，详情请联系管理员！";
                _TipsInfo.State = 0;
                filterContext.Result = new JsonResult()
                {
                    Data = _TipsInfo
                };
            }
        }

        /// <summary>
        /// 检测验证码
        /// </summary>
        /// <param name="filterContext"></param>
        private void ValidateVCode(ActionExecutingContext filterContext)
        {
            if(_ConfigData.VerificationCode=="0")
            {
                return;
            }
            var request = filterContext.RequestContext.HttpContext.Request;
            string validateCode = StringHelper.Format<string>(request.Form["_VerificationCode"]);
            VerificationCodeBLL verificationCodeBLL = new VerificationCodeBLL();
            _TipsInfo = verificationCodeBLL.Validate(validateCode);
            if (_TipsInfo.State!=1)
            {
                filterContext.Result = new JsonResult()
                {
                    Data = _TipsInfo
                };
            }
        }


    }
}
