package com.jsowell.thirdparty.zhongdianlian; import com.alibaba.fastjson2.JSONObject; import com.jsowell.common.annotation.Anonymous; import com.jsowell.common.core.controller.BaseController; import com.jsowell.common.util.JWTUtils; import com.jsowell.pile.dto.QueryStationInfoDTO; import com.jsowell.thirdparty.lianlian.common.CommonResult; import com.jsowell.thirdparty.lianlian.dto.CommonParamsDTO; 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.zhongdianlian.service.ZDLService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Map; /** * TODO * * @author Lemon * @Date 2023/9/14 15:31 */ @Anonymous @RestController @RequestMapping("/zdl") public class ZDLController extends BaseController { @Autowired private ZDLService zdlService; @Autowired private LianLianService lianLianService; /** * 获取token接口 * http://localhost:8080/zdl/v1/query_token */ @PostMapping("/v1/query_token") public CommonResult queryToken(@RequestBody CommonParamsDTO dto) { logger.info("中电联平台请求令牌 params:{}", JSONObject.toJSONString(dto)); try { Map map = zdlService.generateToken(dto); logger.info("中电联平台请求令牌 result:{}", JSONObject.toJSONString(map)); return CommonResult.success(0, "请求令牌成功!", map.get("Data"), map.get("Sig")); } catch (UnsupportedEncodingException e) { logger.error("中电联平台 请求令牌接口 异常"); return CommonResult.failed("获取token发生异常"); } } /** * 中电联平台查询充电站信息 * http://localhost:8080/zdl/v1/query_stations_info * @param dto * @return */ @PostMapping("/v1/query_stations_info") public CommonResult query_stations_info(HttpServletRequest request, @RequestBody CommonParamsDTO dto) { logger.info("中电联平台查询充电站信息 params:{}", JSONObject.toJSONString(dto)); try { // 校验令牌 String token = request.getHeader("Authorization"); if (!JWTUtils.checkThirdPartyToken(token)) { // 校验失败 return CommonResult.failed("令牌校验错误"); } // 校验签名 Map resultMap = lianLianService.checkoutSign(dto); if (resultMap == null) { // 签名错误 return CommonResult.failed("签名校验错误"); } String operatorSecret = resultMap.get("OperatorSecret"); String dataString = resultMap.get("Data"); // 解密data byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), operatorSecret.getBytes(), operatorSecret.getBytes()); String dataStr = new String(plainText, StandardCharsets.UTF_8); // 转换成相应对象 QueryStationInfoDTO queryStationInfoDTO = JSONObject.parseObject(dataStr, QueryStationInfoDTO.class); queryStationInfoDTO.setOperatorId(dto.getOperatorID()); Map map = zdlService.queryStationsInfo(queryStationInfoDTO); return CommonResult.success(0, "查询充电站信息成功!", map.get("Data"), map.get("Sig")); } catch (Exception e) { logger.info("中电联平台查询充电站信息 error:", e); e.printStackTrace(); } return CommonResult.failed("查询充电站信息发生异常"); } }