|
@@ -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);
|
|
|
|
+ }
|
|
|
|
+}
|