mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-22 03:55:17 +08:00
新增 路通云停系统获取令牌接口
This commit is contained in:
21
jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/lutongyunting/service/LTYTService.java
vendored
Normal file
21
jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/lutongyunting/service/LTYTService.java
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.jsowell.thirdparty.lutongyunting.service;
|
||||
|
||||
import com.jsowell.pile.dto.lutongyunting.GetTokenDTO;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author Lemon
|
||||
* @Date 2023/8/23 11:07
|
||||
*/
|
||||
public interface LTYTService {
|
||||
|
||||
/**
|
||||
* 获取平台令牌
|
||||
* @param dto
|
||||
* @return
|
||||
*/
|
||||
String getToken(GetTokenDTO dto);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.jsowell.thirdparty.lutongyunting.service.impl;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.jsowell.common.constant.CacheConstants;
|
||||
import com.jsowell.common.core.redis.RedisCache;
|
||||
import com.jsowell.common.util.DateUtils;
|
||||
import com.jsowell.common.util.StringUtils;
|
||||
import com.jsowell.common.util.sign.MD5Util;
|
||||
import com.jsowell.pile.dto.lutongyunting.GetTokenDTO;
|
||||
import com.jsowell.thirdparty.lutongyunting.service.LTYTService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author Lemon
|
||||
* @Date 2023/8/23 11:19
|
||||
*/
|
||||
@Service
|
||||
public class LTYTServiceImpl implements LTYTService {
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
// private static final String APP_ID = "I2qa3hdr116100dc"; // I2qa3hdr116100dc I2qa3jfnd96f267c
|
||||
|
||||
// private static final String SECRET_KEY = "2qa3hdr13754a8e"; // 2qa3hdr13754a8e 2qa3jfndb37926d
|
||||
|
||||
private static final String BASE_URL = "https://entry-openapi.sn6.co/api/";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(LTYTServiceImpl.class);
|
||||
|
||||
/**
|
||||
* 路通云停系统获取令牌
|
||||
* @param dto
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getToken(GetTokenDTO dto) {
|
||||
// 检查缓存中是否有token
|
||||
String key = CacheConstants.LTYT_TOKEN_BY_APPID + dto.getAppId();
|
||||
String token = redisCache.getCacheObject(key);
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
return token;
|
||||
}
|
||||
// String appId = "I2qa3hdr116100dc";
|
||||
String accTime = DateUtils.dateTimeNow(DateUtils.YYYYMMDDHHMMSS);
|
||||
|
||||
String url = BASE_URL + "getApiToken";
|
||||
|
||||
// 封装参数
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("appId", dto.getAppId());
|
||||
jsonObject.put("accTime", accTime);
|
||||
jsonObject.put("version", "1.0");
|
||||
|
||||
// Map<String, Object> map = new LinkedHashMap<>();
|
||||
// map.put("appId", dto.getAppId());
|
||||
// map.put("accTime", accTime);
|
||||
// map.put("version", "1.0");
|
||||
|
||||
// 获取sign签名
|
||||
String sign = getSign(jsonObject, dto.getSecretKey());
|
||||
// map.put("sign", sign);
|
||||
|
||||
jsonObject.put("sign", sign);
|
||||
|
||||
// 发送请求
|
||||
String result = HttpUtil.post(url, JSON.toJSONString(jsonObject));
|
||||
log.info("路通云停系统获取令牌 result:{}", result);
|
||||
JSONObject resultJson = JSONObject.parseObject(result);
|
||||
Integer code = resultJson.getInteger("code");
|
||||
if (code == 200) {
|
||||
token = resultJson.getJSONObject("data").getString("token");
|
||||
Integer expiredTime = resultJson.getJSONObject("data").getInteger("expiry");
|
||||
// 存入缓存
|
||||
redisCache.setCacheObject(key, token, expiredTime, TimeUnit.HOURS);
|
||||
return redisCache.getCacheObject(key);
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// String accTime = DateUtils.dateTimeNow(DateUtils.YYYYMMDDHHMMSS);
|
||||
// Map<String, Object> map = new LinkedMap<>();
|
||||
// map.put("appId", "appId");
|
||||
// map.put("accTime", accTime);
|
||||
// map.put("version", "version");
|
||||
// System.out.println(map);
|
||||
// // 排序
|
||||
// TreeMap<String, Object> params = new TreeMap<String, Object>(map);
|
||||
//
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// for (String key : params.keySet()) {
|
||||
// sb.append(key).append("=").append(params.get(key)).append("&");
|
||||
// }
|
||||
// sb.deleteCharAt(sb.length() - 1);
|
||||
//
|
||||
// String dataStr = sb + "secretKey";
|
||||
// String sign = MD5Util.MD5Encode(dataStr).toLowerCase(Locale.ROOT);
|
||||
// System.out.println(sign);
|
||||
}
|
||||
|
||||
private static String getSign(JSONObject jsonObject, String secretKey) {
|
||||
// String secretKey = "";
|
||||
TreeMap<String, Object> params = new TreeMap<>(jsonObject);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String key : params.keySet()) {
|
||||
sb.append(key).append("=").append(params.get(key)).append("&");
|
||||
}
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
sb.append(secretKey);
|
||||
return MD5Util.MD5Encode(sb.toString()).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user