mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-22 03:55:17 +08:00
117 lines
4.3 KiB
Java
117 lines
4.3 KiB
Java
package com.jsowell.thirdparty.huawei;
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.jsowell.common.constant.Constants;
|
|
import com.jsowell.common.core.redis.RedisCache;
|
|
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
|
|
import com.jsowell.common.util.DateUtils;
|
|
import com.jsowell.common.util.StringUtils;
|
|
import com.jsowell.pile.domain.ThirdPartySettingInfo;
|
|
import com.jsowell.pile.service.ThirdPartySettingInfoService;
|
|
import com.jsowell.thirdparty.lianlian.common.CommonResult;
|
|
import com.jsowell.thirdparty.lianlian.dto.CommonParamsDTO;
|
|
import com.jsowell.thirdparty.lianlian.util.Cryptos;
|
|
import com.jsowell.thirdparty.lianlian.util.Encodes;
|
|
import com.jsowell.thirdparty.lianlian.util.GBSignUtils;
|
|
import com.jsowell.thirdparty.zhongdianlian.dto.ZDLGetTokenDTO;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Date;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* 华为Service
|
|
*
|
|
* @author Lemon
|
|
* @Date 2024/1/19 14:10:02
|
|
*/
|
|
@Service
|
|
public class HuaweiServiceV2 {
|
|
|
|
@Autowired
|
|
private ThirdPartySettingInfoService thirdPartySettingInfoService;
|
|
|
|
@Autowired
|
|
private RedisCache redisCache;
|
|
|
|
/**
|
|
* 获取华为 Token
|
|
* @return
|
|
*/
|
|
public String getHuaWeiToken() {
|
|
String operatorId = Constants.OPERATORID_JIANG_SU;
|
|
String redisKey = "huawei_get_token:" + operatorId;
|
|
// 先查缓存
|
|
String cacheToken = redisCache.getCacheObject(redisKey);
|
|
if (StringUtils.isNotBlank(cacheToken)) {
|
|
return cacheToken;
|
|
}
|
|
// 通过华为的type查询出密钥配置
|
|
ThirdPartySettingInfo info = new ThirdPartySettingInfo();
|
|
info.setType(ThirdPlatformTypeEnum.HUA_WEI.getCode());
|
|
ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.selectSettingInfo(info);
|
|
|
|
String operatorSecret = settingInfo.getOperatorSecret();
|
|
String dataSecret = settingInfo.getDataSecret();
|
|
String dataSecretIv = settingInfo.getDataSecretIv();
|
|
String signSecret = settingInfo.getSignSecret();
|
|
String urlAddress = settingInfo.getUrlAddress();
|
|
|
|
String requestUrl = urlAddress + "query_token";
|
|
|
|
// 拼装参数
|
|
JSONObject jsonObject = new JSONObject();
|
|
jsonObject.put("OperatorID", operatorId);
|
|
jsonObject.put("OperatorSecret", operatorSecret);
|
|
|
|
// 加密
|
|
byte[] encryptText = Cryptos.aesEncrypt(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8),
|
|
dataSecret.getBytes(), dataSecretIv.getBytes());
|
|
String strData = Encodes.encodeBase64(encryptText);
|
|
|
|
Map<String, String> request = new LinkedHashMap<>();
|
|
request.put("OperatorID", operatorId);
|
|
request.put("Data", strData);
|
|
request.put("TimeStamp", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
|
|
request.put("Seq", "0001");
|
|
|
|
// 生成签名
|
|
String sig = GBSignUtils.sign(request, signSecret);
|
|
request.put("Sig", sig);
|
|
|
|
String tokenRequest = JSONObject.toJSONString(request);
|
|
String response = HttpUtil.post(requestUrl, tokenRequest);
|
|
|
|
CommonResult<?> commonResult = JSONObject.parseObject(response, CommonResult.class);
|
|
if (commonResult.getRet() != 0) {
|
|
return commonResult.getMsg();
|
|
}
|
|
// 解密data
|
|
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64((String) commonResult.getData()),
|
|
dataSecret.getBytes(), dataSecretIv.getBytes());
|
|
String dataStr = new String(plainText, StandardCharsets.UTF_8);
|
|
Map<String, String> resultMap = (Map<String, String>) JSON.parse(dataStr);
|
|
int succStat = Integer.parseInt(resultMap.get("SuccStat"));
|
|
if (succStat != 0) {
|
|
return resultMap.get("FailReason");
|
|
}
|
|
String token = resultMap.get("AccessToken");
|
|
int tokenAvailableTime = Integer.parseInt(resultMap.get("TokenAvailableTime")); // 凭证有效期,单位秒
|
|
// 存入缓存
|
|
redisCache.setCacheObject(redisKey, token, tokenAvailableTime, TimeUnit.SECONDS);
|
|
return token;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|