PubController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. function.WriteLog(value, "活体认证提交");
  78. JsonData data = JsonMapper.ToObject(value);
  79. int targetId = int.Parse(data["targetId"].ToString());
  80. string realName = data["realName"].ToString();
  81. string idCard = data["idCard"].ToString();
  82. string metaInfo = data["metaInfo"].ToString().Replace("\"", "\\\"");
  83. string accessToken = GetToken(); //data["accessToken"].ToString();
  84. string jsonString = "{\"targetId\":" + targetId + ",\"realName\":\"" + realName + "\",\"idCard\":\"" + idCard + "\",\"metaInfo\":\"" + metaInfo + "\"}";
  85. string jsonData = AesEncrypt(jsonString);
  86. jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
  87. Dictionary<string, string> header = new Dictionary<string, string>();
  88. header.Add("Authorization", "Bearer " + accessToken);
  89. string result = PostWebRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/faceVerify/initFaceVerify", jsonData, header);
  90. return result;
  91. }
  92. public string queryFaceVerify(string value)
  93. {
  94. value = DesDecrypt(value);
  95. function.WriteLog(value, "活体认证查询");
  96. JsonData data = JsonMapper.ToObject(value);
  97. int targetId = int.Parse(data["targetId"].ToString());
  98. string accessToken = GetToken(); //data["accessToken"].ToString();
  99. string jsonString = "{\"targetId\":" + targetId + "}";
  100. string jsonData = AesEncrypt(jsonString);
  101. jsonData = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonData));
  102. Dictionary<string, string> header = new Dictionary<string, string>();
  103. header.Add("Authorization", "Bearer " + accessToken);
  104. string result = HttpRequest(ConfigurationManager.AppSettings["LkbHost"] + "/v1/kxs/userServer/faceVerify/query?value=" + jsonData, header);
  105. return result;
  106. }
  107. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  108. {
  109. string ret = string.Empty;
  110. try
  111. {
  112. function.WriteLog(DateTime.Now.ToString(), "请求开店宝API日志");
  113. function.WriteLog(postUrl, "请求开店宝API日志");
  114. function.WriteLog(paramData, "请求开店宝API日志");
  115. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  116. // 设置提交的相关参数
  117. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  118. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  119. request.Method = "POST";
  120. request.KeepAlive = false;
  121. request.AllowAutoRedirect = true;
  122. request.ContentType = "application/json";
  123. foreach (string key in headers.Keys)
  124. {
  125. request.Headers.Add(key, headers[key]);
  126. }
  127. 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)";
  128. request.ContentLength = postData.Length;
  129. // 提交请求数据
  130. Stream outputStream = request.GetRequestStream();
  131. outputStream.Write(postData, 0, postData.Length);
  132. outputStream.Close();
  133. HttpWebResponse response;
  134. Stream responseStream;
  135. StreamReader reader;
  136. string srcString;
  137. response = request.GetResponse() as HttpWebResponse;
  138. responseStream = response.GetResponseStream();
  139. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  140. srcString = reader.ReadToEnd();
  141. ret = srcString; //返回值赋值
  142. reader.Close();
  143. function.WriteLog(srcString, "请求开店宝API日志");
  144. }
  145. catch (WebException ex)
  146. {
  147. HttpWebResponse response = (HttpWebResponse)ex.Response;
  148. Stream myResponseStream = response.GetResponseStream();
  149. //获取响应内容
  150. StreamReader myStreamReader = new StreamReader(myResponseStream);
  151. ret = myStreamReader.ReadToEnd();
  152. myResponseStream.Close();
  153. }
  154. catch (Exception ex)
  155. {
  156. ret = "fail";
  157. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求开店宝API异常");
  158. }
  159. return ret;
  160. }
  161. public string HttpRequest(string url, Dictionary<string, string> header, string Method = "GET")
  162. {
  163. string result = "";
  164. try
  165. {
  166. HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
  167. webReq.Method = Method;
  168. webReq.KeepAlive = true;
  169. webReq.Timeout = 1200000;
  170. webReq.ContentType = "text/html";//application/x-www-form-urlencoded
  171. if (header.Count > 0)
  172. {
  173. foreach (string key in header.Keys)
  174. {
  175. webReq.Headers.Add(key, header[key]);
  176. }
  177. }
  178. webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
  179. using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
  180. {
  181. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  182. {
  183. result = reader.ReadToEnd();
  184. //function.WriteLog(context.Request.QueryString["mobile"] + " " + reader.ReadToEnd());
  185. }
  186. if (response != null)
  187. response.Close();
  188. }
  189. }
  190. catch (WebException ex)
  191. {
  192. HttpWebResponse response = (HttpWebResponse)ex.Response;
  193. Stream myResponseStream = response.GetResponseStream();
  194. //获取响应内容
  195. StreamReader myStreamReader = new StreamReader(myResponseStream);
  196. result = myStreamReader.ReadToEnd();
  197. myResponseStream.Close();
  198. }
  199. catch (Exception ex)
  200. {
  201. result = ex.ToString();
  202. }
  203. return result;
  204. }
  205. public string AesEncrypt(string str)
  206. {
  207. if (string.IsNullOrEmpty(str)) return null;
  208. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  209. // string key = "ZwwQCjEnGwaKzKWeuk4XIaHR0zbF8kSk";
  210. // string iv = "Jg4FxlLbfn0viypF";
  211. string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
  212. string iv = "DYgjCEIMVrj2W9xN";
  213. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  214. {
  215. Key = Encoding.UTF8.GetBytes(key),
  216. IV = Encoding.UTF8.GetBytes(iv),
  217. Mode = System.Security.Cryptography.CipherMode.CBC,
  218. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  219. };
  220. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  221. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  222. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  223. }
  224. #endregion
  225. }
  226. }