From 62a7f3228c94db41bf57858a6549eb0db033bf1e Mon Sep 17 00:00:00 2001
From: Administrator <zhubaomin>
Date: 星期三, 29 五月 2024 11:44:17 +0800
Subject: [PATCH] 2024-05-29 朱宝民

---
 pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/client/ClientCtrl.java |  105 ++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 85 insertions(+), 20 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/client/ClientCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/client/ClientCtrl.java
index 7dea6ef..b6099e9 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/client/ClientCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/client/ClientCtrl.java
@@ -10,10 +10,15 @@
 import com.dy.common.webUtil.ResultCodeMsg;
 import com.dy.pipIrrGlobal.pojoBa.BaClient;
 import com.dy.pipIrrGlobal.pojoSe.SeClient;
+import com.dy.pipIrrGlobal.pojoSe.SeCodeVerify;
+import com.dy.pipIrrGlobal.pojoSe.SeOpenId;
 import com.dy.pipIrrGlobal.voSe.VoClient;
+import com.dy.pipIrrSell.client.dto.CodeVerifyDTO;
 import com.dy.pipIrrSell.result.SellResultCode;
 import com.dy.pipIrrSell.sms.AliyunSmsSv;
 import com.dy.pipIrrSell.sms.RandomCode;
+import com.dy.pipIrrSell.util.RestTemplateUtil;
+import com.dy.pipIrrSell.wechatpay.PayInfo;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.media.Content;
 import io.swagger.v3.oas.annotations.media.Schema;
@@ -29,6 +34,7 @@
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.*;
 
+import java.io.IOException;
 import java.util.*;
 
 /**
@@ -47,6 +53,18 @@
     private final ClientSv clientSv;
     private final AliyunSmsSv aliyunSmsSv;
     //private final RedisUtils redisUtils;
+    private final RestTemplateUtil restTemplateUtil;
+
+    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;
+    private final String description = PayInfo.description;
+    private final String loginUrl = PayInfo.loginUrl;
+    private final String notifyUrl = PayInfo.notifyUrl;
+    private final String grantType = PayInfo.grantType;
 
     /**
      * 鑾峰彇鍐滄埛鍒楄〃
@@ -363,6 +381,19 @@
 
         //redisUtils.set(phoneNumber, securityCode, 60);
 
+        // 鑾峰彇褰撳墠鏃堕棿鎴冲苟寤跺悗3鍒嗛挓
+        Long timestamp = System.currentTimeMillis();
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeInMillis(timestamp);
+        calendar.add(Calendar.SECOND, 180);
+        Long expires = calendar.getTimeInMillis();
+
+        SeCodeVerify codeVerify = new SeCodeVerify();
+        codeVerify.setPhoneNumber(phoneNumber);
+        codeVerify.setSecurityCode(securityCode);
+        codeVerify.setExpires(expires);
+        clientSv.addCodeVerify(codeVerify);
+
         SendSmsResponse response = aliyunSmsSv.sendSms(phoneNumber, templateParam);
         if (response.getCode().equals("OK")) {
             // 鍙戦�佹垚鍔熷鐞嗛�昏緫
@@ -375,10 +406,9 @@
 
     /**
      * 鏍¢獙楠岃瘉鐮�
-     * @param phoneNumber
-     * @param code
+     * @param po
+     * @param bindingResult
      * @return
-     * @throws ClientException
      */
     @Operation(summary = "鏍¢獙楠岃瘉鐮�", description = "鏍¢獙楠岃瘉鐮�")
     @ApiResponses(value = {
@@ -389,27 +419,62 @@
                             schema = @Schema(implementation = Boolean.class))}
             )
     })
-    @PostMapping(path = "verify")
+    @PostMapping(path = "verify", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @Transactional(rollbackFor = Exception.class)
     @SsoAop()
-    public BaseResponse<Boolean> verify(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("code") String code) throws ClientException {
-        if(phoneNumber == null || phoneNumber.length() <= 0 || code == null || code.length() <=0) {
-            return BaseResponseUtils.buildFail(SellResultCode.VERIFY_PARAMS_INCOMPLETE.getMessage());
+    public BaseResponse<Boolean> verify(@RequestBody @Valid CodeVerifyDTO po, BindingResult bindingResult) throws IOException {
+        if(bindingResult != null && bindingResult.hasErrors()){
+            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
         }
 
-        // redis涓敭涓嶅瓨鍦ㄦ垨閿�间笉瀛樺湪鍒欐彁绀烘牎楠屽け璐�
-        //if(!redisUtils.hasKey(phoneNumber) || redisUtils.get(phoneNumber) == null)
-        //    return BaseResponseUtils.buildFail(SellResultCode.CODE_VERIFY_FAIL.getMessage());
-        //
-        //String code_redis = redisUtils.get(phoneNumber).toString();
-        //System.out.println("--------------------: " + code_redis);
-        //
-        //if(code_redis != null && code_redis.equals(code)) {
-        //    return BaseResponseUtils.buildSuccess(true) ;
-        //}else {
-        //    return BaseResponseUtils.buildFail(SellResultCode.CODE_VERIFY_FAIL.getMessage());
-        //}
+        String phoneNumber = po.getPhoneNumber();
+        String securityCode = po.getSecurityCode();
+        String code = po.getCode();
 
-        return BaseResponseUtils.buildSuccess(true) ;
+        // 杩涜鎵嬫満鍙枫�侀獙璇佺爜銆佽繃鏈熸椂闂存牎楠�
+        SeCodeVerify codeVerify = clientSv.getCodeVerify(phoneNumber);
+        if(codeVerify == null) {
+            return BaseResponseUtils.buildFail(SellResultCode.NO_SECURITY_CODE_FOR_PHONE.getMessage());
+        }
+
+        if(!codeVerify.getSecurityCode().equals(securityCode)) {
+            return BaseResponseUtils.buildFail(SellResultCode.SECURITY_CODE_ERROR.getMessage());
+        }
+
+        Long currentTimestamp = System.currentTimeMillis();
+        if(currentTimestamp > codeVerify.getExpires() ) {
+            return BaseResponseUtils.buildFail(SellResultCode.VALIDATION_TIMEOUT.getMessage());
+        }
+
+        // 鏍¢獙閫氳繃锛岃繘琛岀櫥褰曞嚟璇佹牎楠�
+        Map<String, Object> queryParams = new HashMap<>();
+        queryParams.put("appid", appid);
+        queryParams.put("secret", secret);
+        queryParams.put("js_code", code);
+        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);
+        if(clientId != null) {
+            // 娣诲姞寰俊鐢ㄦ埛璐︽埛璁板綍
+            SeOpenId seOpenId = new SeOpenId();
+            seOpenId.setClientId(clientId);
+            seOpenId.setOpenId(openid);
+            seOpenId.setSessionKey(sessionKey);
+            seOpenId.setCreateTime(new Date());
+            Long SessionId = clientSv.addOpenId(seOpenId);
+            return BaseResponseUtils.buildSuccess(SessionId);
+
+        } else {
+            return BaseResponseUtils.buildError(SellResultCode.PHONE_NUMBER_IS_ERROR.getMessage());
+        }
     }
 
     /**

--
Gitblit v1.8.0