package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import config.ManagementConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import utils.HttpDeal;
import utils.ResponseCode;
import utils.ResultData;

/**
 * Created by hushe on 2017/10/28.
 */
@RestController
@RequestMapping("/location")
public class LocationController {
    private Logger logger = LoggerFactory.getLogger(LocationController.class);

    @RequestMapping(method =  RequestMethod.GET, value = "/ip")
    public ResultData ip2city(String ip){
        ResultData result = new ResultData();

        String url = new StringBuffer
                ("http://apis.map.qq.com/ws/location/v1/ip").append("?ip=").append(ip)
                .append("&key=").append(ManagementConfig.getValue("tencent_map_key"))
                .toString();

        String response = HttpDeal.getResponse(url);
        JSONObject json = JSON.parseObject(response);
        try {
            JSONObject jo = new JSONObject();
            jo.put("nation", json.getJSONObject("result").getJSONObject("ad_info").getJSONObject("nation"));
            jo.put("province", json.getJSONObject("result").getJSONObject("ad_info").getJSONObject("province"));
            jo.put("city", json.getJSONObject("result").getJSONObject("ad_info").getJSONObject("city"));
            result.setData(jo);
        }catch (Exception e){
            logger.error(e.getMessage());
            result.setResponseCode(ResponseCode.RESPONSE_ERROR);
            result.setDescription(e.getMessage());
        }
        return result;
    }
}
@@ -14,16 +14,17 @@
 oller;%0A%0A
+%0A
 import c
@@ -178,16 +178,61 @@
 actory;%0A
+import org.springframework.util.StringUtils;%0A
 import o
@@ -1204,16 +1204,64 @@
   try %7B%0A
+            if(!StringUtils.isEmpty(json))%7B%0A    
         
@@ -1298,16 +1298,20 @@
 ject();%0A
+    
         
@@ -1415,32 +1415,36 @@
 ));%0A            
+   
+ 
 jo.put(%22province
@@ -1532,32 +1532,36 @@
 ));%0A            
+    
 jo.put(%22city%22, j
@@ -1641,32 +1641,36 @@
 ));%0A            
+  
+  
 result.setData(j
@@ -1673,16 +1673,170 @@
 ta(jo);%0A
+            %7Delse %7B%0A                result.setResponseCode(ResponseCode.RESPONSE_NULL);%0A                result.setDescription(%22%E6%9C%AA%E8%8E%B7%E5%8F%96%E5%88%B0%E5%9F%8E%E5%B8%82%E4%BF%A1%E6%81%AF%EF%BC%81%22);%0A            %7D%0A
         

修改ip2city接口