using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace iNethinkCMS.Helper
{
    public class VarifyCode
    {
        /// <summary>
        /// 验证码过期时间,默认10
        /// </summary>
        public static int Expires { get; set; } = 10;
        /// <summary>
        /// 返回 新的验证码
        /// 注:已经做了过期检测,有效期内只返回同一个验证码
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public static int Next(string tel) {
            int code = 0;
            if (HttpContext.Current.Session[tel] != null)
            {
                code = (int)HttpContext.Current.Session[tel];
            }
            else {
                code = new Random().Next(100000, 999999);
                HttpContext.Current.Session[tel] = code;
                HttpContext.Current.Session.Timeout = Expires;
            }
            return code;
        }
        public static bool isCodeValidate(string tel, int code)
        {
            if (code == (int)HttpContext.Current.Session[tel])
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}