Browse Source

省份区域查询

hexiao 1 năm trước cách đây
mục cha
commit
07e780bfa2

+ 34 - 0
api/src/main/java/com/ankaibei/workflow/api/AreaApi.java

@@ -0,0 +1,34 @@
+package com.ankaibei.workflow.api;
+
+
+import com.ankaibei.workflow.vo.AreaInfo;
+import com.ankaibei.workflow.vo.SysUser;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import java.util.List;
+
+@FeignClient(name = "areaApi", url = "${api-url:localhost:8077/api}")
+public interface AreaApi {
+
+    /**
+     * 根据条件查询行政区域信息
+     */
+    @GetMapping("/foreign/admin/area/getInfoLikeKeywordsAndSubdistrict")
+    List<AreaInfo> getInfoLikeKeywordsAndSubdistrict(@RequestParam("keywords")String keywords,@RequestParam("subdistrict")Integer subdistrict);
+
+    /**
+     * 查询所有的省份信息
+     */
+    @GetMapping("/foreign/admin/area/getProvinces")
+    List<AreaInfo> getProvinces();
+
+    /**
+     * 根据省份关键字查询城市信息
+     */
+    @GetMapping("/foreign/admin/area/getCity")
+    List<AreaInfo> getCity(@RequestParam("keywords")String keywords);
+
+
+}

+ 29 - 0
api/src/main/java/com/ankaibei/workflow/vo/AreaInfo.java

@@ -0,0 +1,29 @@
+package com.ankaibei.workflow.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+public class AreaInfo {
+
+    // 行政区域编码
+    private String adcode;
+
+    // 名称
+    private String name;
+
+    // 行政区域中心点(经纬度)
+    private String center;
+
+    // 行政区域级别 (country国家、province省份)
+    private String level;
+
+    private List<AreaInfo> districts;
+}

+ 31 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/api/AreaApiService.java

@@ -0,0 +1,31 @@
+package com.ankaibei.workFlow.webadmin.api;
+
+import com.ankaibei.workFlow.webadmin.utils.GaoDeWebUtil;
+import com.ankaibei.workflow.api.AreaApi;
+import com.ankaibei.workflow.vo.AreaInfo;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/foreign/admin/area")
+public class AreaApiService implements AreaApi {
+
+
+    @Override
+    public List<AreaInfo> getInfoLikeKeywordsAndSubdistrict(String keywords, Integer subdistrict) {
+        return GaoDeWebUtil.getInfoLikeKeywordsAndSubdistrict(keywords, subdistrict);
+    }
+
+    @Override
+    public List<AreaInfo> getProvinces() {
+        return GaoDeWebUtil.getProvinces();
+    }
+
+    @Override
+    public List<AreaInfo> getCity(String keywords) {
+        return GaoDeWebUtil.getCity(keywords);
+    }
+
+}

+ 5 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/config/ApplicationConfig.java

@@ -48,4 +48,9 @@ public class ApplicationConfig {
      * 缺省值是 one day
      */
     private int sessionExpiredSeconds = 86400;
+
+    /**
+     * 高德WEB服务key
+     */
+    private String gdKey;
 }

+ 109 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/utils/GaoDeWebUtil.java

@@ -0,0 +1,109 @@
+package com.ankaibei.workFlow.webadmin.utils;
+
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSON;
+import com.ankaibei.workFlow.webadmin.config.ApplicationConfig;
+import com.ankaibei.workflow.vo.AreaInfo;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
+
+import javax.annotation.PostConstruct;
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Map;
+
+@Component
+public class GaoDeWebUtil {
+
+    @Autowired
+    private ApplicationConfig applicationConfig;
+
+    public static String key = "1038f05a58704762e009c8e726226c26";
+
+    public final static String areaPath = "https://restapi.amap.com/v3/config/district?keywords=%s&subdistrict=%d&key=%s";
+
+    @PostConstruct
+    private void init() {
+        GaoDeWebUtil.key = applicationConfig.getGdKey();
+    }
+
+
+    /*
+     keywords     中华人民共和国   规则:只支持单个关键词语搜索关键词支持:行政区名称、citycode、adcode
+                                例如,在subdistrict=2,搜索省份(例如山东),能够显示市(例如济南),区(例如历下区)
+     subdistrict  1             规则:设置显示下级行政区级数(行政区级别包括:国家、省/直辖市、市、区/县4个级别)
+                                可选值:0、1、2、3
+                                0:不返回下级行政区;
+                                1:返回下一级行政区;
+                                2:返回下两级行政区;
+                                3:返回下三级行政区;
+    * */
+
+    /**
+     * 根据关键字模糊查询行政区域信息
+     *
+     * @param keywords
+     * @param subdistrict
+     * @return
+     */
+    public static List<AreaInfo> getInfoLikeKeywordsAndSubdistrict(String keywords, Integer subdistrict) {
+        String format = String.format(areaPath, keywords, subdistrict, key);
+        String result = HttpUtil.get(format);
+        if (!StringUtils.isBlank(result)) {
+            Map<String, Object> map = JSON.parseObject(result, Map.class);
+            if (!ObjectUtils.isEmpty(map)
+                    && !ObjectUtils.isEmpty(map.get("status"))
+                    && map.get("status").toString().equals("1")) {
+                Object districts = map.get("districts");
+                if (!ObjectUtils.isEmpty(districts)) {
+                    List<AreaInfo> areaInfos = JSON.parseArray(JSON.toJSONString(districts), AreaInfo.class);
+                    return areaInfos;
+                }
+            }
+        }
+        return null;
+    }
+
+
+    /**
+     * 获取全部省份
+     * @return
+     */
+    public static List<AreaInfo> getProvinces() {
+        List<AreaInfo> country = getInfoLikeKeywordsAndSubdistrict("中华人民共和国", 1);
+        if (!ObjectUtils.isEmpty(country)) {
+            return country.get(0).getDistricts();
+        }
+        return null;
+    }
+
+    /**
+     * 根据关键字获取下级城市
+     * @param keywords
+     * @return
+     */
+    public static List<AreaInfo> getCity(String keywords) {
+        List<AreaInfo> country = getInfoLikeKeywordsAndSubdistrict(keywords, 1);
+        if (!ObjectUtils.isEmpty(country)) {
+            if (country.size() > 1 ) {
+                for (AreaInfo areaInfo : country) {
+                    if(areaInfo.getLevel().equals("city")){
+                        return areaInfo.getDistricts();
+                    }
+                }
+            }
+          return country.get(0).getDistricts();
+        }
+        return null;
+    }
+
+    public static void main(String[] args) {
+        List<AreaInfo> list = getInfoLikeKeywordsAndSubdistrict("中华人民共和国", 3);
+//        List<AreaInfo> list =  getProvinces();
+//        List<AreaInfo> list = getCity("北京");
+        System.out.println(list);
+    }
+}

+ 2 - 0
application-webadmin/src/main/resources/application-product.yml

@@ -70,6 +70,8 @@ application:
   credentialIpList: "*"
   # Session的用户和数据权限在Redis中的过期时间(秒)。
   sessionExpiredSeconds: 86400
+  # 高德web 服务key
+  gdKey: 1038f05a58704762e009c8e726226c26
 
 sequence:
   # Snowflake 分布式Id生成算法所需的WorkNode参数值。