WebExtension.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using Masuit.Tools.Core.Config;
  2. using Masuit.Tools.Logging;
  3. using Masuit.Tools.Models;
  4. using Masuit.Tools.NoSQL;
  5. using Microsoft.AspNetCore.Http;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net.Http;
  13. using System.Net.Http.Headers;
  14. using System.Threading.Tasks;
  15. namespace Masuit.Tools.Core.Net
  16. {
  17. /// <summary>
  18. /// Web操作扩展
  19. /// </summary>
  20. public static class WebExtension
  21. {
  22. #region 获取线程内唯一的EF上下文对象
  23. /// <summary>
  24. /// 获取线程内唯一的EF上下文对象,.NetCore中获取EF数据上下文对象,需要通过依赖注入的方式,请考虑在自己的项目中通过Masuit.Tools提供的CallContext对象实现获取线程内唯一的EF上下文对象,示例代码:
  25. /// <para>public static DataContext GetDbContext()</para>
  26. /// <para>{</para>
  27. /// <para> DataContext db;</para>
  28. /// <para> if (CallContext&lt;DataContext>.GetData("db") is null)</para>
  29. /// <para> {</para>
  30. /// <para> DbContextOptions&lt;DataContext> dbContextOption = new DbContextOptions&lt;DataContext>();</para>
  31. /// <para> DbContextOptionsBuilder&lt;DataContext> dbContextOptionBuilder = new DbContextOptionsBuilder&lt;DataContext>(dbContextOption);</para>
  32. /// <para> db = new DataContext(dbContextOptionBuilder.UseSqlServer(CommonHelper.ConnectionString).Options);</para>
  33. /// <para> CallContext&lt;DataContext>.SetData("db", db);</para>
  34. /// <para> }</para>
  35. /// <para> db = CallContext&lt;DataContext>.GetData("db");</para>
  36. /// <para> return db;</para>
  37. /// <para>}</para>
  38. /// </summary>
  39. /// <typeparam name="T">EF上下文容器对象</typeparam>
  40. /// <returns>EF上下文容器对象</returns>
  41. [Obsolete(@".NetCore中获取EF数据上下文对象,需要通过依赖注入的方式,请考虑在自己的项目中通过Masuit.Tools提供的CallContext对象实现获取线程内唯一的EF上下文对象,示例代码:
  42. public static DataContext GetDbContext()
  43. {
  44. DataContext db;
  45. if (CallContext<DataContext>.GetData(""db"") is null)
  46. {
  47. DbContextOptions<DataContext> dbContextOption = new DbContextOptions<DataContext>();
  48. DbContextOptionsBuilder<DataContext> dbContextOptionBuilder = new DbContextOptionsBuilder<DataContext>(dbContextOption);
  49. db = new DataContext(dbContextOptionBuilder.UseSqlServer(CommonHelper.ConnectionString).Options);
  50. CallContext<DataContext>.SetData(""db"", db);
  51. }
  52. db = CallContext<DataContext>.GetData(""db"");
  53. return db;
  54. }")]
  55. public static T GetDbContext<T>() where T : new()
  56. {
  57. throw new Exception(@".NetCore中获取EF数据上下文对象,需要通过依赖注入的方式,请考虑在自己的项目中通过Masuit.Tools提供的CallContext对象实现获取线程内唯一的EF上下文对象,示例代码:
  58. public static DataContext GetDbContext()
  59. {
  60. DataContext db;
  61. if (CallContext<DataContext>.GetData(""db"") is null)
  62. {
  63. DbContextOptions<DataContext> dbContextOption = new DbContextOptions<DataContext>();
  64. DbContextOptionsBuilder<DataContext> dbContextOptionBuilder = new DbContextOptionsBuilder<DataContext>(dbContextOption);
  65. db = new DataContext(dbContextOptionBuilder.UseSqlServer(CommonHelper.ConnectionString).Options);
  66. CallContext<DataContext>.SetData(""db"", db);
  67. }
  68. db = CallContext<DataContext>.GetData(""db"");
  69. return db;
  70. }
  71. ");
  72. }
  73. #endregion
  74. #region 获取客户端IP地址信息
  75. /// <summary>
  76. /// 根据IP地址获取详细地理信息
  77. /// </summary>
  78. /// <param name="ip"></param>
  79. /// <returns></returns>
  80. public static async Task<Tuple<string, List<string>>> GetIPAddressInfo(this string ip)
  81. {
  82. ip.MatchInetAddress(out var isIpAddress);
  83. if (isIpAddress)
  84. {
  85. var address = await GetPhysicsAddressInfo(ip);
  86. if (address.Status == 0)
  87. {
  88. string detail = $"{address.AddressResult.FormattedAddress} {address.AddressResult.AddressComponent.Direction}{address.AddressResult.AddressComponent.Distance ?? "0"}米";
  89. List<string> pois = address.AddressResult.Pois.Select(p => $"{p.AddressDetail}{p.Name} {p.Direction}{p.Distance ?? "0"}米").ToList();
  90. return new Tuple<string, List<string>>(detail, pois);
  91. }
  92. return new Tuple<string, List<string>>("IP地址不正确", new List<string>());
  93. }
  94. return new Tuple<string, List<string>>($"{ip}不是一个合法的IP地址", new List<string>());
  95. }
  96. /// <summary>
  97. /// 根据IP地址获取详细地理信息对象
  98. /// </summary>
  99. /// <param name="ip"></param>
  100. /// <returns></returns>
  101. public static async Task<PhysicsAddress> GetPhysicsAddressInfo(this string ip)
  102. {
  103. ip.MatchInetAddress(out var isIpAddress);
  104. if (isIpAddress)
  105. {
  106. string ak = CoreConfig.Configuration["AppSettings:BaiduAK"];
  107. if (string.IsNullOrEmpty(ak))
  108. {
  109. throw new Exception("未配置BaiduAK,请先在您的应用程序web.config或者App.config中的AppSettings节点下添加BaiduAK配置节(注意大小写)");
  110. }
  111. using (HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") })
  112. {
  113. client.DefaultRequestHeaders.Referrer = new Uri("http://lbsyun.baidu.com/jsdemo.htm");
  114. var task = client.GetAsync($"/location/ip?ak={ak}&ip={ip}&coor=bd09ll").ContinueWith(async t =>
  115. {
  116. if (t.IsFaulted || t.IsCanceled)
  117. {
  118. return null;
  119. }
  120. var res = await t;
  121. if (res.IsSuccessStatusCode)
  122. {
  123. var ipAddress = JsonConvert.DeserializeObject<BaiduIP>(await res.Content.ReadAsStringAsync());
  124. if (ipAddress.Status == 0)
  125. {
  126. LatiLongitude point = ipAddress.AddressInfo.LatiLongitude;
  127. string result = client.GetStringAsync($"/geocoder/v2/?location={point.Y},{point.X}&output=json&pois=1&radius=1000&latest_admin=1&coordtype=bd09ll&ak={ak}").Result;
  128. PhysicsAddress address = JsonConvert.DeserializeObject<PhysicsAddress>(result);
  129. if (address.Status == 0)
  130. {
  131. return address;
  132. }
  133. }
  134. else
  135. {
  136. using (var client2 = new HttpClient { BaseAddress = new Uri("http://ip.taobao.com") })
  137. {
  138. return await await client2.GetAsync($"/service/getIpInfo.php?ip={ip}").ContinueWith(async tt =>
  139. {
  140. if (tt.IsFaulted || tt.IsCanceled)
  141. {
  142. return null;
  143. }
  144. var result = await tt;
  145. if (result.IsSuccessStatusCode)
  146. {
  147. TaobaoIP taobaoIp = JsonConvert.DeserializeObject<TaobaoIP>(await result.Content.ReadAsStringAsync());
  148. if (taobaoIp.Code == 0)
  149. {
  150. return new PhysicsAddress()
  151. {
  152. Status = 0,
  153. AddressResult = new AddressResult()
  154. {
  155. FormattedAddress = taobaoIp.IpData.Country + taobaoIp.IpData.Region + taobaoIp.IpData.City,
  156. AddressComponent = new AddressComponent()
  157. {
  158. Province = taobaoIp.IpData.Region
  159. },
  160. Pois = new List<Pois>()
  161. }
  162. };
  163. }
  164. }
  165. return null;
  166. });
  167. }
  168. }
  169. }
  170. return null;
  171. });
  172. return await await task;
  173. }
  174. }
  175. return null;
  176. }
  177. /// <summary>
  178. /// 根据IP地址获取ISP
  179. /// </summary>
  180. /// <param name="ip"></param>
  181. /// <returns></returns>
  182. public static string GetISP(this string ip)
  183. {
  184. if (ip.MatchInetAddress())
  185. {
  186. using (var client = new HttpClient { BaseAddress = new Uri("http://ip.taobao.com") })
  187. {
  188. var task = client.GetAsync($"/service/getIpInfo.php?ip={ip}").ContinueWith(async t =>
  189. {
  190. if (t.IsFaulted)
  191. {
  192. return $"未能找到{ip}的ISP信息";
  193. }
  194. var result = await t;
  195. if (result.IsSuccessStatusCode)
  196. {
  197. TaobaoIP taobaoIp = JsonConvert.DeserializeObject<TaobaoIP>(await result.Content.ReadAsStringAsync());
  198. if (taobaoIp.Code == 0)
  199. {
  200. return taobaoIp.IpData.Isp;
  201. }
  202. }
  203. return $"未能找到{ip}的ISP信息";
  204. });
  205. return task.Result.Result;
  206. }
  207. }
  208. return $"{ip}不是一个合法的IP";
  209. }
  210. #endregion
  211. /// <summary>
  212. /// 上传图片到百度图床
  213. /// </summary>
  214. /// <param name="stream"></param>
  215. /// <returns></returns>
  216. public static async Task<string> UploadImageAsync(Stream stream)
  217. {
  218. using (HttpClient httpClient = new HttpClient()
  219. {
  220. BaseAddress = new Uri("https://sp1.baidu.com"),
  221. })
  222. {
  223. httpClient.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("Mozilla/5.0"));
  224. using (var bc = new StreamContent(stream))
  225. {
  226. bc.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
  227. {
  228. FileName = "1.jpg",
  229. Name = "image"
  230. };
  231. bc.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
  232. using (var content = new MultipartFormDataContent { bc })
  233. {
  234. return await await httpClient.PostAsync("/70cHazva2gU2pMbgoY3K/n/image?needJson=true", content).ContinueWith(async t =>
  235. {
  236. if (t.IsCanceled || t.IsFaulted)
  237. {
  238. Console.WriteLine("发送请求出错了" + t.Exception);
  239. return string.Empty;
  240. }
  241. var res = await t;
  242. if (res.IsSuccessStatusCode)
  243. {
  244. string s = await res.Content.ReadAsStringAsync();
  245. var token = JObject.Parse(s);
  246. if ((int)token["errno"] == 0)
  247. {
  248. s = (string)token["data"]["imageUrl"];
  249. return s;
  250. }
  251. s = (string)token["errmsg"];
  252. return s;
  253. }
  254. return string.Empty;
  255. });
  256. }
  257. }
  258. }
  259. }
  260. #region 写Session
  261. //private static readonly RedisHelper Helper = RedisHelper.GetInstance(1);
  262. /// <summary>
  263. /// 写Session
  264. /// </summary>
  265. /// <param name="session"></param>
  266. /// <param name="key">键</param>
  267. /// <param name="value">值</param>
  268. public static void Set(this ISession session, string key, object value)
  269. {
  270. session.SetString(key, value.ToJsonString());
  271. }
  272. /// <summary>
  273. /// 将Session存到Redis,需要先在config中配置链接字符串,连接字符串在config配置文件中的ConnectionStrings节下配置,name固定为RedisHosts,值的格式:127.0.0.1:6379,allowadmin=true,若未正确配置,将按默认值“127.0.0.1:6379,allowadmin=true”进行操作,如:<br/>
  274. /// &lt;connectionStrings&gt;<br/>
  275. /// &lt;add name = "RedisHosts" connectionString="127.0.0.1:6379,allowadmin=true"/&gt;<br/>
  276. /// &lt;/connectionStrings&gt;
  277. /// </summary>
  278. /// <typeparam name="T"></typeparam>
  279. /// <param name="session"></param>
  280. /// <param name="key">键</param>
  281. /// <param name="obj">需要存的对象</param>
  282. /// <param name="expire">过期时间,默认20分钟</param>
  283. /// <returns></returns>
  284. public static void SetByRedis<T>(this ISession session, string key, T obj, int expire = 20)
  285. {
  286. if (HttpContext2.Current is null)
  287. {
  288. throw new Exception("请确保此方法调用是在同步线程中执行!");
  289. }
  290. //var sessionKey = HttpContext2.Current.Request.Cookies["SessionID"];
  291. //if (string.IsNullOrEmpty(sessionKey))
  292. //{
  293. // sessionKey = Guid.NewGuid().ToString().AESEncrypt();
  294. // HttpContext2.Current.Response.Cookies.Append("SessionID", sessionKey);
  295. //}
  296. if (session != null)
  297. {
  298. session.SetString(key, obj.ToJsonString());
  299. }
  300. try
  301. {
  302. using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
  303. {
  304. redisHelper.SetHash("Session:" + HttpContext2.Current.Connection.Id, key, obj, TimeSpan.FromMinutes(expire)); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
  305. }
  306. }
  307. catch
  308. {
  309. // ignored
  310. }
  311. }
  312. #endregion
  313. #region 获取Session
  314. /// <summary>
  315. /// 获取Session
  316. /// </summary>
  317. /// <typeparam name="T">对象</typeparam>
  318. /// <param name="session"></param>
  319. /// <param name="key">键</param>
  320. /// <returns>对象</returns>
  321. public static T Get<T>(this ISession session, string key)
  322. {
  323. string value = session.GetString(key);
  324. if (string.IsNullOrEmpty(value))
  325. {
  326. return default(T);
  327. }
  328. return JsonConvert.DeserializeObject<T>(value);
  329. }
  330. /// <summary>
  331. /// 从Redis取Session
  332. /// </summary>
  333. /// <typeparam name="T"></typeparam>
  334. /// <param name="_"></param>
  335. /// <param name="key">键</param>
  336. /// <param name="expire">过期时间,默认20分钟</param>
  337. /// <returns></returns>
  338. public static T GetByRedis<T>(this ISession _, string key, int expire = 20) where T : class
  339. {
  340. if (HttpContext2.Current is null)
  341. {
  342. throw new Exception("请确保此方法调用是在同步线程中执行!");
  343. }
  344. T obj = default(T);
  345. if (_ != null)
  346. {
  347. obj = _.Get<T>(key);
  348. }
  349. if (obj == default(T))
  350. {
  351. try
  352. {
  353. var sessionKey = "Session:" + HttpContext2.Current.Connection.Id;
  354. using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
  355. {
  356. if (redisHelper.KeyExists(sessionKey) && redisHelper.HashExists(sessionKey, key))
  357. {
  358. redisHelper.Expire(sessionKey, TimeSpan.FromMinutes(expire));
  359. return redisHelper.GetHash<T>(sessionKey, key);
  360. }
  361. return default(T);
  362. }
  363. }
  364. catch
  365. {
  366. return default(T);
  367. }
  368. }
  369. return obj;
  370. }
  371. /// <summary>
  372. /// 从Redis移除对应键的Session
  373. /// </summary>
  374. /// <param name="session"></param>
  375. /// <param name="key"></param>
  376. /// <returns></returns>
  377. public static void RemoveByRedis(this ISession session, string key)
  378. {
  379. if (HttpContext2.Current is null)
  380. {
  381. throw new Exception("请确保此方法调用是在同步线程中执行!");
  382. }
  383. if (session != null)
  384. {
  385. session.Remove(key);
  386. }
  387. try
  388. {
  389. var sessionKey = "Session:" + HttpContext2.Current.Connection.Id;
  390. using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
  391. {
  392. if (redisHelper.KeyExists(sessionKey) && redisHelper.HashExists(sessionKey, key))
  393. {
  394. redisHelper.DeleteHash(sessionKey, key);
  395. }
  396. }
  397. }
  398. catch (Exception e)
  399. {
  400. LogManager.Error(e);
  401. }
  402. }
  403. /// <summary>
  404. /// Session个数
  405. /// </summary>
  406. /// <param name="session"></param>
  407. /// <returns></returns>
  408. public static int SessionCount(this ISession session)
  409. {
  410. try
  411. {
  412. using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
  413. {
  414. return redisHelper.GetServer().Keys(1, "Session:*").Count();
  415. }
  416. }
  417. catch (Exception e)
  418. {
  419. LogManager.Error(e);
  420. return 0;
  421. }
  422. }
  423. #endregion
  424. }
  425. }