Bladeren bron

改造活体接口

lcl 7 maanden geleden
bovenliggende
commit
866fd85178
1 gewijzigde bestanden met toevoegingen van 140 en 9 verwijderingen
  1. 140 9
      Areas/Api/Controllers/PubController.cs

+ 140 - 9
Areas/Api/Controllers/PubController.cs

@@ -12,6 +12,7 @@ using System.Web;
 using System.Security.Cryptography;
 using System.Text;
 using Aliyun.OSS;
+using System.Net;
 
 namespace MySystem.Areas.Api.Controllers
 {
@@ -46,16 +47,44 @@ namespace MySystem.Areas.Api.Controllers
 
 
         #region 活体认证
+        public string GetToken()
+        {
+            if(!string.IsNullOrEmpty(RedisDbconn.Instance.Get<string>("Lkb_Access_Token")))
+            {
+                return RedisDbconn.Instance.Get<string>("Lkb_Access_Token");
+            }
+            string basic = "kxs_app:MxYh7A9Gkngp5YxWwKkuKlBGUaAIvpTn";
+            basic = Convert.ToBase64String(Encoding.UTF8.GetBytes(basic));
+            Dictionary<string, string> header = new Dictionary<string, string>();
+            header.Add("Authorization", "Basic " + basic);
+            string url = ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/oauth2/token?scope=server&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer";
+            Dictionary<string, string> req = new Dictionary<string, string>();
+            req.Add("username", "14781419364");
+            req.Add("password", "123456");
+            string reqJson = Newtonsoft.Json.JsonConvert.SerializeObject(req);
+            string jsonData = AesEncrypt(reqJson);
+            jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
+            string result = PostWebRequest(url, jsonData, header);
+            JsonData data = JsonMapper.ToObject(result);
+            if(data["status"].ToString() != "1")
+            {
+                return "";
+            }
+            string token = data["data"]["access_token"].ToString();
+            RedisDbconn.Instance.Set("Lkb_Access_Token", token);
+            RedisDbconn.Instance.SetExpire("Lkb_Access_Token", 3600);
+            return token;
+        }
         public string initFaceVerify(string value)
         {
-            function.WriteLog(value, "活体认证提交");
             value = DesDecrypt(value);
+            function.WriteLog(value, "活体认证提交");
             JsonData data = JsonMapper.ToObject(value);
             int targetId = int.Parse(data["targetId"].ToString());
             string realName = data["realName"].ToString();
             string idCard = data["idCard"].ToString();
-            string metaInfo = data["metaInfo"].ToString();
-            string accessToken = data["accessToken"].ToString();
+            string metaInfo = data["metaInfo"].ToString().Replace("\"", "\\\"");
+            string accessToken = GetToken(); //data["accessToken"].ToString();
             string jsonString = "{\"targetId\":" + targetId + ",\"realName\":\"" + realName + "\",\"idCard\":\"" + idCard + "\",\"metaInfo\":\"" + metaInfo + "\"}";
 
             string jsonData = AesEncrypt(jsonString);
@@ -63,16 +92,16 @@ namespace MySystem.Areas.Api.Controllers
 
             Dictionary<string, string> header = new Dictionary<string, string>();
             header.Add("Authorization", "Bearer " + accessToken);
-            string result = function.PostWebRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/lkb/mchServer/faceVerify/initFaceVerify", jsonData, header, "application/json");
+            string result = PostWebRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/faceVerify/initFaceVerify", jsonData, header);
             return result;
         }
         public string queryFaceVerify(string value)
         {
-            function.WriteLog(value, "活体认证查询");
             value = DesDecrypt(value);
+            function.WriteLog(value, "活体认证查询");
             JsonData data = JsonMapper.ToObject(value);
             int targetId = int.Parse(data["targetId"].ToString());
-            string accessToken = data["accessToken"].ToString();
+            string accessToken = GetToken(); //data["accessToken"].ToString();
             string jsonString = "{\"targetId\":" + targetId + "}";
 
             string jsonData = AesEncrypt(jsonString);
@@ -80,15 +109,117 @@ namespace MySystem.Areas.Api.Controllers
 
             Dictionary<string, string> header = new Dictionary<string, string>();
             header.Add("Authorization", "Bearer " + accessToken);
-            string result = function.GetWebRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/lkb/mchServer/faceVerify/query?value=" + jsonData, header);
+            string result = HttpRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/faceVerify/query?value=" + jsonData, header);
+            return result;
+        }
+        public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
+        {
+            string ret = string.Empty;
+            try
+            {
+                function.WriteLog(DateTime.Now.ToString(), "请求开店宝API日志");
+                function.WriteLog(postUrl, "请求开店宝API日志");
+                function.WriteLog(paramData, "请求开店宝API日志");
+                byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
+                // 设置提交的相关参数 
+                HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
+                System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
+                request.Method = "POST";
+                request.KeepAlive = false;
+                request.AllowAutoRedirect = true;
+                request.ContentType = "application/json";
+                foreach (string key in headers.Keys)
+                {
+                    request.Headers.Add(key, headers[key]);
+                }
+                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
+
+                request.ContentLength = postData.Length;
+
+                // 提交请求数据 
+                Stream outputStream = request.GetRequestStream();
+                outputStream.Write(postData, 0, postData.Length);
+                outputStream.Close();
+                HttpWebResponse response;
+                Stream responseStream;
+                StreamReader reader;
+                string srcString;
+                response = request.GetResponse() as HttpWebResponse;
+                responseStream = response.GetResponseStream();
+                reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
+                srcString = reader.ReadToEnd();
+                ret = srcString;   //返回值赋值
+                reader.Close();
+                function.WriteLog(srcString, "请求开店宝API日志");
+            }
+            catch (WebException ex)
+            {
+                HttpWebResponse response = (HttpWebResponse)ex.Response;
+                Stream myResponseStream = response.GetResponseStream();
+                //获取响应内容
+                StreamReader myStreamReader = new StreamReader(myResponseStream);
+                ret = myStreamReader.ReadToEnd();
+                myResponseStream.Close();
+            }
+            catch (Exception ex)
+            {
+                ret = "fail";
+                function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求开店宝API异常");
+            }
+            return ret;
+        }
+        public string HttpRequest(string url, Dictionary<string, string> header, string Method = "GET")
+        {
+            string result = "";
+            try
+            {
+                HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
+                webReq.Method = Method;
+                webReq.KeepAlive = true;
+                webReq.Timeout = 1200000;
+                webReq.ContentType = "text/html";//application/x-www-form-urlencoded
+                if (header.Count > 0)
+                {
+                    foreach (string key in header.Keys)
+                    {
+                        webReq.Headers.Add(key, header[key]);
+                    }
+                }
+                webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
+                using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
+                {
+                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
+                    {
+                        result = reader.ReadToEnd();
+                        //function.WriteLog(context.Request.QueryString["mobile"] + "  " + reader.ReadToEnd());
+                    }
+                    if (response != null)
+                        response.Close();
+                }
+            }
+            catch (WebException ex)
+            {
+                HttpWebResponse response = (HttpWebResponse)ex.Response;
+                Stream myResponseStream = response.GetResponseStream();
+                //获取响应内容
+                StreamReader myStreamReader = new StreamReader(myResponseStream);
+                result = myStreamReader.ReadToEnd();
+                myResponseStream.Close();
+            }
+            catch (Exception ex)
+            {
+                result = ex.ToString();
+            }
             return result;
         }
         public string AesEncrypt(string str)
         {
             if (string.IsNullOrEmpty(str)) return null;
             Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
-            string key = "ZwwQCjEnGwaKzKWeuk4XIaHR0zbF8kSk";
-            string iv = "Jg4FxlLbfn0viypF";
+            // string key = "ZwwQCjEnGwaKzKWeuk4XIaHR0zbF8kSk";
+            // string iv = "Jg4FxlLbfn0viypF";
+            string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
+            string iv = "DYgjCEIMVrj2W9xN";
 
             System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
             {