diff --git a/src/main/java/lingtao/net/util/HttpUtils.java b/src/main/java/lingtao/net/util/HttpUtils.java new file mode 100644 index 0000000..27c37d5 --- /dev/null +++ b/src/main/java/lingtao/net/util/HttpUtils.java @@ -0,0 +1,156 @@ +package lingtao.net.util; + +import okhttp3.*; + +import java.io.IOException; +import java.util.Map; + +public class HttpUtils { + + 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"); + + /** + * 通用HTTP GET请求方法 + * + * @param url 请求地址 + * @param headers 请求头映射 + * @return 响应结果字符串 + */ + public static String sendGetRequest(String url, Map headers) { + Request.Builder builder = new Request.Builder() + .header("Content-Type", "application/json") + .url(url); + // 添加请求头 + if (headers != null && !headers.isEmpty()) { + for (Map.Entry 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 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 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 formParams, Map headers) { + StringBuilder formBody = new StringBuilder(); + if (formParams != null && !formParams.isEmpty()) { + for (Map.Entry 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 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 formParams) { + return sendPostFormRequest(url, formParams, null); + } +} diff --git a/src/main/java/lingtao/net/util/HuaFangPriceUtil.java b/src/main/java/lingtao/net/util/HuaFangPriceUtil.java index 9e4a2af..8fe92e9 100644 --- a/src/main/java/lingtao/net/util/HuaFangPriceUtil.java +++ b/src/main/java/lingtao/net/util/HuaFangPriceUtil.java @@ -14,156 +14,11 @@ 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 headers) { - Request.Builder builder = new Request.Builder() - .header("Content-Type", "application/json") - .url(url); - // 添加请求头 - if (headers != null && !headers.isEmpty()) { - for (Map.Entry 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 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 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 formParams, Map headers) { - StringBuilder formBody = new StringBuilder(); - if (formParams != null && !formParams.isEmpty()) { - for (Map.Entry 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 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 formParams) { - return sendPostFormRequest(url, formParams, null); - } public static String getToken() { String token = ""; @@ -175,7 +30,7 @@ public class HuaFangPriceUtil { params.put("userName", userName); params.put("password", password); - String tokenString = sendPostRequest("http://ds.gw.chenghuiyin.com/ordering/api/User/GetToken", JSONObject.toJSONString(params)); + String tokenString = HttpUtils.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); @@ -191,7 +46,7 @@ public class HuaFangPriceUtil { public static HuaFangPriceResultVo getPrice(HuaFangPriceRequstVo params) { Map 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); + String resultText = HttpUtils.sendPostRequest("http://ds.quote.chenghuiyin.com/api/app/price/calc-price", JSONObject.toJSONString(params), heards); HuaFangPriceResultVo result = JSONObject.parseObject(resultText, HuaFangPriceResultVo.class); return result; } diff --git a/src/main/java/lingtao/net/util/PriceUtils.java b/src/main/java/lingtao/net/util/PriceUtils.java index 91f8a8b..0d60052 100644 --- a/src/main/java/lingtao/net/util/PriceUtils.java +++ b/src/main/java/lingtao/net/util/PriceUtils.java @@ -2651,27 +2651,27 @@ public class PriceUtils { double price = 0; if (length == 240 && width == 200 || length == 200 && width == 240) { if (kind.equals("春亚布")) { - price = num * count * 1.8; + price = num * count * 2; } else { - price = num * count * 2.5; + price = num * count * 3; } } else if (length == 200 && width == 140 || length == 140 && width == 200) { if (kind.equals("春亚布")) { - price = num * count * 1.5; + price = num * count * 1.8; } else { - price = num * count * 1.3; + price = num * count * 2.5; } } else { if (kind.equals("春亚布")) { price = num * count * 1.5; } else { - price = num * count * 1.8; + price = num * count * 2; } } pro = new Product(); pro.setCount(count); - pro.setPrice(Math.ceil(price) > 50 ? price : 50); + pro.setPrice(Math.max(Math.ceil(price), 30)); list.add(pro); return list; } diff --git a/src/main/webapp/views/product/notePaper.jsp b/src/main/webapp/views/product/notePaper.jsp index ca203bb..dfdebb0 100644 --- a/src/main/webapp/views/product/notePaper.jsp +++ b/src/main/webapp/views/product/notePaper.jsp @@ -408,8 +408,8 @@ layer.msg("联单尺寸不能小于10*7 cm",{offset:['300px','300px']},function(){}); return false; } - if ((size.split("*")[0] > 42 || size.split("*")[1] > 28.5) && (size.split("*")[0] > 28.5 || size.split("*")[1] > 42)) { - layer.msg("联单尺寸超过42*28.5 cm请单独报价",{offset:['300px','300px']},function(){}); + if ((size.split("*")[0] > 21 || size.split("*")[1] > 28.5) && (size.split("*")[0] > 28.5 || size.split("*")[1] > 21)) { + layer.msg("彩色信纸尺寸不能超过21*28.5 cm",{offset:['300px','300px']},function(){}); return false; } } diff --git a/src/main/webapp/views/product/poster.jsp b/src/main/webapp/views/product/poster.jsp index 17e8a80..6503c03 100644 --- a/src/main/webapp/views/product/poster.jsp +++ b/src/main/webapp/views/product/poster.jsp @@ -35,7 +35,7 @@ - +