提供给lianlian获取token

This commit is contained in:
2023-05-26 19:25:35 +08:00
parent 9a238d6ae2
commit caae13281d
7 changed files with 1437 additions and 1256 deletions

View File

@@ -10,13 +10,16 @@ import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.dto.*;
import com.jsowell.thirdparty.lianlian.common.CommonResult;
import com.jsowell.thirdparty.lianlian.domain.StationStatsInfo;
import com.jsowell.thirdparty.lianlian.dto.QueryTokenDTO;
import com.jsowell.thirdparty.lianlian.service.LianLianService;
import com.jsowell.thirdparty.lianlian.vo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Map;
/**
* 对接联联平台controller
@@ -317,7 +320,13 @@ public class LianLianController extends BaseController {
* http://localhost:8080/LianLian/v1/query_token
*/
@PostMapping("/v1/query_token")
public CommonResult<?> queryToken() {
return null;
public CommonResult<?> queryToken(QueryTokenDTO dto) {
try {
Map<String, String> map = lianLianService.generateToken(dto);
return CommonResult.success(0, "", map.get("data"), map.get("sig"));
} catch (UnsupportedEncodingException e) {
logger.error("获取token接口 异常");
return CommonResult.failed("获取token发生异常");
}
}
}

View File

@@ -1,3 +1,4 @@
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.ImmutableMap;
@@ -18,24 +19,11 @@ import com.jsowell.netty.command.ykc.ProofreadTimeCommand;
import com.jsowell.netty.handler.HeartbeatRequestHandler;
import com.jsowell.netty.service.yunkuaichong.YKCBusinessService;
import com.jsowell.netty.service.yunkuaichong.YKCPushCommandService;
import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.domain.PileAuthCard;
import com.jsowell.pile.domain.PileBillingDetail;
import com.jsowell.pile.domain.PileBillingTemplate;
import com.jsowell.pile.domain.PileStationInfo;
import com.jsowell.pile.domain.WxpayCallbackRecord;
import com.jsowell.pile.domain.*;
import com.jsowell.pile.dto.*;
import com.jsowell.pile.mapper.MemberBasicInfoMapper;
import com.jsowell.pile.mapper.PileBillingTemplateMapper;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.service.IPileAuthCardService;
import com.jsowell.pile.service.IPileBasicInfoService;
import com.jsowell.pile.service.IPileBillingTemplateService;
import com.jsowell.pile.service.IPileMsgRecordService;
import com.jsowell.pile.service.IPileStationInfoService;
import com.jsowell.pile.service.SimCardService;
import com.jsowell.pile.service.WechatPayService;
import com.jsowell.pile.service.WxpayCallbackRecordService;
import com.jsowell.pile.service.*;
import com.jsowell.pile.vo.web.BillingTemplateVO;
import com.jsowell.pile.vo.web.OrderListVO;
import com.jsowell.pile.vo.web.PileDetailVO;
@@ -43,7 +31,11 @@ import com.jsowell.service.MemberService;
import com.jsowell.service.OrderService;
import com.jsowell.service.PileRemoteService;
import com.jsowell.service.PileService;
import com.jsowell.thirdparty.lianlian.dto.QueryTokenDTO;
import com.jsowell.thirdparty.lianlian.service.LianLianService;
import com.jsowell.thirdparty.lianlian.util.Cryptos;
import com.jsowell.thirdparty.lianlian.util.Encodes;
import com.jsowell.thirdparty.lianlian.util.GBSignUtils;
import com.jsowell.wxpay.common.WeChatPayParameter;
import com.jsowell.wxpay.dto.AppletTemplateMessageSendDTO;
import com.jsowell.wxpay.dto.WeChatRefundDTO;
@@ -59,6 +51,7 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StopWatch;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -144,6 +137,38 @@ public class SpringBootTestController {
static final String ALGORITHM_MAC = "HmacMD5";
@Test
public void testGenerateLianlianToken() throws UnsupportedEncodingException {
String OperatorID = "425010765";
String OperatorSecret = "123123123123aaaa"; // 1234567890abcdef
//请求dataX
Map<String, String> data = new HashMap<>();
data.put("OperatorID", OperatorID);
data.put("OperatorSecret", OperatorSecret);
String dataJson = JSONUtil.toJsonStr(data);
//加密
byte[] encryptText = Cryptos.aesEncrypt(dataJson.getBytes("UTF-8"),
OperatorSecret.getBytes(), OperatorSecret.getBytes());
String strData = Encodes.encodeBase64(encryptText);
Map<String, String> request = new LinkedHashMap<>();
request.put("OperatorID", OperatorID);
request.put("Data", strData);
request.put("TimeStamp", System.currentTimeMillis() + "");
request.put("Seq", "0001");
//生成签名
String sig = GBSignUtils.sign(request, OperatorSecret);
request.put("Sig", sig);
String tokenRequest = JSONUtil.toJsonStr(request);
QueryTokenDTO dto = JSONObject.parseObject(tokenRequest, QueryTokenDTO.class);
lianLianService.generateToken(dto);
}
@Test
public void testupdateElecAmount() {
orderBasicInfoService.updateElecAmount();

View File

@@ -55,7 +55,7 @@ public class JWTUtils {
* @param ttlMillis 过期时间 毫秒
* @return Token String 凭证
*/
private static String createToken(String id, String subject, long ttlMillis) {
public static String createToken(String id, String subject, long ttlMillis) {
// 签名方法 HS256
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;

View File

@@ -0,0 +1,24 @@
package com.jsowell.thirdparty.lianlian.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class QueryTokenDTO {
@JsonProperty(value = "OperatorID")
private String operatorID;
@JsonProperty(value = "Data")
private String data;
@JsonProperty(value = "TimeStamp")
private String timeStamp;
@JsonProperty(value = "Seq")
private String seq;
@JsonProperty(value = "Sig")
private String sig;
}

View File

@@ -2,9 +2,12 @@ package com.jsowell.thirdparty.lianlian.service;
import com.jsowell.pile.dto.*;
import com.jsowell.thirdparty.lianlian.domain.StationStatsInfo;
import com.jsowell.thirdparty.lianlian.dto.QueryTokenDTO;
import com.jsowell.thirdparty.lianlian.vo.*;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
public interface LianLianService {
@@ -150,4 +153,6 @@ public interface LianLianService {
* @return
*/
String pushPileChargeStatusChange(String orderCode);
Map<String, String> generateToken(QueryTokenDTO dto) throws UnsupportedEncodingException;
}

View File

@@ -7,6 +7,7 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.RealTimeMonitorData;
import com.jsowell.common.enums.lianlian.LianLianPileStatusEnum;
@@ -18,6 +19,7 @@ import com.jsowell.common.enums.ykc.PileConnectorDataBaseStatusEnum;
import com.jsowell.common.enums.ykc.PileStatusEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.JWTUtils;
import com.jsowell.common.util.PageUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.netty.command.ykc.StartChargingCommand;
@@ -34,6 +36,7 @@ import com.jsowell.pile.vo.web.PileConnectorInfoVO;
import com.jsowell.pile.vo.web.PileModelInfoVO;
import com.jsowell.pile.vo.web.PileStationVO;
import com.jsowell.thirdparty.lianlian.domain.*;
import com.jsowell.thirdparty.lianlian.dto.QueryTokenDTO;
import com.jsowell.thirdparty.lianlian.service.LianLianService;
import com.jsowell.thirdparty.lianlian.util.Cryptos;
import com.jsowell.thirdparty.lianlian.util.Encodes;
@@ -44,6 +47,7 @@ import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@@ -101,10 +105,11 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 根据站点id推送站点信息 notification_stationInfo
*
* @param dto
*/
@Override
public String pushStationInfo(LianLianPushStationInfoDTO dto){
public String pushStationInfo(LianLianPushStationInfoDTO dto) {
// String OperatorID = "987654321";
// String SigSecret = "1234567890abcdef"; // 签名秘钥
// String DataSecret = "1234567890abcdef"; // 消息密钥
@@ -189,13 +194,14 @@ public class LianLianServiceImpl implements LianLianService {
return result;
}
public static void main(String[] args){
public static void main(String[] args) {
}
/**
* 查询充电站信息
* 对外接口 query_stations_info
*
* @return
*/
@Override
@@ -340,6 +346,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 查询统计信息
*
* @param dto
* @return
*/
@@ -425,6 +432,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 请求设备认证
*
* @param dto
* @return
*/
@@ -469,6 +477,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 请求启动充电
*
* @param dto
*/
public QueryStartChargeVO query_start_charge(QueryStartChargeDTO dto) {
@@ -517,6 +526,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 查询充电状态
*
* @param startChargeSeq
* @return
*/
@@ -534,10 +544,11 @@ public class LianLianServiceImpl implements LianLianService {
if (StringUtils.equals(orderStatus, OrderStatusEnum.IN_THE_CHARGING.getValue())) {
// 充电中
orderStatus = "2";
}if (StringUtils.equals(orderStatus, OrderStatusEnum.ORDER_COMPLETE.getValue())) {
}
if (StringUtils.equals(orderStatus, OrderStatusEnum.ORDER_COMPLETE.getValue())) {
// 充电完成
orderStatus = "4";
}else {
} else {
// 直接给 5-未知
orderStatus = "5";
}
@@ -566,6 +577,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 请求停止充电
*
* @param dto
* @return
*/
@@ -595,6 +607,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 从联联平台获取令牌
*
* @param operatorId
* @param operatorSecret
* @return
@@ -605,16 +618,16 @@ public class LianLianServiceImpl implements LianLianService {
// String operatorSecret = dto.getOperatorSecret();
String token = "";
try {
//测试用请求地址
// 测试用请求地址
String requestUrl = urlAddress + "query_token";
//请求data
// 请求data
Map<String, String> data = new HashMap<>();
data.put("OperatorID", operatorId);
data.put("OperatorSecret", operatorSecret);
String dataJson = JSONUtil.toJsonStr(data);
//加密
// 加密
byte[] encryptText = Cryptos.aesEncrypt(dataJson.getBytes("UTF-8"),
operatorSecret.getBytes(), operatorSecret.getBytes());
String strData = Encodes.encodeBase64(encryptText);
@@ -625,7 +638,7 @@ public class LianLianServiceImpl implements LianLianService {
request.put("TimeStamp", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
request.put("Seq", "0001");
//生成签名
// 生成签名
String sig = GBSignUtils.sign(request, operatorSecret);
request.put("Sig", sig);
@@ -635,7 +648,7 @@ public class LianLianServiceImpl implements LianLianService {
LianLianResultVO result = JSON.parseObject(response, LianLianResultVO.class);
if (result.getRet() == 0) {
//解密data
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64((String) result.getData()),
operatorSecret.getBytes(), operatorSecret.getBytes());
String dataStr = new String(plainText, "UTF-8");
@@ -650,6 +663,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 联联平台推送 设备状态变化推送 notification_stationStatus
*
* @param pileConnectorCode
* @param status
* @return
@@ -694,6 +708,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 推送订单信息 notification_orderInfo
*
* @param orderCode
* @return
*/
@@ -742,10 +757,10 @@ public class LianLianServiceImpl implements LianLianService {
if (StringUtils.equals(orderBasicInfo.getPayMode(), OrderPayModeEnum.PAYMENT_OF_WECHATPAY.getValue())) {
// 微信支付
orderInfo.setPayChannel(Integer.valueOf(PayChannelEnum.WEXIN_PAY.getCode()));
}else if (StringUtils.equals(orderBasicInfo.getPayMode(), OrderPayModeEnum.PAYMENT_OF_ALIPAY.getValue())) {
} else if (StringUtils.equals(orderBasicInfo.getPayMode(), OrderPayModeEnum.PAYMENT_OF_ALIPAY.getValue())) {
// 支付宝支付
orderInfo.setPayChannel(Integer.valueOf(PayChannelEnum.ALI_PAY.getCode()));
}else {
} else {
// 其他
orderInfo.setPayChannel(Integer.valueOf(PayChannelEnum.OTHER.getCode()));
}
@@ -767,7 +782,7 @@ public class LianLianServiceImpl implements LianLianService {
detail.setDetailPower(orderDetail.getSharpUsedElectricity());
detail.setDetailElecMoney(orderDetail.getSharpElectricityPrice());
detail.setDetailSeviceMoney(orderDetail.getSharpServicePrice());
}else if (StringUtils.equals(billingPriceVO.getTimeType(), "2")) {
} else if (StringUtils.equals(billingPriceVO.getTimeType(), "2")) {
// 峰时段
detail.setDetailStartTime(billingPriceVO.getStartTime());
detail.setDetailEndTime(billingPriceVO.getEndTime());
@@ -776,7 +791,7 @@ public class LianLianServiceImpl implements LianLianService {
detail.setDetailPower(orderDetail.getPeakUsedElectricity());
detail.setDetailElecMoney(orderDetail.getPeakElectricityPrice());
detail.setDetailSeviceMoney(orderDetail.getPeakServicePrice());
}else if (StringUtils.equals(billingPriceVO.getTimeType(), "3")) {
} else if (StringUtils.equals(billingPriceVO.getTimeType(), "3")) {
// 平时段
detail.setDetailStartTime(billingPriceVO.getStartTime());
detail.setDetailEndTime(billingPriceVO.getEndTime());
@@ -785,7 +800,7 @@ public class LianLianServiceImpl implements LianLianService {
detail.setDetailPower(orderDetail.getFlatUsedElectricity());
detail.setDetailElecMoney(orderDetail.getFlatElectricityPrice());
detail.setDetailSeviceMoney(orderDetail.getFlatServicePrice());
}else if (StringUtils.equals(billingPriceVO.getTimeType(), "4")) {
} else if (StringUtils.equals(billingPriceVO.getTimeType(), "4")) {
// 谷时段
detail.setDetailStartTime(billingPriceVO.getStartTime());
detail.setDetailEndTime(billingPriceVO.getEndTime());
@@ -817,6 +832,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 推送启动充电结果
*
* @param orderCode
* @return
*/
@@ -851,10 +867,10 @@ public class LianLianServiceImpl implements LianLianService {
if (StringUtils.equals(OrderStatusEnum.IN_THE_CHARGING.getValue(), orderStatus)) {
// 充电中
orderStatus = "2";
}else if (StringUtils.equals(OrderStatusEnum.NOT_START.getValue(), orderStatus)) {
} else if (StringUtils.equals(OrderStatusEnum.NOT_START.getValue(), orderStatus)) {
// 未启动
orderStatus = "1";
}else {
} else {
// 其他状态都为 未知
orderStatus = "5";
}
@@ -874,6 +890,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 推送充电状态(仅桩充电时调)
*
* @param orderCode
* @return
*/
@@ -911,6 +928,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 推送停止充电结果(仅在 交易记录的帧类型中调用)
*
* @param orderCode
* @return
*/
@@ -941,11 +959,11 @@ public class LianLianServiceImpl implements LianLianService {
if (StringUtils.equals(orderStatus, OrderStatusEnum.IN_THE_CHARGING.getValue())) {
// 充电中
orderStatus = "2";
}else if (StringUtils.equals(orderStatus, OrderStatusEnum.ORDER_COMPLETE.getValue())) {
} else if (StringUtils.equals(orderStatus, OrderStatusEnum.ORDER_COMPLETE.getValue())) {
// 订单完成
orderStatus = "4";
successFlag = "0";
}else {
} else {
orderStatus = "5";
}
// 获取token
@@ -972,6 +990,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 推送充电订单信息
*
* @param orderCode
* @return
*/
@@ -1019,6 +1038,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 推送订单结算信息
*
* @param dto
* @return
*/
@@ -1050,6 +1070,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 查询订单结算信息
*
* @param orderCode
* @return
*/
@@ -1078,7 +1099,7 @@ public class LianLianServiceImpl implements LianLianService {
// 获取令牌
String token = getToken(urlAddress, operatorId, operatorSecret);
if (StringUtils.isBlank(token)){
if (StringUtils.isBlank(token)) {
return null;
}
// 发送请求
@@ -1089,6 +1110,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 推送订单对账结果信息
*
* @param orderCode
* @return
*/
@@ -1135,7 +1157,7 @@ public class LianLianServiceImpl implements LianLianService {
// 获取令牌
String token = getToken(urlAddress, operatorId, operatorSecret);
if (StringUtils.isBlank(token)){
if (StringUtils.isBlank(token)) {
return null;
}
String jsonString = JSONObject.toJSONString(json);
@@ -1147,6 +1169,7 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 设备充电中状态变化推送 notification_connector_charge_status
*
* @param orderCode
* @return
*/
@@ -1191,7 +1214,7 @@ public class LianLianServiceImpl implements LianLianService {
// 获取令牌
String token = getToken(urlAddress, operatorId, operatorSecret);
if (StringUtils.isBlank(token)){
if (StringUtils.isBlank(token)) {
return null;
}
JSONObject json = new JSONObject();
@@ -1204,6 +1227,55 @@ public class LianLianServiceImpl implements LianLianService {
return result;
}
@Override
public Map<String, String> generateToken(QueryTokenDTO dto) throws UnsupportedEncodingException {
String operatorID = dto.getOperatorID();
// 通过operatorID 查出 operatorSecret
String operatorSecret = "123123123123aaaa";
Map<String, String> map = Maps.newLinkedHashMap();
map.put("OperatorID", dto.getOperatorID());
map.put("Data", dto.getData());
map.put("TimeStamp", dto.getTimeStamp());
map.put("Seq", dto.getSeq());
String sign = GBSignUtils.sign(map, operatorSecret);
System.out.println(sign);
// 验证签名 得到请求方传过来的签名sig->自己拿到请求体后再按双方约定的协议生成一个sig->对比两个sig是否一致
if (!StringUtils.equals(dto.getSig(), sign)) {
System.out.println("签名校验==失败");
return null;
}
System.out.println("签名校验通过!!!");
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dto.getData()), operatorSecret.getBytes(), operatorSecret.getBytes());
String dataStr = new String(plainText, "UTF-8");
Map<String, String> resMap = (Map<String, String>) JSON.parse(dataStr);
// 对比解出来的密钥是否和数据库中保存的一致
// 生成token返回 eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0MjUwMTA3NjUiLCJpYXQiOjE2ODUwOTcxMTYsInN1YiI6IjEyMzEyMzEyMzEyM2FhYWEiLCJleHAiOjY4NjkwOTcxMTZ9.NyxOUIZmgsqtfex7oiMRR2LaWePTA56WHVMXIkWWt2w
long ttlMillis = 60 * 60 * 24 * 1000;
String token = JWTUtils.createToken(operatorID, operatorSecret, ttlMillis);
System.out.println(token);
// 组装返回参数
AccessTokenVO vo = new AccessTokenVO();
vo.setAccessToken(token);
vo.setOperatorID(operatorID);
vo.setTokenAvailableTime(String.valueOf(ttlMillis / 1000));
vo.setFailReason("0");
vo.setSuccStat("0");
Map<String, String> resultMap = Maps.newLinkedHashMap();
// 加密数据
resultMap.put("data", "");
// 生成sig
resultMap.put("sig", "");
return resultMap;
}
/**
* TODO 请求打印充电小票
*/
@@ -1213,10 +1285,11 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 获取桩列表信息
*
* @param pileStationInfo
* @return
*/
private List<EquipmentInfo> getPileList(PileStationInfo pileStationInfo){
private List<EquipmentInfo> getPileList(PileStationInfo pileStationInfo) {
List<EquipmentInfo> resultList = new ArrayList<>();
// 通过站点id查询桩基本信息
List<PileBasicInfo> list = pileBasicInfoService.getPileListByStationId(String.valueOf(pileStationInfo.getId()));
@@ -1262,10 +1335,11 @@ public class LianLianServiceImpl implements LianLianService {
/**
* 获取枪口列表
*
* @param pileBasicInfo
* @return
*/
private List<ConnectorInfo> getConnectorList(PileBasicInfo pileBasicInfo){
private List<ConnectorInfo> getConnectorList(PileBasicInfo pileBasicInfo) {
List<ConnectorInfo> resultList = new ArrayList<>();
List<PileConnectorInfo> list = pileConnectorInfoService.selectPileConnectorInfoList(pileBasicInfo.getSn());

View File

@@ -0,0 +1,44 @@
package com.jsowell.thirdparty.lianlian.vo;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class AccessTokenVO {
/**
* 字符串 对接平台组织机构代码
*/
@JsonProperty(value = "OperatorID")
private String operatorID;
/**
* 整型
* 0:成功;
* 1:失败
*/
@JsonProperty(value = "SuccStat")
private String succStat;
/**
* 字符串 全局唯一凭证
*/
@JsonProperty(value = "AccessToken")
private String accessToken;
/**
* 整型 凭证有效期,单位秒
*/
@JsonProperty(value = "TokenAvailableTime")
private String tokenAvailableTime;
/**
* 整型
* 0:无;
* 1:无此对接平台;
* 2:密钥错误; 399:自定义
*/
@JsonProperty(value = "FailReason")
private String failReason;
}