From d83e4be3fe4e5a3c52d70ab36d64584e966c1800 Mon Sep 17 00:00:00 2001
From: Administrator <zhubaomin>
Date: 星期一, 17 六月 2024 09:02:09 +0800
Subject: [PATCH] 2024-06-17 朱宝民 优化微信支付代码

---
 pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java |   65 ++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java
index 2d237fa..4314bed 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java
@@ -18,6 +18,7 @@
 import com.dy.pipIrrSell.virtualCard.enums.LastOperateENUM;
 import com.dy.pipIrrSell.virtualCard.enums.RefundItemStateENUM;
 import com.dy.pipIrrSell.wallet.enums.RefundStatusENUM;
+import com.dy.pipIrrSell.wechatpay.dto.Code2Session;
 import com.dy.pipIrrSell.wechatpay.dto.DtoOrder;
 import com.dy.pipIrrSell.wechatpay.dto.NotifyResource;
 import com.dy.pipIrrSell.wechatpay.dto.OrderNotify;
@@ -73,6 +74,7 @@
 
     private final String privateCertFileName = PayInfo.privateCertFileName;
     private final String appid = PayInfo.appid;
+    private final String secret = PayInfo.secret;
     private final String mchid = PayInfo.mchid;
     private final String schema = PayInfo.schema;
     private final String signType = PayInfo.signType;
@@ -83,6 +85,68 @@
 
     // 骞冲彴璇佷功鍏挜
     private final Map CERTIFICATE_MAP = new HashMap();
+
+    /**
+     * 鐧诲綍鍑瘉鏍¢獙锛屽啘鎴风粦瀹氳处鍙烽�昏緫鍖呭惈鐧诲綍鍑瘉鏍¢獙锛屾鎺ュ彛浣滃簾
+     * @param code2Session 鐧诲綍鍑瘉鏍¢獙浼犲叆瀵硅薄
+     * @param bindingResult
+     * @return
+     * @throws Exception
+     */
+    @Operation(summary = "鐧诲綍鍑瘉鏍¢獙", description = "鐧诲綍鍑瘉鏍¢獙")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = Boolean.class))}
+            )
+    })
+    @PostMapping(path = "getSessionId")
+    @Transactional(rollbackFor = Exception.class)
+    @SsoAop()
+    public BaseResponse<Boolean> getSessionId(@RequestBody @Valid Code2Session code2Session, BindingResult bindingResult) throws Exception {
+        if(bindingResult != null && bindingResult.hasErrors()){
+            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+        }
+
+        String phoneNumber = code2Session.getPhoneNumber();
+        String jsCode = code2Session.getJs_code();
+
+        Map<String, Object> queryParams = new HashMap<>();
+        queryParams.put("appid", appid);
+        queryParams.put("secret", secret);
+        queryParams.put("js_code", jsCode);
+        queryParams.put("grant_type", grantType);
+        Map<String, String> headerParams = new HashMap<>();
+        JSONObject job = restTemplateUtil.get(loginUrl, queryParams, headerParams);
+
+        if(job.getLong("errcode") != null && job.getLong("errcode") >= -1) {
+            return BaseResponseUtils.buildFail("鐧诲綍鍑瘉鏍¢獙澶辫触");
+        }
+
+        String openid = job.getString("openid");
+        String sessionKey = job.getString("session_key");
+
+        Long clientId = clientSv.getClientIdByPhone(phoneNumber);
+        String SessionId = "";
+        if(clientId != null) {
+            // 娣诲姞寰俊鐢ㄦ埛璐︽埛璁板綍
+            SeOpenId seOpenId = new SeOpenId();
+            seOpenId.setClientId(clientId);
+            seOpenId.setOpenId(openid);
+            seOpenId.setSessionKey(sessionKey);
+            seOpenId.setCreateTime(new Date());
+            Long rec = clientSv.addOpenId(seOpenId);
+            if(rec != null) {
+                SessionId = String.valueOf(rec);
+            }
+            return BaseResponseUtils.buildSuccess(SessionId);
+
+        } else {
+            return BaseResponseUtils.buildError(SellResultCode.PHONE_NUMBER_IS_ERROR.getMessage());
+        }
+    }
 
     /**
      * 涓嬭浇寰俊鏀粯骞冲彴璇佷功 娴嬭瘯瀹屽簾闄�
@@ -221,6 +285,7 @@
         if(job_result == null) {
             return BaseResponseUtils.buildFail(SellResultCode.RECHARGE_ADD_FAIL.getMessage());
         }
+
         return BaseResponseUtils.buildSuccess(job_result) ;
     }
 

--
Gitblit v1.8.0