parent
eea448ad56
commit
16105a418c
@ -0,0 +1,26 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.constant; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 13:38 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
public class Constant { |
||||||
|
|
||||||
|
public final static String KEY = "TourRunToken"; |
||||||
|
|
||||||
|
public final static Long TIMEOUT = 120L; |
||||||
|
|
||||||
|
public final static String MD5 = "md5"; |
||||||
|
|
||||||
|
public final static String CODE = "code"; |
||||||
|
|
||||||
|
public final static String message = "message"; |
||||||
|
|
||||||
|
public final static String RESULT = "result"; |
||||||
|
|
||||||
|
public final static String SUCCESS_CODE = "0"; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.controller; |
||||||
|
|
||||||
|
import com.zilber.boot.module.vehicle.dto.pr.DevicePR; |
||||||
|
import com.zilber.boot.module.vehicle.service.TourRunService; |
||||||
|
import com.zilber.boot.utils.AjaxResult; |
||||||
|
import com.zilber.boot.utils.page.PageUtils; |
||||||
|
import io.swagger.annotations.Api; |
||||||
|
import io.swagger.annotations.ApiOperation; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||||
|
import org.springframework.web.bind.annotation.RestController; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 11:57 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
@RestController |
||||||
|
@Api(tags = "车辆机械设备管理") |
||||||
|
@RequestMapping("/vehicle") |
||||||
|
public class VehicleController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private TourRunService service; |
||||||
|
|
||||||
|
@GetMapping("/list") |
||||||
|
@ApiOperation("设备列表") |
||||||
|
public AjaxResult list(DevicePR pr) { |
||||||
|
return AjaxResult.success(service.list(pr)); |
||||||
|
} |
||||||
|
|
||||||
|
@GetMapping("/h5") |
||||||
|
@ApiOperation("h5") |
||||||
|
public AjaxResult h5( |
||||||
|
@RequestParam(required = true) String tabType, |
||||||
|
@RequestParam(required = true) String imei) { |
||||||
|
return AjaxResult.success(service.h5(tabType, imei)); |
||||||
|
} |
||||||
|
|
||||||
|
@GetMapping("/token") |
||||||
|
@ApiOperation("获取途强token") |
||||||
|
public AjaxResult token(){ |
||||||
|
return AjaxResult.success(service.token()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.dto.pr; |
||||||
|
|
||||||
|
import com.zilber.boot.utils.page.Pageable; |
||||||
|
import io.swagger.annotations.ApiModel; |
||||||
|
import io.swagger.annotations.ApiModelProperty; |
||||||
|
import lombok.AllArgsConstructor; |
||||||
|
import lombok.Data; |
||||||
|
import lombok.NoArgsConstructor; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 11:52 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
@NoArgsConstructor |
||||||
|
@AllArgsConstructor |
||||||
|
@ApiModel("设备管理分页请求") |
||||||
|
public class DevicePR |
||||||
|
extends Pageable { |
||||||
|
|
||||||
|
@ApiModelProperty("使用范围") |
||||||
|
private String scope; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.enums; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 11:37 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
public enum Method { |
||||||
|
|
||||||
|
TOKEN("jimi.oauth.token.get"), |
||||||
|
REFRESH("jimi.oauth.token.refresh"), |
||||||
|
LIST("jimi.user.device.list"), |
||||||
|
; |
||||||
|
|
||||||
|
private String code; |
||||||
|
|
||||||
|
Method(String code) { |
||||||
|
this.code = code; |
||||||
|
} |
||||||
|
|
||||||
|
public String getCode() { |
||||||
|
return code; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCode(String code) { |
||||||
|
this.code = code; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.enums; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 11:40 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
public enum TabType { |
||||||
|
|
||||||
|
LocationIndex("locationIndex"),//定位
|
||||||
|
TrackIndex("trackIndex"),//轨迹
|
||||||
|
AlarmIndex("alarmIndex"),//告警
|
||||||
|
InstructionIndex("instructionIndex"),//指令
|
||||||
|
ReportIndex("reportIndex"),//报表
|
||||||
|
FenceIndex("fenceIndex"),//围栏
|
||||||
|
; |
||||||
|
|
||||||
|
private String type; |
||||||
|
|
||||||
|
TabType(String type) { |
||||||
|
this.type = type; |
||||||
|
} |
||||||
|
|
||||||
|
public String getType() { |
||||||
|
return type; |
||||||
|
} |
||||||
|
|
||||||
|
public void setType(String type) { |
||||||
|
this.type = type; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.properties; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-07 10:10 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
@Component |
||||||
|
public class TourRunProperties { |
||||||
|
|
||||||
|
@Value("${tourRun.appKey}") |
||||||
|
private String appKey; |
||||||
|
|
||||||
|
@Value("${tourRun.appSecret}") |
||||||
|
private String appSecret; |
||||||
|
|
||||||
|
@Value("${tourRun.tourRunAccount}") |
||||||
|
private String tourRunAccount; |
||||||
|
|
||||||
|
@Value("${tourRun.tourRunPassword}") |
||||||
|
private String tourRunPassword; |
||||||
|
|
||||||
|
@Value("${tourRun.openapiUrl}") |
||||||
|
private String openapiUrl; |
||||||
|
|
||||||
|
@Value("${tourRun.h5Url}") |
||||||
|
private String h5Url; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,21 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.service; |
||||||
|
|
||||||
|
import com.zilber.boot.module.vehicle.dto.pr.DevicePR; |
||||||
|
import com.zilber.boot.utils.page.PageUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 11:51 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
public interface TourRunService { |
||||||
|
|
||||||
|
PageUtils list(DevicePR pr); |
||||||
|
|
||||||
|
String h5(String tabType, String imei); |
||||||
|
|
||||||
|
String token(); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,246 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.service.impl; |
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject; |
||||||
|
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; |
||||||
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
||||||
|
import com.zilber.boot.exception.ServiceException; |
||||||
|
import com.zilber.boot.module.vehicle.constant.Constant; |
||||||
|
import com.zilber.boot.module.vehicle.dto.pr.DevicePR; |
||||||
|
import com.zilber.boot.module.vehicle.enums.Method; |
||||||
|
import com.zilber.boot.module.vehicle.properties.TourRunProperties; |
||||||
|
import com.zilber.boot.module.vehicle.service.TourRunService; |
||||||
|
import com.zilber.boot.module.vehicle.util.SignUtils; |
||||||
|
import com.zilber.boot.utils.StringUtils; |
||||||
|
import com.zilber.boot.utils.page.PageUtils; |
||||||
|
import org.apache.commons.codec.digest.DigestUtils; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.data.redis.core.RedisTemplate; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.net.HttpURLConnection; |
||||||
|
import java.text.SimpleDateFormat; |
||||||
|
import java.util.*; |
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
import java.util.function.Predicate; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
|
||||||
|
import static com.zilber.boot.module.vehicle.util.http.HttpUtils.sendGet; |
||||||
|
import static com.zilber.boot.module.vehicle.util.http.HttpUtils.sendPost; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 11:56 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description |
||||||
|
*/ |
||||||
|
@Service |
||||||
|
public class TourRunServiceImpl |
||||||
|
implements TourRunService { |
||||||
|
|
||||||
|
// 申请来的appKey和appSecret
|
||||||
|
// @Value("${tourRun.tourRunPassword}")
|
||||||
|
// private String appKey;
|
||||||
|
//
|
||||||
|
// @Value("${tourRun.tourRunPassword}")
|
||||||
|
// private String appSecret;
|
||||||
|
//
|
||||||
|
// @Value("${tourRun.tourRunPassword}")
|
||||||
|
// private String tourRunAccount;
|
||||||
|
//
|
||||||
|
// @Value("${tourRun.tourRunPassword}")
|
||||||
|
// private String tourRunPassword;
|
||||||
|
//
|
||||||
|
// @Value("${tourRun.openapiUrl}")
|
||||||
|
// private String openapiUrl;
|
||||||
|
//
|
||||||
|
// @Value("${tourRun.h5Url}")
|
||||||
|
// private String h5Url;
|
||||||
|
|
||||||
|
@Autowired |
||||||
|
private TourRunProperties properties; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private RedisTemplate<String, String> redisTemplate; |
||||||
|
|
||||||
|
@Override |
||||||
|
public PageUtils list(DevicePR pr) { |
||||||
|
String token = token(); |
||||||
|
|
||||||
|
Map<String, String> paramMap = paramMap(Method.LIST.getCode()); |
||||||
|
|
||||||
|
// 私有参数
|
||||||
|
paramMap.put("access_token", token); |
||||||
|
paramMap.put("target", properties.getTourRunAccount()); |
||||||
|
// 计算签名
|
||||||
|
String sign = ""; |
||||||
|
try { |
||||||
|
sign = SignUtils.signTopRequest(paramMap, properties.getAppSecret(), Constant.MD5); |
||||||
|
paramMap.put("sign", sign); |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
//调用com/zilber/boot/module/vehicle/util/http/HttpUtils方法
|
||||||
|
JSONObject jsonObject = sendPost(properties.getOpenapiUrl(), headerMap(), paramMap); |
||||||
|
//初始化列表
|
||||||
|
// List<JSONObject> resultList = null;
|
||||||
|
// if ( jsonObject != null) {
|
||||||
|
// if ( !jsonObject.get("code").toString().equals("0"))
|
||||||
|
// {
|
||||||
|
// throw new ServiceException(jsonObject.get("message").toString());
|
||||||
|
// }
|
||||||
|
// resultList = jsonObject.getList("result", JSONObject.class);
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// throw new ServiceException("列表获取失败");
|
||||||
|
// }
|
||||||
|
List<JSONObject> resultList = handleResult(jsonObject, JSONObject.class, "获取列表失败"); |
||||||
|
if (CollectionUtils.isNotEmpty(resultList)) |
||||||
|
{ |
||||||
|
List<JSONObject> handledList = resultList.stream() |
||||||
|
.filter(jo -> jo.get("mcTypeUseScope").toString().equals(pr.getScope())) |
||||||
|
.sorted((o1, o2) -> StringUtils.compare(o1.get("imei").toString(), o2.get("imei").toString())) |
||||||
|
.collect(Collectors.toList()); |
||||||
|
return new PageUtils( |
||||||
|
//将处理后的列表手动分页
|
||||||
|
handledList.stream() |
||||||
|
.skip((long) (pr.getPageNo() - 1) *pr.getPageSize()) |
||||||
|
.limit(pr.getPageSize()) |
||||||
|
.collect(Collectors.toList()), |
||||||
|
handledList.size(), |
||||||
|
pr.getPageSize(), |
||||||
|
pr.getPageNo()); |
||||||
|
} |
||||||
|
return new PageUtils(new ArrayList<>(), 0, pr.getPageSize(), pr.getPageNo()); |
||||||
|
} |
||||||
|
|
||||||
|
//https://openh5.aichezaixian.com/?token=XXX&imei=AAA&appKey=BBB&activeTab=CCC
|
||||||
|
@Override |
||||||
|
public String h5(String tabType, String imei) { |
||||||
|
if ( StringUtils.isEmpty(tabType)) { |
||||||
|
throw new ServiceException("tab栏位不能为空"); |
||||||
|
} |
||||||
|
if ( StringUtils.isEmpty(imei)){ |
||||||
|
throw new ServiceException("imei号不能为空"); |
||||||
|
} |
||||||
|
String suffix = "/?token={}&imei={}&appKey={}&activeTab={}"; |
||||||
|
String token = token(); |
||||||
|
|
||||||
|
String format = StringUtils.format(suffix, token, imei, properties.getAppKey(), tabType); |
||||||
|
return sendGet(properties.getH5Url() + format); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String token() { |
||||||
|
String token = redisTemplate.opsForValue().get(Constant.KEY); |
||||||
|
if (StringUtils.isEmpty(token)) { |
||||||
|
|
||||||
|
Map<String, String> paramMap = paramMap(Method.TOKEN.getCode()); |
||||||
|
|
||||||
|
// 私有参数
|
||||||
|
paramMap.put("user_id", properties.getTourRunAccount()); |
||||||
|
paramMap.put("user_pwd_md5", DigestUtils.md5Hex(properties.getTourRunPassword())); |
||||||
|
paramMap.put("expires_in", "120"); |
||||||
|
|
||||||
|
// 计算签名
|
||||||
|
String sign = ""; |
||||||
|
try { |
||||||
|
sign = SignUtils.signTopRequest(paramMap, properties.getAppSecret(), Constant.MD5); |
||||||
|
paramMap.put("sign", sign); |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
//调用com/zilber/boot/module/vehicle/util/http/HttpUtils方法
|
||||||
|
JSONObject jsonObject = sendPost(properties.getOpenapiUrl(), headerMap(), paramMap); |
||||||
|
// if ( jsonObject != null) {
|
||||||
|
// if ( !jsonObject.get("code").toString().equals("0"))
|
||||||
|
// {
|
||||||
|
// throw new ServiceException(jsonObject.get("message").toString());
|
||||||
|
// }
|
||||||
|
// token = ((JSONObject) jsonObject.get("result")).get("accessToken").toString();
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// throw new ServiceException("无法获取token");
|
||||||
|
// }
|
||||||
|
token = handleResult(jsonObject, "无法获取token"); |
||||||
|
redisTemplate.opsForValue().set(Constant.KEY, token, Constant.TIMEOUT, TimeUnit.SECONDS); |
||||||
|
} |
||||||
|
return token; |
||||||
|
} |
||||||
|
|
||||||
|
public static String getCurrentDate() { |
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||||||
|
return formatter.format(new Date()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 构造并初始化部分参数的请求头map |
||||||
|
* @return map |
||||||
|
*/ |
||||||
|
private Map<String, String> headerMap(){ |
||||||
|
Map<String, String> headerMap = new HashMap<>(); |
||||||
|
headerMap.put("Content-Type", "application/x-www-form-urlencoded"); |
||||||
|
return headerMap; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 构造并初始化部分参数的请求参数map |
||||||
|
* @return map |
||||||
|
*/ |
||||||
|
private Map<String, String> paramMap(String method){ |
||||||
|
Map<String, String> paramMap = new HashMap<>(); |
||||||
|
// 公共参数
|
||||||
|
paramMap.put("app_key", properties.getAppKey()); |
||||||
|
paramMap.put("v", "1.0"); |
||||||
|
paramMap.put("timestamp", getCurrentDate()); |
||||||
|
paramMap.put("sign_method", Constant.MD5); |
||||||
|
paramMap.put("format", "json"); |
||||||
|
paramMap.put("method", method); |
||||||
|
return paramMap; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理接收到的返回结果(列表) |
||||||
|
* @param result 返回结果 |
||||||
|
* @param clazz 数据类 |
||||||
|
* @param errorMessage 自定义错误信息(result为空时的异常信息) |
||||||
|
* @return |
||||||
|
* @param <T> |
||||||
|
*/ |
||||||
|
private <T> List<T> handleResult(JSONObject result, Class<T> clazz, String errorMessage) { |
||||||
|
List<T> resultList = null; |
||||||
|
if ( result != null) { |
||||||
|
if ( !result.get(Constant.CODE).toString().equals(Constant.SUCCESS_CODE)) { |
||||||
|
throw new ServiceException(result.get(Constant.message).toString()); |
||||||
|
} |
||||||
|
resultList = result.getList(Constant.RESULT, clazz); |
||||||
|
return resultList; |
||||||
|
} |
||||||
|
else { |
||||||
|
throw new ServiceException(errorMessage); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 处理接收到的返回结果(字符串) |
||||||
|
* @param result 结果 |
||||||
|
* @param errorMessage 自定义错误信息(result为空时的异常信息) |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private String handleResult(JSONObject result, String errorMessage) |
||||||
|
{ |
||||||
|
if ( result != null) { |
||||||
|
if ( !result.get(Constant.CODE).toString().equals(Constant.SUCCESS_CODE)) { |
||||||
|
throw new ServiceException(result.get(Constant.message).toString()); |
||||||
|
} |
||||||
|
return ((JSONObject) result.get("result")).get("accessToken").toString(); |
||||||
|
} |
||||||
|
else { |
||||||
|
throw new ServiceException(errorMessage); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,103 @@ |
|||||||
|
/* |
||||||
|
* COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2017. |
||||||
|
* ALL RIGHTS RESERVED. |
||||||
|
* |
||||||
|
* No part of this publication may be reproduced, stored in a retrieval system, or transmitted, |
||||||
|
* on any form or by any means, electronic, mechanical, photocopying, recording, |
||||||
|
* or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd. |
||||||
|
* |
||||||
|
* Amendment History: |
||||||
|
* |
||||||
|
* Date By Description |
||||||
|
* ------------------- ----------- ------------------------------------------- |
||||||
|
* 2017年4月5日 yaojianping Create the class
|
||||||
|
* http://www.jimilab.com/
|
||||||
|
*/ |
||||||
|
|
||||||
|
package com.zilber.boot.module.vehicle.util; |
||||||
|
|
||||||
|
import javax.crypto.Mac; |
||||||
|
import javax.crypto.SecretKey; |
||||||
|
import javax.crypto.spec.SecretKeySpec; |
||||||
|
import java.io.IOException; |
||||||
|
import java.security.GeneralSecurityException; |
||||||
|
import java.security.MessageDigest; |
||||||
|
import java.security.NoSuchAlgorithmException; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* @FileName CodeUtils.java |
||||||
|
* @Description: 服务器签名工具类 |
||||||
|
* |
||||||
|
* @Date 2017年4月5日 下午4:59:06 |
||||||
|
* @author yaojianping |
||||||
|
* @version 1.0 |
||||||
|
*/ |
||||||
|
public class SignUtils { |
||||||
|
|
||||||
|
public static String signTopRequest(Map<String, String> params, String secret, String signMethod) throws IOException { |
||||||
|
// 第一步:检查参数是否已经排序
|
||||||
|
String[] keys = params.keySet().toArray(new String[0]); |
||||||
|
Arrays.sort(keys); |
||||||
|
|
||||||
|
// 第二步:把所有参数名和参数值串在一起
|
||||||
|
StringBuilder query = new StringBuilder(); |
||||||
|
if ("md5".equals(signMethod)) { |
||||||
|
query.append(secret); |
||||||
|
} |
||||||
|
for (String key : keys) { |
||||||
|
String value = params.get(key); |
||||||
|
if (StringUtil.areNotEmpty(key, value)) { |
||||||
|
query.append(key).append(value); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// 第三步:使用MD5/HMAC加密
|
||||||
|
query.append(secret); |
||||||
|
byte[] bytes = encryptMD5(query.toString()); |
||||||
|
|
||||||
|
// 第四步:把二进制转化为大写的十六进制
|
||||||
|
return byte2hex(bytes); |
||||||
|
} |
||||||
|
|
||||||
|
public static byte[] encryptHMAC(String data, String secret) throws IOException { |
||||||
|
byte[] bytes = null; |
||||||
|
try { |
||||||
|
SecretKey secretKey = new SecretKeySpec(secret.getBytes("utf-8"), "HmacMD5"); |
||||||
|
Mac mac = Mac.getInstance(secretKey.getAlgorithm()); |
||||||
|
mac.init(secretKey); |
||||||
|
bytes = mac.doFinal(data.getBytes("utf-8")); |
||||||
|
} catch (GeneralSecurityException gse) { |
||||||
|
throw new IOException(gse.toString()); |
||||||
|
} |
||||||
|
return bytes; |
||||||
|
} |
||||||
|
|
||||||
|
public static byte[] encryptMD5(String data) throws IOException { |
||||||
|
return encryptMD5(data.getBytes("utf-8")); |
||||||
|
} |
||||||
|
|
||||||
|
private static byte[] encryptMD5(byte[] bytes) throws IOException { |
||||||
|
MessageDigest md = null; |
||||||
|
try { |
||||||
|
md = MessageDigest.getInstance("MD5"); |
||||||
|
} catch (NoSuchAlgorithmException e) { |
||||||
|
throw new IOException(e.toString()); |
||||||
|
} |
||||||
|
return md.digest(bytes); |
||||||
|
} |
||||||
|
|
||||||
|
public static String byte2hex(byte[] bytes) { |
||||||
|
StringBuilder sign = new StringBuilder(); |
||||||
|
for (int i = 0; i < bytes.length; i++) { |
||||||
|
String hex = Integer.toHexString(bytes[i] & 0xFF); |
||||||
|
if (hex.length() == 1) { |
||||||
|
sign.append("0"); |
||||||
|
} |
||||||
|
sign.append(hex.toUpperCase()); |
||||||
|
} |
||||||
|
return sign.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,135 @@ |
|||||||
|
/* |
||||||
|
* COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2017. |
||||||
|
* ALL RIGHTS RESERVED. |
||||||
|
* |
||||||
|
* No part of this publication may be reproduced, stored in a retrieval system, or transmitted, |
||||||
|
* on any form or by any means, electronic, mechanical, photocopying, recording, |
||||||
|
* or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd. |
||||||
|
* |
||||||
|
* Amendment History: |
||||||
|
* |
||||||
|
* Date By Description |
||||||
|
* ------------------- ----------- ------------------------------------------- |
||||||
|
* 2017年4月5日 yaojianping Create the class
|
||||||
|
* http://www.jimilab.com/
|
||||||
|
*/ |
||||||
|
|
||||||
|
package com.zilber.boot.module.vehicle.util; |
||||||
|
|
||||||
|
import java.util.regex.Matcher; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
|
||||||
|
/** |
||||||
|
* @FileName StringUtil.java |
||||||
|
* @Description: |
||||||
|
* |
||||||
|
* @Date 2017年4月5日 下午5:36:34 |
||||||
|
* @author yaojianping |
||||||
|
* @version 1.0 |
||||||
|
*/ |
||||||
|
public final class StringUtil { |
||||||
|
|
||||||
|
public static boolean areNotEmpty(String... values) { |
||||||
|
boolean result = true; |
||||||
|
if ((values == null) || (values.length == 0)) |
||||||
|
result = false; |
||||||
|
else { |
||||||
|
for (String value : values) { |
||||||
|
result &= !isEmpty(value); |
||||||
|
} |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isEmpty(String value) { |
||||||
|
int strLen; |
||||||
|
if ((value == null) || ((strLen = value.length()) == 0)) |
||||||
|
return true; |
||||||
|
for (int i = 0; i < strLen; i++) { |
||||||
|
if (!Character.isWhitespace(value.charAt(i))) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public static final char UNDERLINE = '_'; |
||||||
|
|
||||||
|
/** |
||||||
|
* 驼峰格式字符串转换为下划线格式字符串 |
||||||
|
* |
||||||
|
* @param param |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static String camelToUnderline(String param) { |
||||||
|
if (param == null || "".equals(param.trim())) { |
||||||
|
return ""; |
||||||
|
} |
||||||
|
int len = param.length(); |
||||||
|
StringBuilder sb = new StringBuilder(len); |
||||||
|
for (int i = 0; i < len; i++) { |
||||||
|
char c = param.charAt(i); |
||||||
|
if (Character.isUpperCase(c)) { |
||||||
|
sb.append(UNDERLINE); |
||||||
|
sb.append(Character.toLowerCase(c)); |
||||||
|
} else { |
||||||
|
sb.append(c); |
||||||
|
} |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 下划线格式字符串转换为驼峰格式字符串 |
||||||
|
* |
||||||
|
* @param param |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static String underlineToCamel(String param) { |
||||||
|
if (param == null || "".equals(param.trim())) { |
||||||
|
return ""; |
||||||
|
} |
||||||
|
int len = param.length(); |
||||||
|
StringBuilder sb = new StringBuilder(len); |
||||||
|
for (int i = 0; i < len; i++) { |
||||||
|
char c = param.charAt(i); |
||||||
|
if (c == UNDERLINE) { |
||||||
|
if (++i < len) { |
||||||
|
sb.append(Character.toUpperCase(param.charAt(i))); |
||||||
|
} |
||||||
|
} else { |
||||||
|
sb.append(c); |
||||||
|
} |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 下划线格式字符串转换为驼峰格式字符串2 |
||||||
|
* |
||||||
|
* @param param |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static String underlineToCamel2(String param) { |
||||||
|
if (param == null || "".equals(param.trim())) { |
||||||
|
return ""; |
||||||
|
} |
||||||
|
StringBuilder sb = new StringBuilder(param); |
||||||
|
Matcher mc = Pattern.compile("_").matcher(param); |
||||||
|
int i = 0; |
||||||
|
while (mc.find()) { |
||||||
|
int position = mc.end() - (i++); |
||||||
|
sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase()); |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
public static void main(String[] args) { |
||||||
|
String aaa = "app_version_fld"; |
||||||
|
System.out.println(underlineToCamel(aaa)); |
||||||
|
System.out.println(underlineToCamel2(aaa)); |
||||||
|
aaa = "appVersionFld"; |
||||||
|
System.out.println(camelToUnderline(aaa)); |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,96 @@ |
|||||||
|
package com.zilber.boot.module.vehicle.util.http; |
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject; |
||||||
|
import org.apache.http.Header; |
||||||
|
import org.apache.http.HttpEntity; |
||||||
|
import org.apache.http.HttpResponse; |
||||||
|
import org.apache.http.NameValuePair; |
||||||
|
import org.apache.http.client.entity.UrlEncodedFormEntity; |
||||||
|
import org.apache.http.client.methods.HttpGet; |
||||||
|
import org.apache.http.client.methods.HttpPost; |
||||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||||
|
import org.apache.http.impl.client.HttpClients; |
||||||
|
import org.apache.http.message.BasicHeader; |
||||||
|
import org.apache.http.message.BasicNameValuePair; |
||||||
|
import org.apache.http.util.EntityUtils; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author LJX |
||||||
|
* @TIME 2025-05-06 11:45 |
||||||
|
* @PROJECT intelligence-site |
||||||
|
* created by Intellij IDEA |
||||||
|
* Description 调用途强api的工具类 |
||||||
|
*/ |
||||||
|
public class HttpUtils { |
||||||
|
|
||||||
|
public static JSONObject sendPost(String url, Map<String, String> headerMap, Map<String, String> paramMap) { |
||||||
|
try { |
||||||
|
HttpPost post = new HttpPost(url); |
||||||
|
List<NameValuePair> list = new ArrayList<NameValuePair>(); |
||||||
|
for (String key : paramMap.keySet()) { |
||||||
|
list.add(new BasicNameValuePair(key, paramMap.get(key))); |
||||||
|
} |
||||||
|
post.setEntity(new UrlEncodedFormEntity(list, "UTF-8")); |
||||||
|
if (null != headerMap) { |
||||||
|
post.setHeaders(assemblyHeader(headerMap)); |
||||||
|
} |
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||||
|
HttpResponse response = httpClient.execute(post); |
||||||
|
HttpEntity entity = response.getEntity(); |
||||||
|
String result = EntityUtils.toString(entity, "utf-8"); |
||||||
|
System.out.println(result); |
||||||
|
return JSONObject.parseObject(result); |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public static String sendGet(String url) { |
||||||
|
try { |
||||||
|
HttpGet get = new HttpGet(url); |
||||||
|
List<NameValuePair> list = new ArrayList<NameValuePair>(); |
||||||
|
// for (String key : paramMap.keySet()) {
|
||||||
|
// list.add(new BasicNameValuePair(key, paramMap.get(key)));
|
||||||
|
// }
|
||||||
|
// post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
|
||||||
|
// if (null != headerMap) {
|
||||||
|
// post.setHeaders(assemblyHeader(headerMap));
|
||||||
|
// }
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||||
|
HttpResponse response = httpClient.execute(get); |
||||||
|
HttpEntity entity = response.getEntity(); |
||||||
|
String result = EntityUtils.toString(entity, "utf-8"); |
||||||
|
System.out.println(result); |
||||||
|
return result; |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 组装头部信息 |
||||||
|
* |
||||||
|
* @param headers |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private static Header[] assemblyHeader(Map<String, String> headers) { |
||||||
|
Header[] allHeader = new BasicHeader[headers.size()]; |
||||||
|
int i = 0; |
||||||
|
for (String str : headers.keySet()) { |
||||||
|
allHeader[i] = new BasicHeader(str, headers.get(str)); |
||||||
|
i++; |
||||||
|
} |
||||||
|
return allHeader; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue