From 368deac57a30df0d5506dd1b970ab61b455c79f0 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期六, 11 七月 2020 22:47:15 +0800
Subject: [PATCH] 优化代码

---
 service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java |   49 ++++++++++++++++++++++++++++++++++---------------
 1 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java b/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java
index a46b745..db19b6c 100644
--- a/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java
+++ b/service-front/src/main/java/com/java110/front/smo/ownerLogin/impl/OwnerAppLoginSMOImpl.java
@@ -6,6 +6,7 @@
 import com.java110.core.context.IPageData;
 import com.java110.core.context.PageData;
 import com.java110.core.factory.AuthenticationFactory;
+import com.java110.core.factory.WechatFactory;
 import com.java110.dto.owner.OwnerAppUserDto;
 import com.java110.dto.smallWeChat.SmallWeChatDto;
 import com.java110.dto.user.UserDto;
@@ -75,9 +76,9 @@
         UserDto userDto = new UserDto();
         userDto.setUserName(loginInfo.getString("username"));
         userDto.setPassword(loginInfo.getString("password"));
-        userDto = super.postForApi(pd,userDto,ServiceCodeConstant.SERVICE_CODE_USER_LOGIN,UserDto.class);
+        userDto = super.postForApi(pd, userDto, ServiceCodeConstant.SERVICE_CODE_USER_LOGIN, UserDto.class);
 
-        if(userDto == null){
+        if (userDto == null) {
             responseEntity = new ResponseEntity<>("鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒", HttpStatus.BAD_REQUEST);
             return responseEntity;
         }
@@ -166,7 +167,7 @@
     }
 
     @Override
-    public ResponseEntity<String> getPageAccessToken(IPageData pd) throws SMOException {
+    public ResponseEntity<String> getPageAccessToken(IPageData pd, HttpServletRequest request) throws SMOException {
         JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
         String authCode = paramIn.getString("code");
         String state = paramIn.getString("state");
@@ -179,19 +180,22 @@
         JSONObject param = JSONObject.parseObject(paramStr);
         String redirectUrl = param.getString("redirectUrl");
         String errorUrl = param.getString("errorUrl");
-        SmallWeChatDto smallWeChatDto = getSmallWechat(pd, paramIn);
-
+        String wId = paramIn.getString("wId");
+        SmallWeChatDto smallWeChatDto = null;
+        if (!StringUtil.isEmpty(wId)) {
+            paramIn.put("appId", WechatFactory.getAppId(wId));
+            smallWeChatDto = getSmallWechat(pd, paramIn);
+        }
         if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
             smallWeChatDto = new SmallWeChatDto();
-            smallWeChatDto.setAppId(wechatAuthProperties.getAppId());
-            smallWeChatDto.setAppSecret(wechatAuthProperties.getSecret());
+            smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
             smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
             smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
         }
 
-
-        String url = WechatConstant.APP_GET_ACCESS_TOKEN_URL.replace("APPID", wechatAuthProperties.getWechatAppId())
-                .replace("SECRET", wechatAuthProperties.getWechatAppSecret())
+        String url = WechatConstant.APP_GET_ACCESS_TOKEN_URL.replace("APPID", smallWeChatDto.getAppId())
+                .replace("SECRET", smallWeChatDto.getAppSecret())
                 .replace("CODE", authCode);
 
         ResponseEntity<String> paramOut = outRestTemplate.getForEntity(url, String.class);
@@ -209,7 +213,7 @@
 
         int loginFlag = paramIn.getInteger("loginFlag");
         //璇存槑鏄櫥褰曢〉闈紝涓嬪彂code 灏卞彲浠ワ紝涓嶉渶瑕佷笅鍙慿ey 涔嬬被
-        if(loginFlag == LOGIN_PAGE){
+        if (loginFlag == LOGIN_PAGE) {
             //灏唎penId鏀惧埌redis 缂撳瓨锛岀粰鍓嶆涓嬪彂涓存椂绁ㄦ嵁
             String code = UUID.randomUUID().toString();
             CommonCache.setValue(code, openId, expireTime);
@@ -247,7 +251,7 @@
         userDto.setUserId(ownerAppUserDtos.get(0).getUserId());
         UserDto tmpUserDto = super.getForApi(pd, userDto, ServiceCodeConstant.QUERY_USER_SECRET, UserDto.class);
 
-        if(StringUtil.isEmpty(tmpUserDto.getKey())){
+        if (StringUtil.isEmpty(tmpUserDto.getKey())) {
             String code = UUID.randomUUID().toString();
             CommonCache.setValue(code, openId, expireTime);
             return ResultVo.redirectPage(errorUrl + "?code=" + code);
@@ -278,6 +282,20 @@
         param.put("redirectUrl", redirectUrl);
         param.put("errorUrl", errorUrl);
         CommonCache.setValue(urlCode, param.toJSONString(), expireTime);
+        String wAppId = request.getHeader("w-app-id");
+        SmallWeChatDto smallWeChatDto = null;
+        if (!StringUtil.isEmpty(wAppId)) {
+            JSONObject paramIn = new JSONObject();
+            paramIn.put("appId", wAppId);
+            smallWeChatDto = getSmallWechat(pd, paramIn);
+        }
+        if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
+            smallWeChatDto = new SmallWeChatDto();
+            smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
+            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+        }
 
         URL url = null;
         String openUrl = "";
@@ -290,14 +308,15 @@
             }
 
             openUrl = WechatConstant.OPEN_AUTH
-                    .replace("APPID", wechatAuthProperties.getWechatAppId())
+                    .replace("APPID", smallWeChatDto.getAppId())
                     .replace("SCOPE", "snsapi_base")
                     .replace(
                             "REDIRECT_URL",
                             URLEncoder
                                     .encode(
                                             (newUrl
-                                                    + "/app/loginOwnerWechatAuth?appId=992020061452450002&urlCode=" + urlCode+"&loginFlag="+loginFlag),
+                                                    + "/app/loginOwnerWechatAuth?appId=992020061452450002&urlCode=" +
+                                                    urlCode + "&loginFlag=" + loginFlag + "&wId=" + WechatFactory.getWId(wAppId)),
                                             "UTF-8")).replace("STATE", "1");
 
         } catch (Exception e) {
@@ -379,7 +398,7 @@
                 pd.getAppId());
         responseEntity = this.callCenterService(restTemplate, pd, "",
                 ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
-                        + paramIn.getString("appId") + "&page=1&row=1&communityId="+ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET);
+                        + paramIn.getString("appId") + "&page=1&row=1&communityId=" + ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET);
 
         if (responseEntity.getStatusCode() != HttpStatus.OK) {
             return responseEntity;

--
Gitblit v1.8.0