102 lines
3.9 KiB
Java
102 lines
3.9 KiB
Java
|
|
package com.ruoyi.common.utils;
|
|||
|
|
|
|||
|
|
import cn.hutool.core.util.StrUtil;
|
|||
|
|
import com.alibaba.fastjson2.JSONArray;
|
|||
|
|
import com.alibaba.fastjson2.JSONObject;
|
|||
|
|
import com.ruoyi.common.constant.ServiceConstants;
|
|||
|
|
import com.ruoyi.common.utils.http.HttpUtils;
|
|||
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 业务工具类
|
|||
|
|
*
|
|||
|
|
* @author ruoyi
|
|||
|
|
*/
|
|||
|
|
@Slf4j
|
|||
|
|
public class CommonUtil {
|
|||
|
|
|
|||
|
|
public static final String GEO_WEB_KEY = SpringUtils.getRequiredProperty("geo.key");
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 格式化错误消息
|
|||
|
|
* @author ruoyi
|
|||
|
|
*/
|
|||
|
|
public static String format(String status) {
|
|||
|
|
String msg = "车辆";
|
|||
|
|
// 检查status是否为null,避免空指针异常
|
|||
|
|
if (status == null) {
|
|||
|
|
return "Status cannot be null.";
|
|||
|
|
}
|
|||
|
|
// 使用switch语句重构原来的if-else结构
|
|||
|
|
switch (status) {
|
|||
|
|
case ServiceConstants.VEHICLE_STATUS_NOT_LISTING:
|
|||
|
|
return msg + ServiceConstants.VEHICLE_STATUS_NOT_LISTING_STR;
|
|||
|
|
case ServiceConstants.VEHICLE_STATUS_IN_APPOINTMENT:
|
|||
|
|
return msg + ServiceConstants.VEHICLE_STATUS_IN_APPOINTMENT_STR;
|
|||
|
|
case ServiceConstants.VEHICLE_STATUS_IN_USING:
|
|||
|
|
return msg + ServiceConstants.VEHICLE_STATUS_IN_USING_STR;
|
|||
|
|
case ServiceConstants.VEHICLE_STATUS_TEMPORARILY_LOCK:
|
|||
|
|
return msg + ServiceConstants.VEHICLE_STATUS_TEMPORARILY_LOCK_STR;
|
|||
|
|
// case ServiceConstants.VEHICLE_STATUS_IN_REPAIR:
|
|||
|
|
// return msg + ServiceConstants.VEHICLE_STATUS_IN_REPAIR_STR;
|
|||
|
|
case ServiceConstants.VEHICLE_STATUS_IN_OFFLINE:
|
|||
|
|
return msg + ServiceConstants.VEHICLE_STATUS_IN_CHANGING_STR;
|
|||
|
|
case ServiceConstants.VEHICLE_STATUS_ABANDON:
|
|||
|
|
return msg + ServiceConstants.VEHICLE_STATUS_ABANDON_STR;
|
|||
|
|
default:
|
|||
|
|
// 处理未知或新增的状态
|
|||
|
|
return "Unknown vehicle status: " + status;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根据定位获取地址
|
|||
|
|
* @author ruoyi
|
|||
|
|
*/
|
|||
|
|
public static String getAddressByGeo(String location) {
|
|||
|
|
String address = null;
|
|||
|
|
try {
|
|||
|
|
String url = StrUtil.format("https://restapi.amap.com/v3/geocode/regeo?key={}&location={}&&radius=1000&extensions=all", GEO_WEB_KEY, location);
|
|||
|
|
String result = HttpUtils.sendGet(url);
|
|||
|
|
log.info("【根据定位获取地址】请求结果result:{}",result);
|
|||
|
|
//将json字符串转换为Object
|
|||
|
|
JSONObject jsonObject = JSONObject.parseObject(result,JSONObject.class);
|
|||
|
|
JSONObject regeocode1 = jsonObject.getJSONObject("regeocode");
|
|||
|
|
address = regeocode1.getString("formatted_address");
|
|||
|
|
log.info("【根据定位获取地址】address=:【{}】",result);
|
|||
|
|
return address;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
log.error("geocode regeo", e);
|
|||
|
|
}
|
|||
|
|
return address;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取省市县
|
|||
|
|
*
|
|||
|
|
* @author ruoyi
|
|||
|
|
*/
|
|||
|
|
public static String getDistrictList() {
|
|||
|
|
String list = null;
|
|||
|
|
try {
|
|||
|
|
String url = StrUtil.format("https://restapi.amap.com/v3/config/district?key={}&keywords={}&subdistrict=3", GEO_WEB_KEY, "中国");
|
|||
|
|
String result = HttpUtils.sendGet(url);
|
|||
|
|
// log.info("【获取省市县】请求结果result:{}",result);
|
|||
|
|
//将json字符串转换为Object
|
|||
|
|
JSONObject jsonObject = JSONObject.parseObject(result,JSONObject.class);
|
|||
|
|
JSONArray districts = jsonObject.getJSONArray("districts");
|
|||
|
|
JSONObject jsonArray = (JSONObject)districts.get(0);
|
|||
|
|
JSONArray districts1 = jsonArray.getJSONArray("districts");
|
|||
|
|
list = districts1.toString();
|
|||
|
|
// log.info("【获取省市县】result=:【{}】",list);
|
|||
|
|
return list;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
log.error("【获取省市县】失败", e);
|
|||
|
|
}
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
}
|