傅行帆 5 年前
父节点
当前提交
3cdd894be3

+ 18
- 0
src/main/java/com/huiju/estateagents/controller/TdCityController.java 查看文件

@@ -171,4 +171,22 @@ public class TdCityController extends BaseController {
171 171
         }
172 172
         return responseBean;
173 173
     }
174
+    
175
+    /**
176
+     * 根据经纬度查询城市ID
177
+     * 经度在前,纬度在后,经纬度间以“,”分割,经纬度小数点后不要超过 6 位
178
+     * @return
179
+     */
180
+    @RequestMapping(value="/wx/location/city",method= RequestMethod.GET)
181
+    public ResponseBean wxLocationCityDetail(@RequestParam(value = "location") String location,HttpServletRequest request){
182
+        ResponseBean responseBean = new ResponseBean();
183
+        try {
184
+            TdCity tdCity = iTdCityService.getLocationCity(location);
185
+            responseBean.addSuccess(tdCity);
186
+        }catch (Exception e){
187
+            logger.error("tdCityList -=- {}",e.toString());
188
+            responseBean.addError(e.getMessage());
189
+        }
190
+        return responseBean;
191
+    }
174 192
 }

+ 7
- 0
src/main/java/com/huiju/estateagents/service/ITdCityService.java 查看文件

@@ -19,4 +19,11 @@ public interface ITdCityService extends IService<TdCity> {
19 19
 
20 20
     // 微信端城市
21 21
     List<TdCity> selectWxCity(String leveltype,Integer orgId);
22
+	
23
+	/**
24
+	 * 根据经纬度获取城市详情信息
25
+	 * @param location
26
+	 * @return
27
+	 */
28
+	TdCity getLocationCity(String location);
22 29
 }

+ 30
- 1
src/main/java/com/huiju/estateagents/service/impl/TdCityServiceImpl.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package com.huiju.estateagents.service.impl;
2 2
 
3
+import com.alibaba.fastjson.JSONObject;
4
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
3 5
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4 6
 import com.huiju.estateagents.entity.TdCity;
5 7
 import com.huiju.estateagents.mapper.TaOrgCityMapper;
@@ -7,6 +9,7 @@ import com.huiju.estateagents.mapper.TdCityMapper;
7 9
 import com.huiju.estateagents.service.ITdCityService;
8 10
 import org.springframework.beans.factory.annotation.Autowired;
9 11
 import org.springframework.stereotype.Service;
12
+import org.springframework.web.client.RestTemplate;
10 13
 
11 14
 import java.util.List;
12 15
 
@@ -33,5 +36,31 @@ public class TdCityServiceImpl extends ServiceImpl<TdCityMapper, TdCity> impleme
33 36
         List<TdCity>  tdCityList= tdCityMapper.selectWxCity(orgId);
34 37
         return tdCityList;
35 38
     }
36
-
39
+    
40
+    /**
41
+     * 根据经纬度获取城市详情信息
42
+     *
43
+     * @param location
44
+     * @return
45
+     */
46
+    @Override
47
+    public TdCity getLocationCity(String location) {
48
+        //获取高德地图的坐标
49
+        String amapString = "https://restapi.amap.com/v3/geocode/regeo?location="+location+"&key=14f05ce59c26364fd0674014dc0d8b1b";
50
+        RestTemplate restTemplate = new RestTemplate();
51
+        String forObject = restTemplate.getForObject(amapString, String.class);
52
+        JSONObject jsonObject = JSONObject.parseObject(forObject);
53
+        //返回城市信息
54
+        if (jsonObject.getString("status").equals("1")){
55
+            JSONObject addressComponent = jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent");
56
+            String citycode = addressComponent.getString("citycode");
57
+            QueryWrapper<TdCity> queryWrapper = new QueryWrapper<>();
58
+            queryWrapper.eq("citycode",citycode);
59
+            queryWrapper.eq("leveltype",2);
60
+            TdCity tdCity = tdCityMapper.selectOne(queryWrapper);
61
+            return tdCity;
62
+        }
63
+        return null;
64
+    }
65
+    
37 66
 }

+ 30
- 0
src/test/java/com/huiju/estateagents/TdCityServiceImplTest.java 查看文件

@@ -0,0 +1,30 @@
1
+package com.huiju.estateagents;
2
+
3
+import com.huiju.estateagents.entity.TdCity;
4
+import com.huiju.estateagents.service.ITdCityService;
5
+import org.junit.Assert;
6
+import org.junit.Test;
7
+import org.junit.runner.RunWith;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.boot.test.context.SpringBootTest;
10
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
+import org.springframework.transaction.annotation.Transactional;
12
+
13
+
14
+@RunWith(SpringJUnit4ClassRunner.class)
15
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
16
+@Transactional
17
+public class TdCityServiceImplTest {
18
+	
19
+	@Autowired
20
+	private ITdCityService iTdCityService;
21
+	
22
+	@Test
23
+	public void getLocationCity() {
24
+		TdCity locationCity = iTdCityService.getLocationCity("116.310003,39.991957");
25
+		//断言 对象不为null
26
+		Assert.assertNotNull(locationCity);
27
+		//断言获取的是北京市
28
+		Assert.assertEquals("北京市",locationCity.getName());
29
+	}
30
+}