From 734dfe9eb0a2176103dce8245c69b1194574c68e Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期一, 29 七月 2024 20:16:15 +0800
Subject: [PATCH] 代理新功能完善

---
 src/main/java/com/taoke/autopay/controller/WebApiController.java |  147 +++++++++++++++++++++++++++++++++++++------------
 1 files changed, 111 insertions(+), 36 deletions(-)

diff --git a/src/main/java/com/taoke/autopay/controller/WebApiController.java b/src/main/java/com/taoke/autopay/controller/WebApiController.java
index eed88bd..96032e9 100644
--- a/src/main/java/com/taoke/autopay/controller/WebApiController.java
+++ b/src/main/java/com/taoke/autopay/controller/WebApiController.java
@@ -1,5 +1,6 @@
 package com.taoke.autopay.controller;
 
+import com.google.gson.Gson;
 import com.taoke.autopay.dto.DYOrderDto;
 import com.taoke.autopay.dto.WXAppInfoDto;
 import com.taoke.autopay.entity.KeyOrder;
@@ -38,8 +39,6 @@
     Logger verifyLogger = LoggerFactory.getLogger("keyVerifyLogger");
 
 
-
-
     @Resource
     private KeyOrderService keyOrderService;
 
@@ -52,7 +51,7 @@
     @ResponseBody
     @RequestMapping(value = "submitKey")
     public String submitKey(String key) {
-        if(1>0){
+        if (1 > 0) {
             return JsonUtil.loadFalseResult(0, "鎺ュ彛宸插叧闂�");
         }
         if (StringUtil.isNullOrEmpty(key)) {
@@ -82,12 +81,12 @@
     }
 
     private void addKey(SubmitKeyInfo keyInfo, Long wxUid) throws KeyVerifyException, KeyOrderException, WxOrderCountException {
-       // 瑙f瀽閾炬帴
+        // 瑙f瀽閾炬帴
         List<String> urllist = UrlUtils.parseUrlsFromText(keyInfo.getKey());
 
 
         String verifyAlipayKey = systemConfigService.getValueCache(SystemConfigKeyEnum.ALIPAY_KEY_VERIFY);
-        if(verifyAlipayKey!=null&&verifyAlipayKey.trim().equalsIgnoreCase("1")) {
+        if (verifyAlipayKey != null && verifyAlipayKey.trim().equalsIgnoreCase("1")) {
             try {
                 // 闇�瑕侀獙璇佹敮浠樺疂鍙d护
                 if (urllist.size() < 1) {
@@ -111,14 +110,13 @@
                 }
                 // 楠岃瘉鍐呭
                 DYOrderDto dto = keyOrderService.verifyKey(tradeInfo.getGoodsTitle(), orderStatus, tradeInfo.getItemRealAmount());
-            }catch(KeyVerifyException ee){
+            } catch (KeyVerifyException ee) {
                 try {
                     verifyLogger.warn("鏍¢獙涓嶉�氳繃锛氥�恵}銆�-{}", keyInfo.getKey(), ee.getMessage());
-                }catch(Exception e){
+                } catch (Exception e) {
                 }
-                throw  ee;
-            }
-            catch(Exception e){
+                throw ee;
+            } catch (Exception e) {
                 throw new KeyVerifyException(KeyVerifyException.CODE_COMMON, e.getMessage());
             }
         }
@@ -137,12 +135,12 @@
 
     }
 
-    private void verifySubmitKey(String key) throws Exception{
+    private void verifySubmitKey(String key) throws Exception {
         List<String> urllist = UrlUtils.parseUrlsFromText(key);
         if (urllist.isEmpty() || !urllist.get(0).contains("ur.alipay.com")) {
-           throw new Exception("涓嶅寘鍚敮浠樺疂閾炬帴");
+            throw new Exception("涓嶅寘鍚敮浠樺疂閾炬帴");
         }
-        if(!key.contains("鏀粯瀹�")){
+        if (!key.contains("鏀粯瀹�")) {
             throw new Exception("娌″寘鍚敮浠樺疂姹夊瓧");
         }
     }
@@ -154,9 +152,9 @@
         if (StringUtil.isNullOrEmpty(keyInfo.getKey())) {
             return JsonUtil.loadFalseResult("璇蜂笂浼爇ey");
         }
-        try{
+        try {
             verifySubmitKey(keyInfo.getKey());
-        }catch (Exception e){
+        } catch (Exception e) {
             return JsonUtil.loadFalseResult("鏀粯瀹濆彛浠や笉姝g‘");
         }
 
@@ -185,7 +183,7 @@
         } catch (WxOrderCountException e) {
             return JsonUtil.loadFalseResult(e.getMessage());
         } catch (KeyVerifyException e) {
-            switch (e.getCode()){
+            switch (e.getCode()) {
                 case KeyVerifyException.CODE_KEY_MONEY_NOT_MATCH:
                     return JsonUtil.loadFalseResult("璇ョ瑪璁㈠崟鏈夎锛屼笉浜堟彁浜�");
                 case KeyVerifyException.CODE_ORDER_MONEY_NOT_MATCH:
@@ -197,40 +195,40 @@
     }
 
     /**
+     * @return java.lang.String
      * @author hxh
      * @description 甯﹀彛浠や笌閲戦鐨勫彛浠ゆ彁浜ゆ帴鍙�
      * @date 0:12 2024/7/9
      * @param: keyInfo
      * @param: session
-     * @return java.lang.String
      **/
     @ResponseBody
     @RequestMapping(value = "submitKeyV3")
     public String submitKeyV3(SubmitKeyInfo keyInfo, HttpSession session, HttpServletRequest request) {
-       String referer = request.getHeader("Referer");
+        String referer = request.getHeader("Referer");
         keyInfo.setReferer(referer);
         WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
         if (StringUtil.isNullOrEmpty(keyInfo.getKey())) {
-            return JsonUtil.loadFalseResult( "璇蜂笂浼爇ey");
+            return JsonUtil.loadFalseResult("璇蜂笂浼爇ey");
         }
 //        if (StringUtil.isNullOrEmpty(keyInfo.getMoney())) {
 //            return JsonUtil.loadFalseResult("璇蜂笂浼爉oney");
 //        }
-        try{
+        try {
             verifySubmitKey(keyInfo.getKey());
-        }catch (Exception e){
+        } catch (Exception e) {
             return JsonUtil.loadFalseResult("鏀粯瀹濆彛浠や笉姝g‘");
         }
         // 楠岃瘉鎻愪氦鏃堕棿
-        String timeStr =  systemConfigService.getValueCache(SystemConfigKeyEnum.KEY_SUBMIT_TIME_RANGE);
-        if(StringUtil.isNullOrEmpty(timeStr)){
+        String timeStr = systemConfigService.getValueCache(SystemConfigKeyEnum.KEY_SUBMIT_TIME_RANGE);
+        if (StringUtil.isNullOrEmpty(timeStr)) {
             return JsonUtil.loadFalseResult("灏氭湭閰嶇疆鐢熸晥鏃堕棿");
         }
-        String startTime = timeStr.split(",")[0].trim().replace(":","");
-        String endTime = timeStr.split(",")[1].trim().replace(":","");
-        String now = TimeUtil.getGernalTime(System.currentTimeMillis(),"HHmmss");
-        if(Integer.parseInt(now)<Integer.parseInt(startTime)||Integer.parseInt(now)>Integer.parseInt(endTime)){
-            return JsonUtil.loadFalseResult(String.format("鍙d护鎻愪氦鏃堕棿娈典负锛�%s-%s",timeStr.split(",")[0],timeStr.split(",")[1]));
+        String startTime = timeStr.split(",")[0].trim().replace(":", "");
+        String endTime = timeStr.split(",")[1].trim().replace(":", "");
+        String now = TimeUtil.getGernalTime(System.currentTimeMillis(), "HHmmss");
+        if (Integer.parseInt(now) < Integer.parseInt(startTime) || Integer.parseInt(now) > Integer.parseInt(endTime)) {
+            return JsonUtil.loadFalseResult(String.format("鍙d护鎻愪氦鏃堕棿娈典负锛�%s-%s", timeStr.split(",")[0], timeStr.split(",")[1]));
         }
         if (user == null) {
             // 鍏堜繚瀛楰EY
@@ -257,7 +255,7 @@
             return JsonUtil.loadFalseResult(e.getMessage());
         } catch (KeyVerifyException e) {
             logger.debug("鍙d护鏍¢獙澶辫触锛歿}-{}-{}", keyInfo.getKey(), e.getCode(), e.getMessage());
-            switch (e.getCode()){
+            switch (e.getCode()) {
                 case KeyVerifyException.CODE_KEY_MONEY_NOT_MATCH:
                     return JsonUtil.loadFalseResult("璇ョ瑪璁㈠崟鏈夎锛屼笉浜堟彁浜�");
                 case KeyVerifyException.CODE_ORDER_MONEY_NOT_MATCH:
@@ -266,6 +264,70 @@
             return JsonUtil.loadFalseResult(e.getMessage());
         }
     }
+
+    @ResponseBody
+    @RequestMapping(value = "submitKeyV4")
+    public String submitKeyV4(SubmitKeyInfo keyInfo, HttpSession session, HttpServletRequest request) {
+        String referer = request.getHeader("Referer");
+        keyInfo.setReferer(referer);
+        WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
+        if (StringUtil.isNullOrEmpty(keyInfo.getKey())) {
+            return JsonUtil.loadFalseResult("璇蜂笂浼爇ey");
+        }
+
+        try {
+            verifySubmitKey(keyInfo.getKey());
+        } catch (Exception e) {
+            return JsonUtil.loadFalseResult("鏀粯瀹濆彛浠や笉姝g‘");
+        }
+        // 楠岃瘉鎻愪氦鏃堕棿
+        String timeStr = systemConfigService.getValueCache(SystemConfigKeyEnum.KEY_SUBMIT_TIME_RANGE);
+        if (StringUtil.isNullOrEmpty(timeStr)) {
+            return JsonUtil.loadFalseResult("灏氭湭閰嶇疆鐢熸晥鏃堕棿");
+        }
+        String startTime = timeStr.split(",")[0].trim().replace(":", "");
+        String endTime = timeStr.split(",")[1].trim().replace(":", "");
+        String now = TimeUtil.getGernalTime(System.currentTimeMillis(), "HHmmss");
+        if (Integer.parseInt(now) < Integer.parseInt(startTime) || Integer.parseInt(now) > Integer.parseInt(endTime)) {
+            return JsonUtil.loadFalseResult(String.format("鍙d护鎻愪氦鏃堕棿娈典负锛�%s-%s", timeStr.split(",")[0], timeStr.split(",")[1]));
+        }
+
+        if (user == null) {
+            // 鍏堜繚瀛楰EY
+            session.setAttribute(Constant.SESSION_KEY_TEMP_ALIPAY_KEY, keyInfo);
+            wxLogger.info("寰俊娌℃湁鎺堟潈:" + session.getId());
+            String redictLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_REDIRECT_LINK);
+            if (StringUtil.isNullOrEmpty(redictLink)) {
+                return JsonUtil.loadFalseResult("鏃犳硶鑾峰彇鍒版巿鏉冮摼鎺�");
+            }
+
+            redictLink = redictLink.replace("snsapi_base", "snsapi_userinfo");
+            // 娌℃湁鐧诲綍锛岃繑鍥炵櫥褰曢摼鎺�
+            JSONObject root = new JSONObject();
+            root.put("link", redictLink);
+            return JsonUtil.loadTrueResult(Constant.RESULT_CODE_NEED_LOGIN, root);
+        }
+        wxLogger.info("寰俊鏈夋巿鏉�:" + session.getId());
+        try {
+            addKey(keyInfo, user.getId());
+            return JsonUtil.loadTrueResult("");
+        } catch (KeyOrderException e) {
+            e.printStackTrace();
+            return JsonUtil.loadFalseResult(e.getMessage());
+        } catch (WxOrderCountException e) {
+            return JsonUtil.loadFalseResult(e.getMessage());
+        } catch (KeyVerifyException e) {
+            LogUtil.loggerDebug.debug("鍙d护鏍¢獙澶辫触锛歿}-{}-{}", keyInfo.getKey(), e.getCode(), e.getMessage());
+            switch (e.getCode()) {
+                case KeyVerifyException.CODE_KEY_MONEY_NOT_MATCH:
+                    return JsonUtil.loadFalseResult("璇ョ瑪璁㈠崟鏈夎锛屼笉浜堟彁浜�");
+                case KeyVerifyException.CODE_ORDER_MONEY_NOT_MATCH:
+                    return JsonUtil.loadFalseResult("鎻愪氦閲戦涓嶅尮閰�");
+            }
+            return JsonUtil.loadFalseResult(e.getMessage());
+        }
+    }
+
 
     @RequestMapping(value = "wxLogin")
     public void wxLogin(String code, String state, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
@@ -278,15 +340,28 @@
         try {
             WXAppInfoDto wxApp = systemConfigService.getWxAppInfoCache();
             String successLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_LOGIN_SUCCESS_LINK);
-            if(!StringUtil.isNullOrEmpty(referer)){
+            if (!StringUtil.isNullOrEmpty(referer)) {
                 Map<String, String> params = HttpUtil.getPramsFromUrl(referer);
-                params.put("state","SUCCESS");
-                successLink = HttpUtil.getWholeUrl(HttpUtil.getUrlWithoutParams(referer),params);
+                params.put("state", "SUCCESS");
+                successLink = HttpUtil.getWholeUrl(HttpUtil.getUrlWithoutParams(referer), params);
             }
 
             WxApiUtil.WXAccessTokenInfo tokenInfo = WxApiUtil.getAcessTokenInfo(code, wxApp);
             if (tokenInfo != null && !StringUtil.isNullOrEmpty(tokenInfo.getOpenid())) {
-                WxUserInfo user = wxUserService.login(tokenInfo.getOpenid());
+                WxApiUtil.WXUserInfo wxUserInfo = null;
+                if (tokenInfo.getScope() != null && tokenInfo.getScope().contains("snsapi_userinfo")) {
+                    try {
+                        wxUserInfo = WxApiUtil.getUserInfo(tokenInfo.getAccess_token(), tokenInfo.getOpenid());
+                        wxLogger.info("瑙f瀽缁撴灉", new Gson().toJson(wxUserInfo));
+                    } catch (Exception e) {
+                        wxLogger.error("瑙f瀽鍑洪敊", e);
+                    }
+                }
+                if (wxUserInfo == null) {
+                    wxUserInfo = new WxApiUtil.WXUserInfo();
+                    wxUserInfo.setOpenid(tokenInfo.getOpenid());
+                }
+                WxUserInfo user = wxUserService.login(wxUserInfo);
                 session.setAttribute(Constant.SESSION_KEY_USER, user);
                 wxLogger.info("寰俊淇濆瓨鐢ㄦ埛淇℃伅锛歿} id-{}", session.getId(), user.getId());
 
@@ -300,10 +375,10 @@
         } catch (Exception e) {
             e.printStackTrace();
             wxLogger.error("鎺堟潈澶辫触锛歿}", e.getMessage());
-            if(!StringUtil.isNullOrEmpty(referer)){
+            if (!StringUtil.isNullOrEmpty(referer)) {
                 Map<String, String> params = HttpUtil.getPramsFromUrl(referer);
-                params.put("state","FAIL");
-                failLink = HttpUtil.getWholeUrl(HttpUtil.getUrlWithoutParams(referer),params);
+                params.put("state", "FAIL");
+                failLink = HttpUtil.getWholeUrl(HttpUtil.getUrlWithoutParams(referer), params);
             }
         }
         response.sendRedirect(failLink);

--
Gitblit v1.8.0