PubController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Http;
  5. using System.DrawingCore.Imaging;
  6. using System.IO;
  7. using Library;
  8. using System.Collections;
  9. using LitJson;
  10. using System.Globalization;
  11. using System.Web;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using Aliyun.OSS;
  15. using System.Net;
  16. namespace MySystem.Areas.Api.Controllers
  17. {
  18. [Area("Api")]
  19. [Route("Api/[controller]/[action]")]
  20. public class PubController : BaseController
  21. {
  22. public PubController(IHttpContextAccessor accessor) : base(accessor)
  23. {
  24. }
  25. #region OCR识别-营业执照
  26. public JsonResult BusinessLicenseOcr(string value)
  27. {
  28. value = DesDecrypt(value);
  29. JsonData data = JsonMapper.ToObject(value);
  30. string Url = data["Url"].ToString();
  31. if(string.IsNullOrEmpty(Url))
  32. {
  33. return Json(new AppResultJson() { Status = "-1", Info = "营业执照识别异常,请手动填写" });
  34. }
  35. string content = AliyunOcr.Check(Url);
  36. if(string.IsNullOrEmpty(content))
  37. {
  38. return Json(new AppResultJson() { Status = "-1", Info = "营业执照识别异常,请手动填写" });
  39. }
  40. Dictionary<string, object> Obj = new Dictionary<string, object>();
  41. Obj.Add("OcrData", content);
  42. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  43. }
  44. #endregion
  45. #region 活体认证
  46. public string GetToken()
  47. {
  48. if(!string.IsNullOrEmpty(RedisDbconn.Instance.Get<string>("Lkb_Access_Token")))
  49. {
  50. return RedisDbconn.Instance.Get<string>("Lkb_Access_Token");
  51. }
  52. string basic = "kxs_app:MxYh7A9Gkngp5YxWwKkuKlBGUaAIvpTn";
  53. basic = Convert.ToBase64String(Encoding.UTF8.GetBytes(basic));
  54. Dictionary<string, string> header = new Dictionary<string, string>();
  55. header.Add("Authorization", "Basic " + basic);
  56. string url = ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/oauth2/token?scope=server&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer";
  57. Dictionary<string, string> req = new Dictionary<string, string>();
  58. req.Add("username", "13281078937");
  59. req.Add("password", "ksx123987.");
  60. string reqJson = Newtonsoft.Json.JsonConvert.SerializeObject(req);
  61. string jsonData = AesEncrypt(reqJson);
  62. jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
  63. string result = PostWebRequest(url, jsonData, header);
  64. JsonData data = JsonMapper.ToObject(result);
  65. if(data["status"].ToString() != "1")
  66. {
  67. return "";
  68. }
  69. string token = data["data"]["access_token"].ToString();
  70. RedisDbconn.Instance.Set("Lkb_Access_Token", token);
  71. RedisDbconn.Instance.SetExpire("Lkb_Access_Token", 3600);
  72. return token;
  73. }
  74. public string initFaceVerify(string value)
  75. {
  76. value = DesDecrypt(value);
  77. JsonData data = JsonMapper.ToObject(value);
  78. string targetId = data["targetId"].ToString();
  79. string realName = data["realName"].ToString();
  80. string idCard = data["idCard"].ToString();
  81. string metaInfo = data["metaInfo"].ToString().Replace("\"", "\\\"");
  82. string accessToken = GetToken(); //data["accessToken"].ToString();
  83. string jsonString = "{\"targetId\":\"" + targetId + "\",\"realName\":\"" + realName + "\",\"idCard\":\"" + idCard + "\",\"metaInfo\":\"" + metaInfo + "\"}";
  84. string jsonData = AesEncrypt(jsonString);
  85. jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
  86. Dictionary<string, string> header = new Dictionary<string, string>();
  87. header.Add("Authorization", "Bearer " + accessToken);
  88. string result = PostWebRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/faceVerify/initFaceVerify", jsonData, header);
  89. return result;
  90. }
  91. public string queryFaceVerify(string value)
  92. {
  93. string status = RedisDbconn.Instance.Get<string>("LkbFaceVerifyStatus");
  94. if(status == "1")
  95. {
  96. return "{\"status\":\"1\",\"msg\":\"\",\"data\":\"\"}";
  97. }
  98. value = DesDecrypt(value);
  99. JsonData data = JsonMapper.ToObject(value);
  100. string targetId = data["targetId"].ToString();
  101. string accessToken = GetToken(); //data["accessToken"].ToString();
  102. string jsonString = "{\"targetId\":\"" + targetId + "\"}";
  103. string jsonData = AesEncrypt(jsonString);
  104. jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
  105. Dictionary<string, string> header = new Dictionary<string, string>();
  106. header.Add("Authorization", "Bearer " + accessToken);
  107. string result = HttpRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/faceVerify/query?value=" + jsonData, header);
  108. return result;
  109. }
  110. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  111. {
  112. string ret = string.Empty;
  113. try
  114. {
  115. function.WriteLog(DateTime.Now.ToString(), "请求开店宝API日志");
  116. function.WriteLog(postUrl, "请求开店宝API日志");
  117. function.WriteLog(paramData, "请求开店宝API日志");
  118. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  119. // 设置提交的相关参数
  120. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  121. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  122. request.Method = "POST";
  123. request.KeepAlive = false;
  124. request.AllowAutoRedirect = true;
  125. request.ContentType = "application/json";
  126. foreach (string key in headers.Keys)
  127. {
  128. request.Headers.Add(key, headers[key]);
  129. }
  130. 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)";
  131. request.ContentLength = postData.Length;
  132. // 提交请求数据
  133. Stream outputStream = request.GetRequestStream();
  134. outputStream.Write(postData, 0, postData.Length);
  135. outputStream.Close();
  136. HttpWebResponse response;
  137. Stream responseStream;
  138. StreamReader reader;
  139. string srcString;
  140. response = request.GetResponse() as HttpWebResponse;
  141. responseStream = response.GetResponseStream();
  142. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  143. srcString = reader.ReadToEnd();
  144. ret = srcString; //返回值赋值
  145. reader.Close();
  146. function.WriteLog(srcString, "请求开店宝API日志");
  147. }
  148. catch (WebException ex)
  149. {
  150. HttpWebResponse response = (HttpWebResponse)ex.Response;
  151. Stream myResponseStream = response.GetResponseStream();
  152. //获取响应内容
  153. StreamReader myStreamReader = new StreamReader(myResponseStream);
  154. ret = myStreamReader.ReadToEnd();
  155. myResponseStream.Close();
  156. }
  157. catch (Exception ex)
  158. {
  159. ret = "fail";
  160. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求开店宝API异常");
  161. }
  162. return ret;
  163. }
  164. public string HttpRequest(string url, Dictionary<string, string> header, string Method = "GET")
  165. {
  166. string result = "";
  167. try
  168. {
  169. HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
  170. webReq.Method = Method;
  171. webReq.KeepAlive = true;
  172. webReq.Timeout = 1200000;
  173. webReq.ContentType = "text/html";//application/x-www-form-urlencoded
  174. if (header.Count > 0)
  175. {
  176. foreach (string key in header.Keys)
  177. {
  178. webReq.Headers.Add(key, header[key]);
  179. }
  180. }
  181. webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
  182. using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
  183. {
  184. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  185. {
  186. result = reader.ReadToEnd();
  187. //function.WriteLog(context.Request.QueryString["mobile"] + " " + reader.ReadToEnd());
  188. }
  189. if (response != null)
  190. response.Close();
  191. }
  192. }
  193. catch (WebException ex)
  194. {
  195. HttpWebResponse response = (HttpWebResponse)ex.Response;
  196. Stream myResponseStream = response.GetResponseStream();
  197. //获取响应内容
  198. StreamReader myStreamReader = new StreamReader(myResponseStream);
  199. result = myStreamReader.ReadToEnd();
  200. myResponseStream.Close();
  201. }
  202. catch (Exception ex)
  203. {
  204. result = ex.ToString();
  205. }
  206. return result;
  207. }
  208. public string AesEncrypt(string str)
  209. {
  210. if (string.IsNullOrEmpty(str)) return null;
  211. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  212. // string key = "ZwwQCjEnGwaKzKWeuk4XIaHR0zbF8kSk";
  213. // string iv = "Jg4FxlLbfn0viypF";
  214. string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
  215. string iv = "DYgjCEIMVrj2W9xN";
  216. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  217. {
  218. Key = Encoding.UTF8.GetBytes(key),
  219. IV = Encoding.UTF8.GetBytes(iv),
  220. Mode = System.Security.Cryptography.CipherMode.CBC,
  221. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  222. };
  223. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  224. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  225. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  226. }
  227. #endregion
  228. }
  229. }