| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using System;
- using System.Collections.Generic;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using System.DrawingCore.Imaging;
- using System.IO;
- using Library;
- using System.Collections;
- using LitJson;
- using System.Globalization;
- using System.Web;
- using System.Security.Cryptography;
- using System.Text;
- using Aliyun.OSS;
- using System.Net;
- namespace MySystem.Areas.Api.Controllers
- {
- [Area("Api")]
- [Route("Api/[controller]/[action]")]
- public class PubController : BaseController
- {
- public PubController(IHttpContextAccessor accessor) : base(accessor)
- {
- }
-
- #region OCR识别-营业执照
- public JsonResult BusinessLicenseOcr(string value)
- {
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- string Url = data["Url"].ToString();
- if(string.IsNullOrEmpty(Url))
- {
- return Json(new AppResultJson() { Status = "-1", Info = "营业执照识别异常,请手动填写" });
- }
- string content = AliyunOcr.Check(Url);
- if(string.IsNullOrEmpty(content))
- {
- return Json(new AppResultJson() { Status = "-1", Info = "营业执照识别异常,请手动填写" });
- }
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- Obj.Add("OcrData", content);
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
- }
- #endregion
- #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", "13281078937");
- req.Add("password", "ksx123987.");
- 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)
- {
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- string targetId = data["targetId"].ToString();
- string realName = data["realName"].ToString();
- string idCard = data["idCard"].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);
- jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
- Dictionary<string, string> header = new Dictionary<string, string>();
- header.Add("Authorization", "Bearer " + accessToken);
- string result = PostWebRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/faceVerify/initFaceVerify", jsonData, header);
- return result;
- }
- public string queryFaceVerify(string value)
- {
- string status = RedisDbconn.Instance.Get<string>("LkbFaceVerifyStatus");
- if(status == "1")
- {
- return "{\"status\":\"1\",\"msg\":\"\",\"data\":\"\"}";
- }
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- string targetId = data["targetId"].ToString();
- string accessToken = GetToken(); //data["accessToken"].ToString();
- string jsonString = "{\"targetId\":\"" + targetId + "\"}";
- string jsonData = AesEncrypt(jsonString);
- jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
- Dictionary<string, string> header = new Dictionary<string, string>();
- header.Add("Authorization", "Bearer " + accessToken);
- 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 = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
- string iv = "DYgjCEIMVrj2W9xN";
- System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
- {
- Key = Encoding.UTF8.GetBytes(key),
- IV = Encoding.UTF8.GetBytes(iv),
- Mode = System.Security.Cryptography.CipherMode.CBC,
- Padding = System.Security.Cryptography.PaddingMode.PKCS7
- };
- System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
- Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- return Convert.ToBase64String(resultArray, 0, resultArray.Length);
- }
- #endregion
- }
- }
|