From 18da67511e915f84024ec44ff8ff8c559ce6905c Mon Sep 17 00:00:00 2001 From: Jack <46790855@qq.com> Date: Fri, 25 Apr 2025 00:19:48 +0800 Subject: [PATCH] =?UTF-8?q?shop=20=E9=A1=B9=E7=9B=AE=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=8B=89=E5=8D=A1=E6=8B=89=E7=9A=84=E4=B8=9A=E5=8A=A1=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../suisung/mall/common/utils/UploadUtil.java | 44 ++++++++++++++++--- mall-pay/pom.xml | 3 +- mall-shop/pom.xml | 8 +++- .../service/impl/LakalaPayServiceImpl.java | 37 +++++++++++----- 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/mall-common/src/main/java/com/suisung/mall/common/utils/UploadUtil.java b/mall-common/src/main/java/com/suisung/mall/common/utils/UploadUtil.java index 5fe43aad..408caef2 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/utils/UploadUtil.java +++ b/mall-common/src/main/java/com/suisung/mall/common/utils/UploadUtil.java @@ -3,14 +3,12 @@ package com.suisung.mall.common.utils; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import org.apache.tika.Tika; +import org.springframework.util.Base64Utils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; -import java.net.URLEncoder; +import java.net.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -25,14 +23,17 @@ public class UploadUtil { * token通过header传递的名称 */ public static final String TOKEN_HEADER_NAME = "Authorization"; - - // 除 text/* 外也需要设置输出编码的 content-type private final static List SET_CHARSET_CONTENT_TYPES = Arrays.asList( "application/json", "application/javascript" ); + public static void main(String[] args) { + String base64 = URLFileToBase64("http://gips3.baidu.com/it/u=1022347589,1106887837&fm=3028&app=3028&f=JPEG&fmt=auto?w=960&h=1280"); + System.out.println(base64); + } + /** * 查看文件, 支持断点续传 * @@ -269,11 +270,40 @@ public class UploadUtil { // inputStream 用完之后,记得关闭 return inputStream; } catch (IOException e) { - e.printStackTrace(); + log.error("发生 I/O 错误: ", e.getMessage()); return null; } } + /** + * 将URL地址转换为Base64字符串 + * + * @param fileUrl + * @return + */ + public static String URLFileToBase64(String fileUrl) { + try { + URL url = new URL(fileUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + + try (InputStream inputStream = connection.getInputStream()) { + byte[] buffer = new byte[inputStream.available()]; + inputStream.read(buffer); + return Base64Utils.encodeToString(buffer); + } finally { + connection.disconnect(); + } + } catch (MalformedURLException e) { + log.error("输入的 URL 格式不正确: {}", e.getMessage()); + } catch (IOException e) { + log.error("在连接或读取文件时发生 I/O 错误: {}", e.getMessage()); + } catch (Exception e) { + log.error("发生未知异常: {}", e.getMessage()); + } + return null; + } + /** * 下载图片到本地 * diff --git a/mall-pay/pom.xml b/mall-pay/pom.xml index c491acd1..44ace36e 100644 --- a/mall-pay/pom.xml +++ b/mall-pay/pom.xml @@ -1,5 +1,5 @@ - 4.0.0 @@ -161,6 +161,7 @@ org.springframework.boot spring-boot-maven-plugin + true diff --git a/mall-shop/pom.xml b/mall-shop/pom.xml index 9059009a..6f04abb6 100644 --- a/mall-shop/pom.xml +++ b/mall-shop/pom.xml @@ -286,13 +286,15 @@ ${project.basedir}/src/main/resources/lib/lkl-java-sdk-1.0.7.jar system + + com.github.javen205 IJPay-Core 2.8.0 compile - + @@ -363,6 +365,10 @@ org.springframework.boot spring-boot-maven-plugin + + + true + com.spotify diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaPayServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaPayServiceImpl.java index c57da239..3a53d3d7 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaPayServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaPayServiceImpl.java @@ -138,6 +138,9 @@ public class LakalaPayServiceImpl implements LakalaPayService { try { //3. 发送请求 String responseStr = LKLSDK.httpPost(v3LabsTransPreorderWechatReq); + if (StrUtil.isBlank(responseStr)) { + throw new ApiException(I18nUtil._("下单无响应!")); + } //4. 响应 return JSONUtil.parseObj(responseStr); @@ -210,7 +213,7 @@ public class LakalaPayServiceImpl implements LakalaPayService { String responseStr = LKLSDK.httpPost(v3LabsTransPreorderWechatReq); log.info("拉卡拉预下单响应数据:{}", responseStr); if (StrUtil.isBlank(responseStr)) { - return null; + throw new ApiException(I18nUtil._("下单无响应!")); } JSONObject lakalaRespJSON = JSONUtil.parseObj(responseStr); @@ -257,6 +260,9 @@ public class LakalaPayServiceImpl implements LakalaPayService { try { //3. 发送请求 String responseStr = LKLSDK.httpPost(v3LabsQueryTradequeryRequest); + if (StrUtil.isBlank(responseStr)) { + throw new ApiException(I18nUtil._("交易查询无响应!")); + } JSONObject lakalaRespJSON = JSONUtil.parseObj(responseStr); // || !lakalaRespJSON.getStr("code").equals("BBS00000") @@ -295,7 +301,7 @@ public class LakalaPayServiceImpl implements LakalaPayService { ShopStoreBase shopStoreBase = shopService.getLklMerchantNoAndTermNo(storeId); if (shopStoreBase == null || StrUtil.isBlank(shopStoreBase.getLkl_merchant_no()) || StrUtil.isBlank(shopStoreBase.getLkl_term_no())) { log.error("缺少参数,退款交易失败!"); - return null; + throw new ApiException(I18nUtil._("缺少必要参数!")); } //2. 装配数据 @@ -313,6 +319,9 @@ public class LakalaPayServiceImpl implements LakalaPayService { try { //3. 发送请求 String responseStr = LKLSDK.httpPost(req); + if (StrUtil.isBlank(responseStr)) { + throw new ApiException(I18nUtil._("退款交易失败!")); + } JSONObject lakalaRespJSON = JSONUtil.parseObj(responseStr); @@ -353,6 +362,9 @@ public class LakalaPayServiceImpl implements LakalaPayService { try { //3. 发送请求 String responseStr = LKLSDK.httpPost(req); + if (StrUtil.isBlank(responseStr)) { + throw new ApiException(I18nUtil._("账户余额查询无响应!")); + } JSONObject lakalaRespJSON = JSONUtil.parseObj(responseStr); @@ -393,8 +405,10 @@ public class LakalaPayServiceImpl implements LakalaPayService { String responseStr = LKLSDK.httpPost(req); JSONObject lakalaRespJSON = JSONUtil.parseObj(responseStr); - if (lakalaRespJSON == null || !lakalaRespJSON.getStr("retCode").equals(lklSuccessCode)) { - throw new ApiException(I18nUtil._(lakalaRespJSON.getStr("retMsg"))); + if (StrUtil.isBlank(responseStr) + || lakalaRespJSON == null + || !lakalaRespJSON.getStr("retCode").equals(lklSuccessCode)) { + throw new ApiException(I18nUtil._("文件上传无响应或失败!")); } return (JSONObject) lakalaRespJSON.get("respData"); @@ -448,10 +462,12 @@ public class LakalaPayServiceImpl implements LakalaPayService { String responseStr = LKLSDK.httpPost(req); JSONObject lakalaRespJSON = JSONUtil.parseObj(responseStr); - if (lakalaRespJSON == null || !lakalaRespJSON.getStr("retCode").equals(lklSuccessCode)) { - throw new ApiException(I18nUtil._(lakalaRespJSON.getStr("retMsg"))); -// responseStr="{'retCode':'000000','retMsg':'申请已受理,请等待审核结果','respData':{'version':'1.0','orderNo':'KFPT20230223181025407788734','orgCode':'1','applyId':681201215598657536}}"; -// lakalaRespJSON = JSONUtil.parseObj(responseStr); + if (StrUtil.isBlank(responseStr) + || lakalaRespJSON == null + || !lakalaRespJSON.getStr("retCode").equals(lklSuccessCode)) { + throw new ApiException(I18nUtil._("分账申请失败!")); + // 成功返回示例:{'retCode':'000000','retMsg':'申请已受理,请等待审核结果','respData':{'version':'1.0','orderNo':'KFPT20230223181025407788734','orgCode':'1','applyId':681201215598657536}} + } paramsJSON.set("apply_id", lakalaRespJSON.getByPath("respData.applyId")); @@ -479,7 +495,7 @@ public class LakalaPayServiceImpl implements LakalaPayService { */ @Override public JSONObject applyLedgerMerNotify(HttpServletRequest request) { - log.info("商户分账申请业务回调收到通知"); + log.info("商户分账申请业务异步通知开始"); // 验签 String authorization = request.getHeader("Authorization"); String requestBody = LakalaUtil.getBody(request); @@ -511,7 +527,8 @@ public class LakalaPayServiceImpl implements LakalaPayService { reqData.getStr("entrustFilePath"), reqData.getStr("auditStatus"), reqData.getStr("auditStatusText"), - reqData.getStr("remark")); + reqData.getStr("remark") + ); if (success) { respData.put("retCode", lklSuccessCode);