using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using System.Web;
using MySystem.Models.Main1;
using LitJson;
using Library;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class MobileCodeCheckController : BaseController
    {
        public MobileCodeCheckController(IHttpContextAccessor accessor) : base(accessor)
        {
        }



        #region 我的-发送验证码
        // [Authorize]
        public JsonResult SendSms(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = SendSmsDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson SendSmsDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string Mobile = data.getItem("Mobile").ToString(); //手机号
            MobileCodeCheck check = RedisDbconn.Instance.Get<MobileCodeCheck>("MobileCodeCheck:" + Mobile);
            if (check != null)
            { 
                return new AppResultJson() { Status = "1", Info = "", Data = Obj };
            }
            // string CheckCode = "111111";
            string CheckCode = function.get_Random(6);
            MobileCodeCheck query = new MobileCodeCheck()
            {
                Mobile = Mobile, //手机号
                CheckCode = CheckCode,
            };
            RedisDbconn.Instance.Set("MobileCodeCheck:" + Mobile, query);
            RedisDbconn.Instance.SetExpire("MobileCodeCheck:" + Mobile, 300);

            SendSMS.Instance.Do(Mobile, CheckCode); //发送短信
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion



        #region 检查签名是否合法，合法返回1，不合法返回提示信息

        /// <summary>
        /// 检查签名是否合法，合法返回1，不合法返回提示信息
        /// </summary>
        /// <param name="value">请求的参数(json字符串)</param>
        /// <param name="signField">要签名的字段</param>
        /// <returns></returns>
        private string CheckSign(string value, string[] signField)
        {
            JsonData json = JsonMapper.ToObject(value);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < signField.Length; i++)
            {
                dic.Add(signField[i], json[signField[i]].ToString());
            }
            string sign = json["sign"].ToString(); //客户端签名字符串
            return new Sign().sign(dic, sign);
        }

        #endregion

    }
}
