فهرست منبع

获取地理信息改进

懒得勤快 5 سال پیش
والد
کامیت
1ce5936113
2فایلهای تغییر یافته به همراه38 افزوده شده و 12 حذف شده
  1. 37 11
      src/Masuit.MyBlogs.Core/Controllers/ToolsController.cs
  2. 1 1
      src/Masuit.MyBlogs.Core/Views/Tools/GetIPInfo.cshtml

+ 37 - 11
src/Masuit.MyBlogs.Core/Controllers/ToolsController.cs

@@ -1,11 +1,12 @@
-using System;
-using Masuit.MyBlogs.Core.Common;
+using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Configs;
 using Masuit.Tools.Core.Net;
 using Masuit.Tools.Models;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
 using System.Net.Http;
 using System.Threading.Tasks;
 
@@ -32,26 +33,44 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="ip"></param>
         /// <returns></returns>
-        [Route("ip/{ip?}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "ip" }, VaryByHeader = "Cookie")]
+        [Route("ip/{ip?}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[]
+        {
+            "ip"
+        }, VaryByHeader = "Cookie")]
         public async Task<ActionResult> GetIpInfo(string ip)
         {
             if (string.IsNullOrEmpty(ip))
             {
                 ip = ClientIP;
             }
+
             ViewBag.IP = ip;
-            PhysicsAddress address = await ip.GetPhysicsAddressInfo();
-            if (address != null && address.Status == 0)
+            var address = await ip.GetPhysicsAddressInfo() ?? new PhysicsAddress()
             {
-                address.AddressResult.Pois.Add(new Pois()
+                Status = 0,
+                AddressResult = new AddressResult()
                 {
-                    AddressDetail = ip.GetIPLocation() + "(本地数据库)"
-                });
-            }
+                    Pois = new List<Pois>()
+                    {
+                        new Pois()
+                        {
+                            AddressDetail = ip.GetIPLocation() + "(本地数据库)"
+                        }
+                    },
+                    AddressComponent = new AddressComponent(),
+                    FormattedAddress = ip.GetIPLocation(),
+                    Location = new Location()
+                    {
+                        Lng = CommonHelper.MaxmindReader.City(ip).Location.Longitude.Value,
+                        Lat = CommonHelper.MaxmindReader.City(ip).Location.Latitude.Value
+                    }
+                }
+            };
             if (Request.Method.Equals(HttpMethods.Get))
             {
                 return View(address);
             }
+
             return Json(address);
         }
 
@@ -61,7 +80,11 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="lat"></param>
         /// <param name="lng"></param>
         /// <returns></returns>
-        [HttpGet("pos"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "lat", "lng" }, VaryByHeader = "Cookie")]
+        [HttpGet("pos"), ResponseCache(Duration = 600, VaryByQueryKeys = new[]
+        {
+            "lat",
+            "lng"
+        }, VaryByHeader = "Cookie")]
         public async Task<ActionResult> Position(string lat, string lng)
         {
             if (string.IsNullOrEmpty(lat) || string.IsNullOrEmpty(lng))
@@ -85,7 +108,10 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="addr"></param>
         /// <returns></returns>
-        [Route("addr"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "addr" }, VaryByHeader = "Cookie")]
+        [Route("addr"), ResponseCache(Duration = 600, VaryByQueryKeys = new[]
+        {
+            "addr"
+        }, VaryByHeader = "Cookie")]
         public async Task<ActionResult> Address(string addr)
         {
             if (string.IsNullOrEmpty(addr))

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Tools/GetIPInfo.cshtml

@@ -60,7 +60,7 @@
     }
     else
     {
-        <span class="text-danger">@ViewBag.IP 不在国内或者不是一个有效的IPv4外网地址</span>
+        <span class="text-danger">@ViewBag.IP 未找到或者不是一个有效的IPv4外网地址</span>
     }
 </div>
 <div class="container-fluid">