记得上下班打卡 | git大法好,push需谨慎

Commit 4fa42401 authored by stonepy's avatar stonepy

修正json格式校验

parent 13cae844
...@@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Map;
@Service @Service
public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigService { public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigService {
...@@ -46,22 +45,18 @@ public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigSer ...@@ -46,22 +45,18 @@ public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigSer
if (exist == null) { if (exist == null) {
return ResponseDto.failure("手册不存在"); return ResponseDto.failure("手册不存在");
} }
try { SweetManual update = SweetManual.getNew();
SweetManual update = SweetManual.getNew(); update.setFoodGuideUrl(nullToEmpty(param.getFoodGuideUrl()));
update.setFoodGuideUrl(nullToEmpty(param.getFoodGuideUrl())); update.setAlbumUrl(nullToEmpty(param.getAlbumUrl()));
update.setAlbumUrl(nullToEmpty(param.getAlbumUrl())); update.setLostFoundWjxId(nullToEmpty(param.getLostFoundWjxId()));
update.setLostFoundWjxId(nullToEmpty(param.getLostFoundWjxId())); if (param.getMapGeojson() != null) {
if (param.getMapGeojson() != null) { update.setMapGeojson(serializeGeojson(param.getMapGeojson()));
update.setMapGeojson(serializeGeojson(param.getMapGeojson()));
}
update.setUpdatedAt(LocalDateTime.now());
sweetManualMapper.update(update, Wrappers.lambdaUpdate(SweetManual.class)
.eq(SweetManual::getManualId, param.getManualId()));
redisDataUtils.setManualExtConfigRedisData(param.getManualId(), toDto(exist, param));
return ResponseDto.success(true);
} catch (IllegalArgumentException e) {
return ResponseDto.failure(e.getMessage());
} }
update.setUpdatedAt(LocalDateTime.now());
sweetManualMapper.update(update, Wrappers.lambdaUpdate(SweetManual.class)
.eq(SweetManual::getManualId, param.getManualId()));
redisDataUtils.setManualExtConfigRedisData(param.getManualId(), toDto(exist, param));
return ResponseDto.success(true);
} }
@Override @Override
...@@ -87,20 +82,16 @@ public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigSer ...@@ -87,20 +82,16 @@ public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigSer
if (exist == null) { if (exist == null) {
return ResponseDto.failure("手册不存在"); return ResponseDto.failure("手册不存在");
} }
try { String geojson = serializeGeojson(param.getMapGeojson());
String geojson = serializeGeojson(param.getMapGeojson()); SweetManual update = SweetManual.getNew();
SweetManual update = SweetManual.getNew(); update.setMapGeojson(geojson);
update.setMapGeojson(geojson); update.setUpdatedAt(LocalDateTime.now());
update.setUpdatedAt(LocalDateTime.now()); sweetManualMapper.update(update, Wrappers.lambdaUpdate(SweetManual.class)
sweetManualMapper.update(update, Wrappers.lambdaUpdate(SweetManual.class) .eq(SweetManual::getManualId, param.getManualId()));
.eq(SweetManual::getManualId, param.getManualId())); SweetManualExtConfigDto cacheDto = toDto(exist);
SweetManualExtConfigDto cacheDto = toDto(exist); cacheDto.setMapGeojson(parseGeojson(geojson));
cacheDto.setMapGeojson(parseGeojson(geojson)); redisDataUtils.setManualExtConfigRedisData(param.getManualId(), cacheDto);
redisDataUtils.setManualExtConfigRedisData(param.getManualId(), cacheDto); return ResponseDto.success(true);
return ResponseDto.success(true);
} catch (IllegalArgumentException e) {
return ResponseDto.failure(e.getMessage());
}
} }
private SweetManualExtConfigDto toDto(SweetManual manual) { private SweetManualExtConfigDto toDto(SweetManual manual) {
...@@ -138,27 +129,11 @@ public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigSer ...@@ -138,27 +129,11 @@ public class SweetManualExtConfigServiceImpl implements ISweetManualExtConfigSer
if (geojson == null) { if (geojson == null) {
return null; return null;
} }
String json;
if (geojson instanceof String) { if (geojson instanceof String) {
json = ((String) geojson).trim(); String json = ((String) geojson).trim();
if (json.isEmpty()) { return json.isEmpty() ? null : json;
return null;
}
} else {
json = JsonUtils.toJson(geojson);
}
validateGeojson(json);
return json;
}
private void validateGeojson(String json) {
Map<?, ?> map = JsonUtils.fromJson(json, Map.class);
if (map == null) {
throw new IllegalArgumentException("地图GeoJSON格式不正确");
}
if (!"FeatureCollection".equals(map.get("type"))) {
throw new IllegalArgumentException("地图GeoJSON必须为FeatureCollection格式");
} }
return JsonUtils.toJson(geojson);
} }
private String nullToEmpty(String value) { private String nullToEmpty(String value) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment