using System;
using System.Collections.Generic;
using System.Text;

namespace Admin.Core.Common
{
    public static class AddressHelper
    {
        // IP地址查询
        public const string IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";

        // 未知地址
        public const string UNKNOWN = "XX XX";

        public static string GetRealAddressByIP(string ip)
        {
            string address = UNKNOWN;
            // 内网不查询
            if (IpHelper.InternalIp(ip))
            {
                return "内网IP";
            }
            try
            {
                string rspStr = HttpHelper.Get(IP_URL + "?ip=" + ip + "&json=true");
                if (rspStr.IsNotEmptyOrNull())
                {
                    //log.error("获取地理位置异常 {}", ip);
                    return UNKNOWN;
                }
                var obj = JsonHelper.ParseFormByJson<object>(rspStr);
                //string region = obj.ObjToDecimal["pro"];
                //string city = obj.getString("city");
                //return string.Format("{0} {1}", region, city);
                return null;
            }
            catch (Exception)
            {
                //log.error("获取地理位置异常 {}", ip);
            }

            return address;
        }
    }

}