‘linchangsheng’ 2 周之前
父节点
当前提交
7373529ad1

+ 48 - 0
src/main/java/com/gree/mall/miniapp/logic/LbsAmapLogic.java

@@ -0,0 +1,48 @@
+package com.gree.mall.miniapp.logic;
+
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.Map;
+
+@Slf4j
+@RequiredArgsConstructor
+@Service
+public class LbsAmapLogic {
+
+//    private static final String lbsWebKey = "b772f8b0ace6bc96c04ae8e48f241e36";
+    private static final String lbsWebKey = "428a7111e02ea8367a3b34804eaa025b";
+
+
+    /**
+     * 根据地址反查经纬度
+     * @Param 中文地址(最好详细)
+     * @return lng,lat
+     */
+    public String getLocationByAddress(String address){
+        //String lbsWebKey = "b772f8b0ace6bc96c04ae8e48f241e36";
+        //String address = "广东省广州市天河区华景新城";
+        String url = "https://restapi.amap.com/v3/geocode/geo?key="+lbsWebKey+"&address="+address;
+        String s = HttpUtil.get(url);
+        JSONObject jsonObject = new JSONObject(s);
+        String status = jsonObject.getStr("status");
+        if(!status.equals("1")){
+            return null;
+        }
+        JSONArray geocodes = jsonObject.getJSONArray("geocodes");
+        if(CollectionUtils.isEmpty(geocodes))
+            return null;
+        Map<String, Object> map = (Map<String, Object>) geocodes.get(0);
+        String location = (String)map.get("location");
+        log.info("根据地址【{}】反查经纬度【{}】",address,new JSONObject(map));
+        return location;
+    }
+
+
+
+}

+ 13 - 0
src/main/java/com/gree/mall/miniapp/logic/user/AddressLogic.java

@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQu
 import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
 import com.gree.mall.miniapp.bean.user.UserAddressBean;
 import com.gree.mall.miniapp.exception.RemoteServiceException;
+import com.gree.mall.miniapp.logic.LbsAmapLogic;
 import com.gree.mall.miniapp.logic.common.CommonLogic;
 import com.gree.mall.miniapp.plus.entity.Coupon;
 import com.gree.mall.miniapp.plus.entity.FreightTemplateDetail;
@@ -37,6 +38,9 @@ public class AddressLogic {
     @Autowired
     private CommonLogic commonLogic;
 
+    @Autowired
+    private LbsAmapLogic lbsAmapLogic;
+
     public UserAddress saveAddress(HttpServletRequest request, UserAddressBean userAddressBean) {
         CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
 
@@ -55,6 +59,11 @@ public class AddressLogic {
                     .set(UserAddress::getDefaultAddr, false)
                     .update();
         }
+        String locationByAddress = lbsAmapLogic.getLocationByAddress(userAddressBean.getProvince() + userAddressBean.getCity() + userAddressBean.getArea() + userAddressBean.getStreet() + userAddressBean.getAddress());
+
+        userAddress.setLng(locationByAddress.split(",")[0]);
+        userAddress.setLat(locationByAddress.split(",")[1]);
+
         userAddress.setCompanyWechatId(currentCompanyWechat.getCurrentCompanyWechatId());
         userAddress.setCompanyName(currentCompanyWechat.getCurrentCompanyName());
         userAddressService.save(userAddress);
@@ -76,6 +85,8 @@ public class AddressLogic {
                     .set(UserAddress::getDefaultAddr, false)
                     .update();
         }
+
+        String locationByAddress = lbsAmapLogic.getLocationByAddress(userAddressBean.getProvince() + userAddressBean.getCity() + userAddressBean.getArea() + userAddressBean.getStreet() + userAddressBean.getAddress());
         userAddressService.lambdaUpdate().eq(UserAddress::getUserAddressId, userAddressBean.getUserAddressId())
                 .set(StringUtils.isNotBlank(userAddressBean.getAddress()), UserAddress::getAddress, userAddressBean.getAddress())
                 .set(StringUtils.isNotBlank(userAddressBean.getProvince()), UserAddress::getProvince, userAddressBean.getProvince())
@@ -85,6 +96,8 @@ public class AddressLogic {
                 .set(StringUtils.isNotBlank(userAddressBean.getName()), UserAddress::getName, userAddressBean.getName())
                 .set(StringUtils.isNotBlank(userAddressBean.getPhone()), UserAddress::getPhone, userAddressBean.getPhone())
                 .set(UserAddress::getStatus, status)
+                .set(UserAddress::getLat, locationByAddress.split(",")[1])
+                .set(UserAddress::getLng, locationByAddress.split(",")[0])
                 .set(StringUtils.isNotEmpty(userAddressBean.getStreet()), UserAddress::getStreet, userAddressBean.getStreet())
                 .set(!Objects.isNull(userAddressBean.getDefaultAddr()), UserAddress::getDefaultAddr, userAddressBean.getDefaultAddr())
                 .update();