From 4fabfe4dbd2eb28d07a4350597d314958cc1c281 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期四, 09 十月 2025 11:16:43 +0800
Subject: [PATCH] 优化

---
 server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java |  373 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 337 insertions(+), 36 deletions(-)

diff --git a/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java b/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java
index 4c42c9b..3800317 100644
--- a/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java
+++ b/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java
@@ -1,28 +1,41 @@
 package com.doumee.core.wx;
 
+import com.alibaba.fastjson.JSONObject;
 import com.doumee.biz.system.SystemDictDataBiz;
 import com.doumee.core.constants.Constants;
 import com.doumee.core.constants.ResponseStatus;
 import com.doumee.core.exception.BusinessException;
+import com.doumee.core.utils.DateUtil;
+import com.doumee.core.utils.HttpsUtil;
 import com.doumee.core.utils.ID;
-import com.doumee.dao.business.ActionLogMapper;
-import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
-import com.github.binarywang.wxpay.bean.request.WxPayDownloadBillRequest;
-import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
-import com.github.binarywang.wxpay.bean.result.WxPayBillResult;
-import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
-import com.github.binarywang.wxpay.exception.WxPayException;
+import com.doumee.core.utils.ImageDesignerUtil;
+import com.doumee.dao.business.RefundMapper;
+import com.doumee.dao.business.TransactionsMapper;
+import com.doumee.dao.business.model.Bikes;
+import com.doumee.dao.business.model.Locks;
+import com.doumee.dao.business.model.Refund;
+import com.doumee.dao.business.model.Transactions;
+import com.doumee.dao.business.web.request.RefundDTO;
+import com.doumee.dao.system.model.SystemDictData;
+import com.wechat.pay.java.service.refund.model.AmountReq;
+import com.wechat.pay.java.service.refund.model.CreateRequest;
+import com.wechat.pay.java.service.refund.model.QueryByOutRefundNoRequest;
 import lombok.extern.slf4j.Slf4j;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.yaml.snakeyaml.scanner.Constant;
 
+import java.io.*;
 import java.math.BigDecimal;
-import java.security.KeyFactory;
-import java.security.Signature;
-import java.security.spec.PKCS8EncodedKeySpec;
-import java.util.Base64;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
 
 /**
  * 寰俊灏忕▼搴�-鍏叡鏂规硶
@@ -31,39 +44,327 @@
 @Slf4j
 public class WxMiniUtilService {
 
+    @Autowired
+    private RefundMapper refundMapper;
+
+    @Autowired
+    private TransactionsMapper transactionsMapper;
 
     @Autowired
     private SystemDictDataBiz systemDictDataBiz;
+    @Autowired
+    private WxPayProperties wxPayProperties;
 
-    /**
-     * 璁㈠崟寰俊閫�娆�
-     * orderNo:鍟嗘埛璁㈠崟鍙�
-     * totalPrice锛氳鍗曟�婚噾棰�
-     * refundPrice锛涢��娆鹃噾棰�
-     */
-    @Transactional(rollbackFor = Exception.class)
-    public static String wxRefund(String orderNo, BigDecimal totalPrice, BigDecimal refundPrice) {
+    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
+    public boolean wxRefund(RefundDTO refundDTO) {
+        // 鍙戦�侀��娆捐姹�
+        Refund refund = new Refund();
+        refund.setId(Constants.getUUID());
+        refund.setCreateDate(new Date());
+        refund.setMemberId(refundDTO.getMemberId());
+        refund.setMoney(refundDTO.getRefundAmount());
+//        refund.setOnlineOrderid(refNum);
+        refund.setPayWay(Constants.ZERO);
+        refund.setStatus(Constants.ZERO);
+        refund.setDoneDate(new Date());
+        refund.setCreator(refundDTO.getCreator());
+        refund.setType(refundDTO.getType());
+        refund.setObjId(refundDTO.getOrderId());
+        refund.setReason(refundDTO.getReason());
+        refund.setCanBalance(refundDTO.getCanBalance());
+        refundMapper.insert(refund);
+
+        CreateRequest request = new CreateRequest();
+        request.setOutTradeNo(refundDTO.getOrderId());
+        request.setOutRefundNo(refund.getId());
+        request.setSubMchid(WxMiniConfig.wxProperties.getSubMchId());
+        request.setNotifyUrl(WxMiniConfig.wxProperties.getRefundNotifyUrl());
+        AmountReq amountReq = new AmountReq();
+        amountReq.setTotal(refundDTO.getTotalAmount().longValue());
+        amountReq.setRefund(refundDTO.getRefundAmount().longValue());
+        amountReq.setCurrency("CNY");
+        request.setAmount(amountReq);
         try {
-            // 鍙戦�侀��娆捐姹�
-            String refNum = ID.nextGUID();
-            WxPayRefundRequest request = new WxPayRefundRequest();
-            request.setOutTradeNo(orderNo);
-            request.setOutRefundNo(refNum);
-           // request.setTotalFee(2);
-          //  request.setRefundFee(1);
-            request.setTotalFee(BaseWxPayRequest.yuanToFen(totalPrice.toString()));
-            request.setRefundFee(BaseWxPayRequest.yuanToFen(refundPrice.toString()));
-            WxPayRefundResult response = WxMiniConfig.wxPayService.refund(request);
-            if ("SUCCESS".equals(response.getReturnCode()) && "SUCCESS".equals(response.getResultCode())) {
-                return refNum;
-            } else {
-                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),response.getErrCode() + response.getErrCodeDes());
+            log.error("=============="+JSONObject.toJSONString(request));
+            com.wechat.pay.java.service.refund.model.Refund response = WxMiniConfig.refundService.create(request);
+            log.error("=============="+JSONObject.toJSONString(response));
+            if ("SUCCESS".equals(response.getStatus().name())
+                    || "PROCESSING".equals(response.getStatus().name()) ) {
+                return  true;
+            }else{
+                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"瀵逛笉璧凤紝閫�娆剧敵璇峰け璐ュ摝锛�");
             }
-        } catch (WxPayException e) {
+        }catch (Exception e){
             e.printStackTrace();
+            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"瀵逛笉璧凤紝閫�娆剧敵璇峰け璐ワ紒");
         }
-        throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"閫�娆惧彂鐢熷紓甯歌鑱旂郴绠$悊鍛�");
+
+    }
+    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
+    public com.wechat.pay.java.service.refund.model.Refund isSuucessRefund(String outTradeNo) {
+        // 鍙戦�侀��娆捐姹�
+
+        QueryByOutRefundNoRequest request = new QueryByOutRefundNoRequest();
+        request.setOutRefundNo(outTradeNo);
+        request.setSubMchid(WxMiniConfig.wxProperties.getSubMchId());
+        try {
+            log.error("=============="+JSONObject.toJSONString(request));
+            com.wechat.pay.java.service.refund.model.Refund response = WxMiniConfig.refundService.queryByOutRefundNo(request);
+            log.error("=============="+JSONObject.toJSONString(response));
+            if ("SUCCESS".equals(response.getStatus().name())
+                    || "PROCESSING".equals(response.getStatus().name()) ) {
+                return  response;
+            }
+        }catch (Exception e){
+            e.printStackTrace();
+
+        }
+        return  null;
+    }
+    public Refund wxRefundOld(RefundDTO refundDTO) {
+        // 鍙戦�侀��娆捐姹�
+        String refNum = ID.nextGUID();
+        CreateRequest request = new CreateRequest();
+        request.setOutTradeNo(refundDTO.getOrderId());
+        request.setOutRefundNo(refNum);
+        request.setSubMchid(WxMiniConfig.wxProperties.getSubMchId());
+        request.setNotifyUrl(WxMiniConfig.wxProperties.getRefundNotifyUrl());
+//
+        AmountReq amountReq = new AmountReq();
+        amountReq.setTotal(refundDTO.getTotalAmount().longValue());
+        amountReq.setRefund(refundDTO.getRefundAmount().longValue());
+        amountReq.setCurrency("CNY");
+        request.setAmount(amountReq);
+        System.out.println("瀹為檯鎬婚噾棰�" + refundDTO.getTotalAmount());
+        System.out.println("瀹為檯閫�娆鹃噾棰�" + refundDTO.getRefundAmount());
+//            request.setTotalFee(1);
+//            request.setRefundFee(1);
+        try {
+            com.wechat.pay.java.service.refund.model.Refund response = WxMiniConfig.refundService.create(request);
+            if ("SUCCESS".equals(response.getStatus().name()) ) {
+                //瀛樺偍閫�娆捐褰� 涓� 娴佹按璁板綍
+                Refund refund = new Refund();
+                refund.setId(Constants.getUUID());
+                refund.setCreateDate(new Date());
+                refund.setMemberId(refundDTO.getMemberId());
+                refund.setMoney(refundDTO.getRefundAmount());
+                refund.setOnlineOrderid(refNum);
+                refund.setPayWay(Constants.ZERO);
+                refund.setStatus(Constants.TWO);
+                refund.setDoneDate(new Date());
+                refund.setCreator(refundDTO.getCreator());
+                refund.setType(refundDTO.getType());
+                refund.setObjId(refundDTO.getOrderId());
+                refund.setReason(refundDTO.getReason());
+                refund.setCanBalance(refundDTO.getCanBalance());
+                refundMapper.insert(refund);
+                //瀛樺偍浜ゆ槗娴佹按琛�
+                Transactions transactions = new Transactions();
+                transactions.setId(Constants.getUUID());
+                transactions.setMemberId(refundDTO.getMemberId());
+                transactions.setCreateDate(new Date());
+                transactions.setIsdeleted(Constants.ZERO);
+                transactions.setOrderId(refundDTO.getOrderId());
+                transactions.setMoney(refundDTO.getRefundAmount());
+                transactions.setPreOrderid(refundDTO.getOrderId());
+                transactions.setOnlineOrderid(refNum);
+                transactions.setDoneDate(new Date());
+                if(refund.getType().equals(Constants.REFUND_TYPE.PLAT_AUTO.getKey())||refund.getType().equals(Constants.REFUND_TYPE.PLAT_FORCE.getKey())){
+                    //骞冲彴鑷姩閫�娆� 鎴� 寮哄埗閫�娆�
+                    transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey());
+                    transactions.setTitle(Constants.REFUND_TYPE.PLAT_AUTO.getInfo());
+                    transactions.setContent(Constants.REFUND_TYPE.PLAT_AUTO.getInfo());
+                }else if(refund.getType().equals(Constants.REFUND_TYPE.NORMAL.getKey())){
+                    //鐢ㄦ埛涓诲姩閫�娆�
+                    transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey());
+                    transactions.setTitle(Constants.REFUND_TYPE.NORMAL.getInfo());
+                    transactions.setContent(Constants.REFUND_TYPE.NORMAL.getInfo());
+                }else if(refund.getType().equals(Constants.REFUND_TYPE.BACK.getKey())){
+                    //缁撶畻鍚庨��娆�
+                    transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey());
+                    transactions.setTitle(Constants.REFUND_TYPE.BACK.getInfo());
+                    transactions.setContent(Constants.REFUND_TYPE.BACK.getInfo());
+                }
+                transactions.setBalance(BigDecimal.ZERO);
+                transactions.setObjId(refund.getId());
+                transactions.setObjType(Constants.ONE);
+                transactionsMapper.insert(transactions);
+                return refund;
+            } else{
+                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"瀵逛笉璧凤紝閫�娆惧け璐ワ紒");
+            }
+        }catch (BusinessException e){
+            throw e;
+        }catch (Exception e){
+            e.printStackTrace();
+            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"瀵逛笉璧凤紝閫�娆惧け璐ワ紒");
+        }
+
+
     }
 
+    public static void main(String[] args) throws IOException {
+      /* InputStream inputStream = HttpsUtil.postJson("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=75_W8lx5-bp9WEo69pQ6hvN70fJJJCNSRby6Wi6z_R9G7Sd5GMdY7ZT8Zq3PrKWrFBq92utfgqu3P8iw1NOvJNfyZssEYCy6k94Wch-GQNXcarSV65gowLk-SA9bncWTJbAGAHEU", "{\"check_path\":false,\"is_hyaline\":false,\"page\":\"pages/index/index\",\"scene\":\"1993/90\",\"env_version\":\"trial\"}");
+        File file = new File("D://test.png");
+
+        //瀹氫箟涓�涓敤浜庤鍙朓nputStream鏁版嵁鐨刡yte鏁扮粍
+        byte[] buffer = new byte[1024];
+
+//杈撳嚭娴侊紝鐢ㄤ簬鍐欏叆鏂囦欢
+        FileOutputStream outputStream = new FileOutputStream(file);
+
+//寰幆璇诲彇鏁版嵁骞跺皢鍏跺啓鍏ユ枃浠朵腑
+        int length;
+        while ((length = inputStream.read(buffer)) > 0) {
+            outputStream.write(buffer, 0, length);
+        }
+
+//鍏抽棴InputStream鍜孫utputStream
+        inputStream.close();
+        outputStream.close();*/
+        Map<String,Object> body = new HashMap<>();
+        // 鍦烘櫙鐮侊紝涓庡墠绔害瀹氾紝鏈�缁堟槸闇�瑕佸墠绔В鏋�
+        body.put("scene",   "1992/41" );
+        // 姝e紡鐗堜负 "release"锛屼綋楠岀増涓� "trial"锛屽紑鍙戠増涓� "develop"銆傞粯璁ゆ槸姝e紡鐗堛��
+//        body.put("env_version", "release");
+        body.put("env_version", "trial");
+        // 閫忔槑锛屾牴鎹綘鐨勫満鏅嚜琛岃缃産ody鍙傛暟
+        body.put("is_hyaline",false);
+        body.put("check_path", false);
+        body.put("page","pages/index/index");
+        OkHttpClient client = new OkHttpClient().newBuilder().build();
+        okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
+        log.info("=========================================="+JSONObject.toJSONString(body));
+        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, JSONObject.toJSONString(body));
+        Request request = new Request.Builder().url("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=75_MppLyM-Fy1rWPbJE8ccXfy90Afg_3wdENqKBEshglyyARP3egjvsti3agoaZJ0UVKHh_wwuIDcbu41ptPKleUcaD1vDRi4BrH8oN8TEHPTUCb1Kcp6ZoMJNNWScZKChAIAHSK").method("POST", requestBody).build();
+        try {
+            Response response = client.newCall(request).execute();
+            if (response.isSuccessful()) {
+                InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
+                String nowDate = DateUtil.getNowShortDate();
+                File file = new File("D://test1.png");
+                FileUtils.copyInputStreamToFile(inputStream,file);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 鐢熸垚灏忕▼搴忕爜
+     * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
+     * @return
+     */
+    public void generateWXMiniCode(Locks locks,String token,String prePath,String path){
+
+        if(Objects.isNull(token)){
+            return;
+        }
+         String url =  "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+ token;
+        String release =   systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.MINI_PROGRAMME_REALEASE).getCode();
+        //鐢熸垚鍥剧墖涓婁紶OSS
+        Map<String,Object> body = new HashMap<>();
+        // 鍦烘櫙鐮侊紝涓庡墠绔害瀹氾紝鏈�缁堟槸闇�瑕佸墠绔В鏋�
+        body.put("scene", locks.getSiteId() + "/" +locks.getCode() );
+        // 姝e紡鐗堜负 "release"锛屼綋楠岀増涓� "trial"锛屽紑鍙戠増涓� "develop"銆傞粯璁ゆ槸姝e紡鐗堛��
+//        body.put("env_version", "release");
+        body.put("env_version", StringUtils.defaultString(release, "release"));
+        // 閫忔槑锛屾牴鎹綘鐨勫満鏅嚜琛岃缃産ody鍙傛暟
+        body.put("is_hyaline", false);
+        body.put("check_path", false);
+        body.put("width", 290);
+        body.put("page","pages/index/index");
+        log.info("=========================================="+url+"\n"+JSONObject.toJSONString(body));
+        try {
+            InputStream inputStream  =HttpsUtil.postJson(url,JSONObject.toJSONString(body));;
+            if (inputStream !=null) {
+                String nowDate = DateUtil.getNowShortDate();
+                String name =locks.getSiteId()+"-"+locks.getCode()+"-"+nowDate+".png";
+                String fileName =prePath+path+"/"+name;
+//                File file = new File(prePath+path,locks.getSiteId()+"-"+locks.getCode()+"-"+nowDate+".png");
+//                FileUtils.copyInputStreamToFile(inputStream,file);
+                if(ImageDesignerUtil.graphicsGenerationIs(locks.getSiteId()+"/"+locks.getCode(),inputStream,fileName)){
+                    locks.setInfo(name);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+    public void generateEbikeWXMiniCode(Bikes bikes, String token, String prePath, String path){
+        if(Objects.isNull(token)){
+            return;
+        }
+         String url =  "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+ token;
+        String release =   systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.MINI_PROGRAMME_REALEASE).getCode();
+        //鐢熸垚鍥剧墖涓婁紶OSS
+        Map<String,Object> body = new HashMap<>();
+        // 鍦烘櫙鐮侊紝涓庡墠绔害瀹氾紝鏈�缁堟槸闇�瑕佸墠绔В鏋�
+        body.put("scene",  Constants.EBIKE_PREFIX +bikes.getCode() );
+        // 姝e紡鐗堜负 "release"锛屼綋楠岀増涓� "trial"锛屽紑鍙戠増涓� "develop"銆傞粯璁ゆ槸姝e紡鐗堛��
+//        body.put("env_version", "release");
+        body.put("env_version", StringUtils.defaultString(release, "release"));
+        // 閫忔槑锛屾牴鎹綘鐨勫満鏅嚜琛岃缃産ody鍙傛暟
+        body.put("is_hyaline", false);
+        body.put("check_path", false);
+        body.put("width", 290);
+        body.put("page","pages/index/index");
+        log.info("=========================================="+url+"\n"+JSONObject.toJSONString(body));
+        try {
+            InputStream inputStream  =HttpsUtil.postJson(url,JSONObject.toJSONString(body));;
+            if (inputStream !=null) {
+                String nowDate = DateUtil.getNowShortDate();
+                String name =Constants.EBIKE_PREFIX+bikes.getCode()+"-"+nowDate+".png";
+                String fileName =prePath+path+"/"+name;
+//                File file = new File(prePath+path,locks.getSiteId()+"-"+locks.getCode()+"-"+nowDate+".png");
+//                FileUtils.copyInputStreamToFile(inputStream,file);
+                if(ImageDesignerUtil.graphicsGenerationIs(bikes.getCode(),inputStream,fileName)){
+                    bikes.setImgurl(name);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+    /**
+     * 鐢熸垚灏忕▼搴忕爜
+     * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
+     * @return
+     */
+    public void generateWXMiniCodeOld(Locks locks,SystemDictData systemDictData,String prePath,String path){
+
+        if(Objects.isNull(systemDictData)){
+            return;
+        }
+      String release =   systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.MINI_PROGRAMME_REALEASE).getCode();
+        //鐢熸垚鍥剧墖涓婁紶OSS
+        Map<String,Object> body = new HashMap<>();
+        // 鍦烘櫙鐮侊紝涓庡墠绔害瀹氾紝鏈�缁堟槸闇�瑕佸墠绔В鏋�
+        body.put("scene", locks.getSiteId() + "/" +locks.getCode() );
+        // 姝e紡鐗堜负 "release"锛屼綋楠岀増涓� "trial"锛屽紑鍙戠増涓� "develop"銆傞粯璁ゆ槸姝e紡鐗堛��
+//        body.put("env_version", "release");
+        body.put("env_version", StringUtils.defaultString(release, "release"));
+        // 閫忔槑锛屾牴鎹綘鐨勫満鏅嚜琛岃缃産ody鍙傛暟
+        body.put("is_hyaline", Boolean.FALSE.toString());
+        body.put("check_path", Boolean.FALSE.toString());
+        body.put("page","pages/index/index");
+        OkHttpClient client = new OkHttpClient().newBuilder().build();
+        okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
+        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, JSONObject.toJSONString(body));
+        log.info("=========================================="+JSONObject.toJSONString(body));
+        Request request = new Request.Builder().url("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+systemDictData.getCode()).method("POST", requestBody).build();
+        try {
+            Response response = client.newCall(request).execute();
+            if (response.isSuccessful()) {
+                InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
+                String nowDate = DateUtil.getNowShortDate();
+                File file = new File(prePath+path,locks.getSiteId()+"-"+locks.getCode()+"-"+nowDate+".png");
+                FileUtils.copyInputStreamToFile(inputStream,file);
+                locks.setInfo(locks.getSiteId()+"-"+locks.getCode()+"-"+nowDate+".png");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
 }

--
Gitblit v1.9.3