新增redis
This commit is contained in:
@@ -177,5 +177,7 @@ public class Product {
|
||||
private String ui_menu_size;
|
||||
private String address;
|
||||
|
||||
private Double lengthVU;
|
||||
private Double widthUV;
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import lingtao.net.bean.SysUser;
|
||||
import lingtao.net.service.PriceService;
|
||||
import lingtao.net.service.ProductService;
|
||||
import lingtao.net.service.QuoteLogService;
|
||||
import lingtao.net.util.HuaFangPriceUtil;
|
||||
import lingtao.net.vo.PricingListVo;
|
||||
import lingtao.net.vo.ProductVo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package lingtao.net.util;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class RedisUtil implements ApplicationContextAware {
|
||||
|
||||
private static RedisTemplate redisTemplate;
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
applicationContext = context;
|
||||
redisTemplate = (RedisTemplate) applicationContext.getBean("redisTemplate");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public static void set(String key, Object value) {
|
||||
if (redisTemplate != null) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存(带过期时间)
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param expireTime 过期时间(秒)
|
||||
*/
|
||||
public static void set(String key, Object value, long expireTime) {
|
||||
if (redisTemplate != null) {
|
||||
redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Object get(String key) {
|
||||
if (redisTemplate != null) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
public static void delete(String key) {
|
||||
if (redisTemplate != null) {
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查键是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @return 是否存在
|
||||
*/
|
||||
public static boolean exists(String key) {
|
||||
if (redisTemplate != null) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置过期时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param expireTime 过期时间(秒)
|
||||
* @return 是否成功
|
||||
*/
|
||||
public static boolean expire(String key, long expireTime) {
|
||||
if (redisTemplate != null) {
|
||||
return redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取剩余过期时间
|
||||
*
|
||||
* @param key 键
|
||||
* @return 剩余过期时间(秒)
|
||||
*/
|
||||
public static long getExpire(String key) {
|
||||
if (redisTemplate != null) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package lingtao.net.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class HuaFangPriceProcessItemsVo {
|
||||
|
||||
private String processName;
|
||||
private String processValue;
|
||||
private List<Double> sizes;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package lingtao.net.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class HuaFangPriceRequstVo {
|
||||
|
||||
private Integer styleNum;
|
||||
|
||||
private List<Double> sizes;
|
||||
|
||||
private String productPrintFace;
|
||||
private String productPrintColor;
|
||||
private String productPaperCode;
|
||||
private String productPaperName;
|
||||
private String productName;
|
||||
private Integer num;
|
||||
|
||||
private List<HuaFangPriceProcessItemsVo> calcProcess;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package lingtao.net.vo;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class HuaFangPriceResultVo {
|
||||
private String id;
|
||||
|
||||
private Double amount;
|
||||
@JSONField(format = "yyyy-MM-dd'T'HH:mm:ssXXX")
|
||||
private LocalDateTime deliveryTime;
|
||||
|
||||
private Double totalWeight;
|
||||
|
||||
private Double deliveryAmount;
|
||||
|
||||
private Double productAmount;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package lingtao.net.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class HuaFangTokenVo {
|
||||
|
||||
private String accessToken;
|
||||
private Integer expiresIn;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user