新增redis

This commit is contained in:
2026-04-20 23:32:52 +08:00
parent fea143f0e1
commit 5a713e8160
13 changed files with 1689 additions and 514 deletions
@@ -0,0 +1,198 @@
package lingtao.net.util;
import com.alibaba.fastjson.JSONObject;
import lingtao.net.vo.HuaFangPriceRequstVo;
import lingtao.net.vo.HuaFangPriceResultVo;
import lingtao.net.vo.HuaFangTokenVo;
import okhttp3.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
public class HuaFangPriceUtil {
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final MediaType FORM = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
private static final String userName = "001008";
private static final String password = "lt666888";
/**
* 通用HTTP GET请求方法
*
* @param url 请求地址
* @param headers 请求头映射
* @return 响应结果字符串
*/
public static String sendGetRequest(String url, Map<String, String> headers) {
Request.Builder builder = new Request.Builder()
.header("Content-Type", "application/json")
.url(url);
// 添加请求头
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
Request request = builder.build();
try (Response response = HTTP_CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("请求异常: " + response.code());
}
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 通用HTTP GET请求方法(无请求头)
*
* @param url 请求地址
* @return 响应结果字符串
*/
public static String sendGetRequest(String url) {
return sendGetRequest(url, null);
}
/**
* 通用HTTP POST请求方法(JSON参数)
*
* @param url 请求地址
* @param jsonParam JSON参数
* @param headers 请求头映射
* @return 响应结果字符串
*/
public static String sendPostRequest(String url, String jsonParam, Map<String, String> headers) {
RequestBody body = RequestBody.create(JSON, jsonParam);
Request.Builder builder = new Request.Builder()
.header("Content-Type", "application/json")
.url(url)
.post(body);
// 添加请求头
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
Request request = builder.build();
try (Response response = HTTP_CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("请求异常: " + response.code());
}
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 通用HTTP POST请求方法(JSON参数,无请求头)
*
* @param url 请求地址
* @param jsonParam JSON参数
* @return 响应结果字符串
*/
public static String sendPostRequest(String url, String jsonParam) {
return sendPostRequest(url, jsonParam, null);
}
/**
* 通用HTTP POST请求方法(表单参数)
*
* @param url 请求地址
* @param formParams 表单参数映射
* @param headers 请求头映射
* @return 响应结果字符串
*/
public static String sendPostFormRequest(String url, Map<String, String> formParams, Map<String, String> headers) {
StringBuilder formBody = new StringBuilder();
if (formParams != null && !formParams.isEmpty()) {
for (Map.Entry<String, String> entry : formParams.entrySet()) {
if (formBody.length() > 0) {
formBody.append("&");
}
formBody.append(entry.getKey()).append("=").append(entry.getValue());
}
}
RequestBody body = RequestBody.create(FORM, formBody.toString());
Request.Builder builder = new Request.Builder()
.header("Content-Type", "application/json")
.url(url)
.post(body);
// 添加请求头
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
Request request = builder.build();
try (Response response = HTTP_CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("请求异常: " + response.code());
}
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 通用HTTP POST请求方法(表单参数,无请求头)
*
* @param url 请求地址
* @param formParams 表单参数映射
* @return 响应结果字符串
*/
public static String sendPostFormRequest(String url, Map<String, String> formParams) {
return sendPostFormRequest(url, formParams, null);
}
public static String getToken() {
String token = "";
LocalDateTime currentTime = LocalDateTime.now();
String templateName = "HUAFANG_TOKEN";
if (!RedisUtil.exists(templateName)) {
Map<String, String> params = new HashMap<>();
params.put("userName", userName);
params.put("password", password);
String tokenString = sendPostRequest("http://ds.gw.chenghuiyin.com/ordering/api/User/GetToken", JSONObject.toJSONString(params));
System.out.println("获取token返回:" + tokenString);
HuaFangTokenVo huaFangTokenVo = JSONObject.parseObject(tokenString, HuaFangTokenVo.class);
RedisUtil.set(templateName, huaFangTokenVo.getAccessToken(), huaFangTokenVo.getExpiresIn() - 50);
}
Object tokenObj = RedisUtil.get(templateName);
if (tokenObj != null) {
token = tokenObj.toString();
}
System.out.println("获取token:" + token);
return token;
}
public static HuaFangPriceResultVo getPrice(HuaFangPriceRequstVo params) {
Map<String, String> heards = new HashMap<>();
heards.put("Authorization", "Bearer " + getToken());
String resultText = sendPostRequest("http://ds.quote.chenghuiyin.com/api/app/price/calc-price", JSONObject.toJSONString(params), heards);
HuaFangPriceResultVo result = JSONObject.parseObject(resultText, HuaFangPriceResultVo.class);
return result;
}
}