‘linchangsheng’ 1 week ago
parent
commit
b9c057cfae

+ 4 - 0
src/main/java/com/gree/mall/miniapp/bean/user/UserWxBean.java

@@ -27,4 +27,8 @@ public class UserWxBean extends User {
 
     @ApiModelProperty("门店信息")
     private AdminWebsit adminWebsit;
+
+
+    @ApiModelProperty("距离")
+    private Double distance;
 }

+ 3 - 1
src/main/java/com/gree/mall/miniapp/controller/user/UserController.java

@@ -108,9 +108,11 @@ public class UserController {
     @ApiOperation(value = "个人信息")
     public ResponseHelper<UserWxBean> detail(
             @ApiParam(required = true, value = "code") @RequestParam(required = true) String userId,
+            @ApiParam(required = false, value = "经纬度") @RequestParam(required = true) String lat,
+            @ApiParam(required = false, value = "经纬度") @RequestParam(required = true) String lng,
             HttpServletRequest request
     ) throws RemoteServiceException {
-        UserWxBean userWxBean = userLogic.userDetail(userId,request);
+        UserWxBean userWxBean = userLogic.userDetail(userId,request,lat,lng);
         return ResponseHelper.success(userWxBean);
     }
 

+ 28 - 1
src/main/java/com/gree/mall/miniapp/logic/user/UserLogic.java

@@ -2,6 +2,7 @@ package com.gree.mall.miniapp.logic.user;
 
 import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
+import cn.hutool.core.convert.Convert;
 import cn.hutool.core.util.IdUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -451,7 +452,7 @@ public class UserLogic {
     /**
      * 个人信息
      */
-    public UserWxBean userDetail(String userId,HttpServletRequest request) throws RemoteServiceException {
+    public UserWxBean userDetail(String userId,HttpServletRequest request,String lat,String lng) throws RemoteServiceException {
         User user = userService.getById(userId);
         if (user == null) {
             throw new RemoteServiceException(ResponseHelper.ResponseCode_AUTH_ERROR, "请求不合法");
@@ -466,10 +467,36 @@ public class UserLogic {
         if (!StringUtil.isEmpty(user.getWebsitId())) {
             AdminWebsit adminWebsit = adminWebsitService.getById(user.getWebsitId());
             userWxBean.setAdminWebsit(adminWebsit);
+            if (!StringUtil.isEmpty(lat)){
+                userWxBean.setDistance(this.haversine(Convert.toDouble(lat),Convert.toDouble(lng),Convert.toDouble(adminWebsit.getLat()),Convert.toDouble(adminWebsit.getLng())));
+            }
         }
         return userWxBean;
     }
 
+    public  double haversine(double lat1, double lon1, double lat2, double lon2) {
+        // 将坐标转换为弧度
+
+        int EARTH_RADIUS = 6371;
+
+        double lat1Rad = Math.toRadians(lat1);
+        double lon1Rad = Math.toRadians(lon1);
+        double lat2Rad = Math.toRadians(lat2);
+        double lon2Rad = Math.toRadians(lon2);
+        // 计算经纬度差值
+        double deltaLat = lat2Rad - lat1Rad;
+        double deltaLon = lon2Rad - lon1Rad;
+        // Haversine 公式
+        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
+                Math.cos(lat1Rad) * Math.cos(lat2Rad) *
+                        Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
+        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
+
+        // 返回距离(单位:公里)
+        return EARTH_RADIUS * c;
+    }
+
+
     /**
      * 我的收益
      */