Преглед изворни кода

fixed 高德地图接口集成

fushengqian пре 2 месеци
родитељ
комит
4981c8edd3

+ 2 - 1
fuint-application/src/main/java/com/fuint/common/enums/OrderSettingEnum.java

@@ -11,7 +11,8 @@ public enum OrderSettingEnum {
     DELIVERY_MIN_AMOUNT("deliveryMinAmount", "订单起送金额"),
     IS_CLOSE("isClose", "关闭交易功能"),
     MP_UPLOAD_SHIPPING("mpUploadShipping", "微信小程序上传发货信息"),
-    PAY_OFF_LINE("payOffLine", "开启前台支付功能");
+    PAY_OFF_LINE("payOffLine", "开启前台支付功能"),
+    DELIVERY_RANGE("deliveryRange", "配送范围");
 
     private String key;
 

+ 9 - 0
fuint-application/src/main/java/com/fuint/common/service/StoreService.java

@@ -140,4 +140,13 @@ public interface StoreService extends IService<MtStore> {
      * */
     Map<String, Object> getLatAndLngByAddress(String addr);
 
+    /**
+     * 获取步行距离
+     *
+     * @param origin 起点经纬度 格式如:116.434446,39.90816
+     * @param destination 终点经纬度 格式如:116.434307,39.90909
+     * @return
+     * */
+    Double getDistance(String origin, String destination);
+
 }

+ 38 - 0
fuint-application/src/main/java/com/fuint/common/service/impl/OrderServiceImpl.java

@@ -632,6 +632,44 @@ public class OrderServiceImpl extends ServiceImpl<MtOrderMapper, MtOrder> implem
                 } else {
                     throw new BusinessCheckException("配送地址出错了,请重新选择配送地址");
                 }
+
+                // 是否超出起送范围
+                MtSetting deliveryRange = settingService.querySettingByName(orderInfo.getMerchantId(), SettingTypeEnum.ORDER.getKey(), OrderSettingEnum.DELIVERY_RANGE.getKey());
+                if (deliveryRange != null && StringUtil.isNotEmpty(deliveryRange.getValue()) && (Double.parseDouble(deliveryRange.getValue()) > 0)) {
+                    MtStore mtStore = storeService.queryStoreById(orderInfo.getStoreId());
+                    if (mtStore != null && StringUtil.isNotEmpty(mtStore.getLatitude()) && StringUtil.isNotEmpty(mtStore.getLongitude())) {
+                        String address = "";
+                        if (mtAddress.getProvinceId() != null && mtAddress.getProvinceId() > 0) {
+                            MtRegion mtProvince = mtRegionMapper.selectById(mtAddress.getProvinceId());
+                            if (mtProvince != null) {
+                                address = mtProvince.getName();
+                            }
+                        }
+                        if (mtAddress.getCityId() != null && mtAddress.getCityId() > 0) {
+                            MtRegion mtCity = mtRegionMapper.selectById(mtAddress.getCityId());
+                            if (mtCity != null) {
+                                address = address + mtCity.getName();
+                            }
+                        }
+                        if (mtAddress.getRegionId() != null && mtAddress.getRegionId() > 0) {
+                            MtRegion mtRegion = mtRegionMapper.selectById(mtAddress.getRegionId());
+                            if (mtRegion != null) {
+                                address = address + mtRegion.getName();
+                            }
+                        }
+                        address = address + mtAddress.getDetail();
+                        Map<String, Object> latAndLng = storeService.getLatAndLngByAddress(address);
+                        if (StringUtil.isNotEmpty(latAndLng.get("lat").toString()) && StringUtil.isNotEmpty(latAndLng.get("lng").toString())) {
+                            Double distance = storeService.getDistance(mtStore.getLongitude() + "," + mtStore.getLatitude(), latAndLng.get("lng").toString() + "," + latAndLng.get("lat").toString());
+                            Double limitDistance = Double.parseDouble(deliveryRange.getValue());
+                            logger.info("订单地址:{},配送距离为:{}", address, distance);
+                            if (distance > limitDistance) {
+                                throw new BusinessCheckException("抱歉,配送距离超过了" + limitDistance + "公里,请重新选择配送地址!");
+                            }
+                        }
+                    }
+                }
+
                 MtOrderAddress orderAddress = new MtOrderAddress();
                 orderAddress.setOrderId(orderInfo.getId());
                 orderAddress.setUserId(orderDto.getUserId());

+ 37 - 1
fuint-application/src/main/java/com/fuint/common/service/impl/StoreServiceImpl.java

@@ -24,6 +24,7 @@ import com.fuint.repository.mapper.MtStoreGoodsMapper;
 import com.fuint.repository.mapper.MtStoreMapper;
 import com.fuint.repository.model.MtMerchant;
 import com.fuint.repository.model.MtStore;
+import com.fuint.utils.HttpUtil;
 import com.fuint.utils.StringUtil;
 import com.github.pagehelper.Page;
 import com.github.pagehelper.PageHelper;
@@ -516,7 +517,7 @@ public class StoreServiceImpl extends ServiceImpl<MtStoreMapper, MtStore> implem
     public Map<String, Object> getLatAndLngByAddress(String addr) {
         String key = env.getProperty("amap.key");
         Map<String, Object> map = new HashMap<>();
-        if (key == null) {
+        if (key == null || key.length() < 10) {
             map.put("lat", "");
             map.put("lng", "");
             return map;
@@ -580,4 +581,39 @@ public class StoreServiceImpl extends ServiceImpl<MtStoreMapper, MtStore> implem
         return map;
     }
 
+    /**
+     * 测量步行距离
+     *
+     * @param origin 起点经纬度 格式如:116.434446,39.90816
+     * @param destination 终点经纬度 格式如:116.434307,39.90909
+     * @return
+     * */
+    public Double getDistance(String origin, String destination) {
+        String key = env.getProperty("amap.key");
+        if (StringUtil.isEmpty(key)) {
+            return 0d;
+        }
+        String url = "https://restapi.amap.com/v3/direction/walking?origin="+origin+"&destination="+destination+"&key="+key;
+        String response = HttpUtil.sendRequest(url);
+        if (StringUtil.isEmpty(response)) {
+            return 0d;
+        }
+        JSONObject resultJson = JSON.parseObject(response);
+        if (resultJson != null && "1".equals(resultJson.getString("status"))) {
+            JSONObject route = resultJson.getJSONObject("route");
+            if (route != null) {
+                JSONArray paths = route.getJSONArray("paths");
+                if (paths != null && paths.size() > 0) {
+                    JSONObject path = paths.getJSONObject(0);
+                    String distance = path.getString("distance");
+                    if (distance != null) {
+                        return Double.parseDouble(distance)/1000;
+                    }
+                }
+            }
+
+        }
+        return 0d;
+    }
+
 }

+ 9 - 2
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendOrderController.java

@@ -369,6 +369,7 @@ public class BackendOrderController extends BaseController {
         String deliveryMinAmount = "";
         String mpUploadShipping = "";
         String payOffLine = "off";
+        String deliveryRange = "0";
 
         for (MtSetting setting : settingList) {
             if (setting.getName().equals(OrderSettingEnum.DELIVERY_FEE.getKey())) {
@@ -381,6 +382,8 @@ public class BackendOrderController extends BaseController {
                 mpUploadShipping = setting.getValue();
             } else if (setting.getName().equals(OrderSettingEnum.PAY_OFF_LINE.getKey())) {
                 payOffLine = setting.getValue();
+            } else if (setting.getName().equals(OrderSettingEnum.DELIVERY_RANGE.getKey())) {
+                deliveryRange = setting.getValue();
             }
         }
 
@@ -389,17 +392,18 @@ public class BackendOrderController extends BaseController {
         result.put("deliveryMinAmount", deliveryMinAmount);
         result.put("mpUploadShipping", mpUploadShipping);
         result.put("payOffLine", payOffLine);
+        result.put("deliveryRange", deliveryRange);
 
         return getSuccessResult(result);
     }
 
     /**
-     * 保存订单设置
+     * 保存交易设置
      *
      * @param request HttpServletRequest对象
      * @return
      */
-    @ApiOperation(value = "保存订单设置")
+    @ApiOperation(value = "保存交易设置")
     @RequestMapping(value = "/saveSetting", method = RequestMethod.POST)
     @CrossOrigin
     @PreAuthorize("@pms.hasPermission('order:setting')")
@@ -409,6 +413,7 @@ public class BackendOrderController extends BaseController {
         String isClose = param.get("isClose") != null ? param.get("isClose").toString() : YesOrNoEnum.FALSE.getKey();
         String deliveryMinAmount = param.get("deliveryMinAmount") != null ? param.get("deliveryMinAmount").toString() : "0";
         String payOffLine = param.get("payOffLine") != null ? param.get("payOffLine").toString() : "off";
+        String deliveryRange = param.get("deliveryRange") != null ? param.get("deliveryRange").toString() : "";
 
         AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
         OrderSettingEnum[] settingList = OrderSettingEnum.values();
@@ -424,6 +429,8 @@ public class BackendOrderController extends BaseController {
                 info.setValue(deliveryMinAmount);
             } else if (setting.getKey().equals(OrderSettingEnum.PAY_OFF_LINE.getKey())) {
                 info.setValue(payOffLine);
+            } else if (setting.getKey().equals(OrderSettingEnum.DELIVERY_RANGE.getKey())) {
+                info.setValue(deliveryRange);
             }
             info.setMerchantId(accountInfo.getMerchantId());
             info.setStoreId(accountInfo.getStoreId());

+ 25 - 31
fuint-utils/src/main/java/com/fuint/utils/HttpUtil.java

@@ -1,9 +1,10 @@
 package com.fuint.utils;
 
-import org.apache.commons.lang.StringUtils;
 import java.io.*;
-import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
+import java.net.HttpURLConnection;
 import java.nio.charset.Charset;
 import java.util.Iterator;
 import java.util.Map;
@@ -40,42 +41,35 @@ public class HttpUtil {
      * 发送http请求
      *
      * @param url
-     * @param data
-     * @param method
      * @return
      * @throws IOException
      */
-    public static String sendRequest(URL url, String data, Method method) throws IOException {
-        HttpURLConnection client = (HttpURLConnection) url.openConnection();
-        client.setConnectTimeout(CONNECT_TIMEOUT);
-        client.setReadTimeout(READ_TIMEOUT);
-        client.setRequestMethod(method.value);
-        if (!StringUtils.isEmpty(data)) {
-            client.setRequestProperty("Content-length", "" + data.length());
+    public static String sendRequest(String url) {
+        URL myURL = null;
+        URLConnection httpsConn;
+        // 进行转码
+        try {
+            myURL = new URL(url);
+        } catch (MalformedURLException e) {
+            // empty
         }
-        if (Method.POST.equals(method)) {
-            // 发送数据
-            if (data != null) {
-                client.setDoOutput(true);
-                OutputStreamWriter osw = new OutputStreamWriter(client.getOutputStream(), "UTF-8");
-                osw.write(data);
-                osw.flush();
-                osw.close();
+        StringBuffer sb = new StringBuffer();
+        try {
+            httpsConn = myURL.openConnection();
+            if (httpsConn != null) {
+                InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
+                BufferedReader br = new BufferedReader(insr);
+                String data = null;
+                while ((data = br.readLine()) != null) {
+                    sb.append(data);
+                }
+                insr.close();
             }
-        }
-        // 发送请求
-        client.connect();
-        if (client.getResponseCode() >= 300) {
-            throw new ServerUnavailable(url, client.getResponseCode(), client.getResponseMessage());
+        } catch (IOException e) {
+            return "";
         }
 
-        // 获取响应
-        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream(), UTF8));
-        StringBuilder response = new StringBuilder();
-        for (String line = in.readLine(); line != null; line = in.readLine()) {
-            response.append(line);
-        }
-        return response.toString();
+        return sb.toString();
     }
 
     public static String sendRequest(URL url, String data, Method method, Map<String, String> headers) throws IOException {