From d0bd61d3b3f637750bb21587dfa81503f2d4b6a8 Mon Sep 17 00:00:00 2001
From: zhibing.pu <393733352@qq.com>
Date: 星期一, 24 六月 2024 11:07:02 +0800
Subject: [PATCH] 修改google地图对接bug

---
 ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleMap/FleetEngineUtil.java |   78 +++++++++++++++++++-------------------
 1 files changed, 39 insertions(+), 39 deletions(-)

diff --git a/ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleMap/FleetEngineUtil.java b/ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleMap/FleetEngineUtil.java
index 79c051d..4be7b9f 100644
--- a/ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleMap/FleetEngineUtil.java
+++ b/ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleMap/FleetEngineUtil.java
@@ -54,7 +54,7 @@
 	 * @param type 0=服务端,1=乘客,2=司机
 	 * @return
 	 */
-	public String fleetEngineAuth(int type, Integer id){
+	public Map<String, Object> fleetEngineAuth(int type, Integer id){
 		try {
 			//谷歌云服务器使用这部分代码
 //			AuthTokenMinter minter = AuthTokenMinter.builder()
@@ -75,23 +75,53 @@
 					.build();
 			
 			String jwt = "";
+			Long expirationTimestamp = 0L;
 			if(0 == type){
 				FleetEngineToken serverToken = minter.getServerToken();
 				jwt = serverToken.jwt();
+				expirationTimestamp = serverToken.expirationTimestamp().getTime();
 			}
 			if(1 == type){
 				FleetEngineToken consumerToken = minter.getConsumerToken(TripClaims.create("I-GO-USER" + id));
 				jwt = consumerToken.jwt();
+				expirationTimestamp = consumerToken.expirationTimestamp().getTime();
 			}
 			if(2 == type){
 				FleetEngineToken driverToken = minter.getDriverToken(VehicleClaims.create("I-GO-CAR" + id));
 				jwt = driverToken.jwt();
+				expirationTimestamp = driverToken.expirationTimestamp().getTime();
 			}
-			return jwt;
+			Map<String, Object> map = new HashMap<>();
+			map.put("token", jwt);
+			map.put("expirationTimestamp", expirationTimestamp);
+			return map;
 		} catch (SigningTokenException e) {
 			throw new RuntimeException(e);
 		}
 	}
+	
+	
+	
+	public String getToken(){
+		String token_json = redisUtil.getValue("google_token");
+		String google_token = "";
+		if(!StringUtils.hasLength(token_json)){
+			Map<String, Object> map = fleetEngineAuth(0, null);
+			redisUtil.setStrValue("google_token", JSON.toJSONString(map));
+			google_token = map.get("token").toString();
+		}else{
+			JSONObject jsonObject1 = JSON.parseObject(token_json);
+			Long expirationTimestamp = jsonObject1.getLong("expirationTimestamp");
+			google_token = jsonObject1.getString("token");
+			if((expirationTimestamp - 10000L) <= System.currentTimeMillis()){
+				Map<String, Object> map = fleetEngineAuth(0, null);
+				redisUtil.setStrValue("google_token", JSON.toJSONString(map));
+				google_token = map.get("token").toString();
+			}
+		}
+		return google_token;
+	}
+	
 	
 	
 	
@@ -103,15 +133,10 @@
 	 * @param id 车辆id
 	 */
 	public String createVehicles(int maximumCapacity, String licensePlate, String id) throws Exception{
-		String google_token = redisUtil.getValue("google_token");
-		if(!StringUtils.hasLength(google_token)){
-			google_token = fleetEngineAuth(0, null);
-			redisUtil.setStrValue("google_token", google_token);
-		}
 		String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/vehicles?vehicleId=" + id;
 		HttpRequest post = HttpUtil.createPost(url);
 		Map<String, String> headers = new HashMap<>();
-		headers.put("Authorization", "Bearer " + google_token);
+		headers.put("Authorization", "Bearer " + getToken());
 		headers.put("Content-Type", "application/json");
 		post.addHeaders(headers);
 		
@@ -177,11 +202,6 @@
 	 * @return
 	 */
 	public String updateVehicles(String vehicleState, Integer maximumCapacity, String licensePlate, String id) throws Exception{
-		String google_token = redisUtil.getValue("google_token");
-		if(!StringUtils.hasLength(google_token)){
-			google_token = fleetEngineAuth(0, null);
-			redisUtil.setStrValue("google_token", google_token);
-		}
 		String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/vehicles/" + id + "?updateMask=";
 		List<String> sb = new ArrayList<>();
 		if(StringUtils.hasLength(vehicleState)){
@@ -198,7 +218,7 @@
 		
 		HttpRequest put = HttpUtil.createRequest(Method.PUT, url);
 		Map<String, String> headers = new HashMap<>();
-		headers.put("Authorization", "Bearer " + google_token);
+		headers.put("Authorization", "Bearer " + getToken());
 		headers.put("Content-Type", "application/json");
 		put.addHeaders(headers);
 		
@@ -273,15 +293,10 @@
 	 * @return
 	 */
 	public String getVehicles(String id) throws Exception{
-		String google_token = redisUtil.getValue("google_token");
-		if(!StringUtils.hasLength(google_token)){
-			google_token = fleetEngineAuth(0, null);
-			redisUtil.setStrValue("google_token", google_token);
-		}
-		String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/vehicles" + (null != id ? "/" + id : "");
+		String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/vehicles/" + (null != id ? id : "");
 		HttpRequest get = HttpUtil.createGet(url);
 		Map<String, String> headers = new HashMap<>();
-		headers.put("Authorization", "Bearer " + google_token);
+		headers.put("Authorization", "Bearer " + getToken());
 		headers.put("Content-Type", "application/json");
 		get.addHeaders(headers);
 		
@@ -382,15 +397,10 @@
 	 * @return
 	 */
 	public String createTrip(String vehicleId, Integer numberOfPassengers, String tripId, String start_lat, String start_lng, String end_lat, String end_lng) throws Exception{
-		String google_token = redisUtil.getValue("google_token");
-		if(!StringUtils.hasLength(google_token)){
-			google_token = fleetEngineAuth(0, null);
-			redisUtil.setStrValue("google_token", google_token);
-		}
 		String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/trips?tripId=" + tripId;
 		HttpRequest post = HttpUtil.createPost(url);
 		Map<String, String> headers = new HashMap<>();
-		headers.put("Authorization", "Bearer " + google_token);
+		headers.put("Authorization", "Bearer " + getToken());
 		headers.put("Content-Type", "application/json");
 		post.addHeaders(headers);
 		
@@ -564,11 +574,6 @@
 	 * @return
 	 */
 	public String updateTrip(String tripStatus, String vehicleId, Integer numberOfPassengers, String tripId, String start_lat, String start_lng, String end_lat, String end_lng) throws Exception {
-		String google_token = redisUtil.getValue("google_token");
-		if(!StringUtils.hasLength(google_token)){
-			google_token = fleetEngineAuth(0, null);
-			redisUtil.setStrValue("google_token", google_token);
-		}
 		String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/trips/" + tripId + "?updateMask=";
 		List<String> sb = new ArrayList<>();
 		if(StringUtils.hasLength(vehicleId)){
@@ -592,7 +597,7 @@
 		
 		HttpRequest put = HttpUtil.createRequest(Method.PUT, url);
 		Map<String, String> headers = new HashMap<>();
-		headers.put("Authorization", "Bearer " + google_token);
+		headers.put("Authorization", "Bearer " + getToken());
 		headers.put("Content-Type", "application/json");
 		put.addHeaders(headers);
 		
@@ -780,15 +785,10 @@
 	 * @return
 	 */
 	public String getTrip(String tripId) throws Exception {
-		String google_token = redisUtil.getValue("google_token");
-		if(!StringUtils.hasLength(google_token)){
-			google_token = fleetEngineAuth(0, null);
-			redisUtil.setStrValue("google_token", google_token);
-		}
 		String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/trips/" + tripId;
 		HttpRequest get = HttpUtil.createGet(url);
 		Map<String, String> headers = new HashMap<>();
-		headers.put("Authorization", "Bearer " + google_token);
+		headers.put("Authorization", "Bearer " + getToken());
 		headers.put("Content-Type", "application/json");
 		get.addHeaders(headers);
 		

--
Gitblit v1.7.1