From 66d22c623d35e7e5c7d2df29bdee5f8d01b71c52 Mon Sep 17 00:00:00 2001 From: 18650502300 <18650502300@163.com> Date: Sat, 25 May 2024 16:00:38 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=A0=B9=E6=8D=AE=E5=AE=9A=E4=BD=8D=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=BF=90=E8=90=A5=E5=8C=BA=E8=AF=A6=E7=BB=86=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/app/AppController.java | 20 +++++- .../controller/system/SysLoginController.java | 8 +-- .../common/core/domain/entity/AsUser.java | 11 ++++ .../com/ruoyi/common/utils/map/GeoUtils.java | 23 +++++++ .../web/service/SysLoginService.java | 11 +++- .../service/IEtOperatingAreaService.java | 9 +++ .../impl/EtOperatingAreaServiceImpl.java | 64 +++++++++++++++++++ 7 files changed, 138 insertions(+), 8 deletions(-) diff --git a/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppController.java b/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppController.java index 4b84093..3637807 100644 --- a/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppController.java +++ b/electripper-admin/src/main/java/com/ruoyi/web/controller/app/AppController.java @@ -88,7 +88,23 @@ public class AppController extends BaseController @GetMapping(value = "/area/{areaId}") public AjaxResult getInfo(@PathVariable("areaId") Long areaId) { - return success(etOperatingAreaService.selectEtOperatingAreaByAreaId(areaId)); + EtOperatingArea area = etOperatingAreaService.selectEtOperatingAreaByAreaId(areaId); + return success(area); + } + + + /** + * 根据定位获取运营区详细信息 + */ + @GetMapping(value = "/area/info") + public AjaxResult areaInfo(String longitude,String latitude) + { + if(StrUtil.isBlank(longitude) || StrUtil.isBlank(latitude)){ + logger.info("没有经纬度参数:【longitude={}】,【latitude={}】",longitude,latitude); + return error("请传经纬度参数"+"【longitude="+longitude+"】,【latitude="+latitude+"】"); + } + EtOperatingArea area = etOperatingAreaService.getAreaInfoByLocation(longitude,latitude); + return success(area); } /** @@ -122,7 +138,7 @@ public class AppController extends BaseController logger.info("没有经纬度参数:【longitude={}】,【latitude={}】",longitude,latitude); return error("请传经纬度参数"+"【longitude="+longitude+"】,【latitude="+latitude+"】"); } - webSocket.SendMessage("需要发送的消息", "识别唯一session"); +// webSocket.SendMessage("需要发送的消息", "识别唯一session"); List asDevices = asDeviceService.vehicleLocalization(longitude,latitude); return success(asDevices); } diff --git a/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java b/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java index c198d26..c41171b 100644 --- a/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java +++ b/electripper-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java @@ -128,10 +128,10 @@ public class SysLoginController * 微信登录 */ @PostMapping("/wxlogin") - public AjaxResult wxlogin(String mobileCode,String jsCode) { + public AjaxResult wxlogin(String mobileCode,String jsCode,String areaId) { AjaxResult ajax = AjaxResult.success(); // 生成令牌 - String token = loginService.wxloing(mobileCode,jsCode); + String token = loginService.wxloing(mobileCode,jsCode,areaId); ajax.put(Constants.TOKEN, token); return ajax; } @@ -140,10 +140,10 @@ public class SysLoginController * 根据openid静默登录 */ @PostMapping("/loginByopenid") - public AjaxResult loginByopenid(String jsCode) { + public AjaxResult loginByopenid(String jsCode,String areaId) { AjaxResult ajax = AjaxResult.success(); // 生成令牌 - String token = loginService.loginByopenid(jsCode); + String token = loginService.loginByopenid(jsCode,areaId); ajax.put(Constants.TOKEN, token); return ajax; } diff --git a/electripper-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java b/electripper-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java index bf4864a..cc3e3fb 100644 --- a/electripper-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java +++ b/electripper-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java @@ -103,6 +103,17 @@ public class AsUser extends BaseEntity /** 系统用户id */ private Long sysUserId; + /** 运营区id */ + private Long areaId; + + public Long getAreaId() { + return areaId; + } + + public void setAreaId(Long areaId) { + this.areaId = areaId; + } + /** 最后一笔订单 */ private EtOrderDto latestOrder; diff --git a/electripper-common/src/main/java/com/ruoyi/common/utils/map/GeoUtils.java b/electripper-common/src/main/java/com/ruoyi/common/utils/map/GeoUtils.java index d8cd7dc..87c8afa 100644 --- a/electripper-common/src/main/java/com/ruoyi/common/utils/map/GeoUtils.java +++ b/electripper-common/src/main/java/com/ruoyi/common/utils/map/GeoUtils.java @@ -27,6 +27,7 @@ import org.opengis.referencing.operation.MathTransform; import java.io.File; import java.util.Collection; import java.util.Collections; +import java.util.List; /** @@ -241,4 +242,26 @@ public class GeoUtils { double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return EARTH_RADIUS * c; } + + /** + * 判获取到最近一个运营区 + * */ + public static boolean getNearestOperatingArea (String longitude, String latitude, List polygon) { + double lon = Double.parseDouble(longitude); + double lat = Double.parseDouble(latitude); + + GeometryFactory geometryFactory = new GeometryFactory(); + Coordinate coordinate = new Coordinate(lon, lat); + Point point = geometryFactory.createPoint(coordinate); + + for (Geometry geometry : polygon) { + if (geometry.contains(point)) { + return true; + }else{ + + } + } + return false; + } + } diff --git a/electripper-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/electripper-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index 753fccd..6045bd7 100644 --- a/electripper-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/electripper-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -198,7 +198,7 @@ public class SysLoginService /** * 微信登录 */ - public String wxloing(String mobileCode,String jsCode) { + public String wxloing(String mobileCode,String jsCode,String areaId) { //根据jsCode换取openid String openId = getOpenId(jsCode); String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="; @@ -229,8 +229,12 @@ public class SysLoginService asUser.setLoginDate(DateUtils.getNowDate()); asUser.setCreateTime(DateUtils.getNowDate()); asUser.setWxopenid(openId); + asUser.setAreaId(Long.parseLong(areaId)); int i = asUserService.insertUser(asUser); user = asUser; + }else{ + user.setAreaId(Long.parseLong(areaId)); + int i = asUserService.updateUser(user); } Authentication authentication = null; // 用户验证 try { @@ -273,12 +277,15 @@ public class SysLoginService /** * 根据openid静默登录 */ - public String loginByopenid(String jsCode) { + public String loginByopenid(String jsCode,String areaId) { //根据jsCode换取openid String openid = getOpenId(jsCode); AsUser user = asUserService.selectUserByWxopenid(openid); if(ObjectUtils.isEmpty(user)){ throw new ServiceException("未查询到用户信息"); + }else{ + user.setAreaId(Long.parseLong(areaId)); + int i = asUserService.updateUser(user); } Authentication authentication = null; // 用户验证 try { diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/IEtOperatingAreaService.java b/electripper-system/src/main/java/com/ruoyi/system/service/IEtOperatingAreaService.java index 2f34037..10b0630 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/IEtOperatingAreaService.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/IEtOperatingAreaService.java @@ -78,4 +78,13 @@ public interface IEtOperatingAreaService extends IService * @return 结果 */ public boolean checkAreaNameUnique(EtOperatingArea etOperatingArea); + + /** + * 根据定位获取运营区详细信息 + * + * @param longitude 经度 + * @param latitude 纬度 + * @return 结果 + */ + EtOperatingArea getAreaInfoByLocation(String longitude, String latitude); } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOperatingAreaServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOperatingAreaServiceImpl.java index 8a9ef50..a20084f 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOperatingAreaServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/EtOperatingAreaServiceImpl.java @@ -2,6 +2,7 @@ package com.ruoyi.system.service.impl; import java.util.List; +import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -12,6 +13,7 @@ import com.ruoyi.common.core.domain.entity.SysDictType; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.map.GeoUtils; +import com.ruoyi.system.domain.AsDevice; import com.ruoyi.system.domain.EtAreaRule; import com.ruoyi.system.domain.SysStudent; import com.ruoyi.system.mapper.EtAreaRuleMapper; @@ -20,7 +22,10 @@ import com.ruoyi.system.service.IAsDeviceService; import com.ruoyi.system.service.IEtFeeRuleService; import jdk.nashorn.internal.ir.annotations.Reference; import lombok.extern.slf4j.Slf4j; +import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.Point; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.system.mapper.EtOperatingAreaMapper; @@ -52,6 +57,9 @@ public class EtOperatingAreaServiceImpl extends ServiceImpl etOperatingAreas = etOperatingAreaService.selectEtOperatingAreaList(new EtOperatingArea()); + EtOperatingArea area = null; + for(EtOperatingArea etOperatingArea:etOperatingAreas){ + Geometry geometry = GeoUtils.fromWkt(etOperatingArea.getBoundary()); + Boolean inCircle = GeoUtils.isInCircle(longitude, latitude, geometry); + if(inCircle){ + area = etOperatingArea; + break; + } + } + if(ObjectUtil.isNotNull(area)){ + /** 在运营区内直接返回*/ + log.info("在【{}】运营区内",area.getAreaName()); + return area; + }else{ + /** 不在任何运营区内,获取到最近一个运营区*/ + log.info("不在任何运营区内,获取到最近一个运营区"); + EtOperatingArea closestArea = null; + double minDistance = Double.MAX_VALUE; + GeometryFactory geometryFactory = new GeometryFactory(); + Coordinate coordinate = new Coordinate(lon, lat); + Point point = geometryFactory.createPoint(coordinate); + for(EtOperatingArea etOperatingArea:etOperatingAreas){ + Geometry geometry = GeoUtils.fromWkt(etOperatingArea.getBoundary()); + if (geometry.contains(point)) { + return etOperatingArea; + }else{ + // 获取多边形的外边界 + Coordinate[] coordinates = geometry.getCoordinates(); + for (Coordinate coord : coordinates) { + double distance = GeoUtils.calculateDistance(lat, lon, coord.y, coord.x); + if (distance < minDistance) { + minDistance = distance; + closestArea = etOperatingArea; + } + } + } + } + return closestArea; + } + } + + + }