From edd99f22438e70895735add28524f54a8cd604bb Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期五, 17 七月 2020 13:02:49 +0800
Subject: [PATCH] 优化代码

---
 service-front/src/main/java/com/java110/front/smo/wxLogin/impl/WxLoginSMOImpl.java |  116 +++++++++++++++++++++++++++++----------------------------
 1 files changed, 59 insertions(+), 57 deletions(-)

diff --git a/service-front/src/main/java/com/java110/front/smo/wxLogin/impl/WxLoginSMOImpl.java b/service-front/src/main/java/com/java110/front/smo/wxLogin/impl/WxLoginSMOImpl.java
index 2985aec..a77dd06 100644
--- a/service-front/src/main/java/com/java110/front/smo/wxLogin/impl/WxLoginSMOImpl.java
+++ b/service-front/src/main/java/com/java110/front/smo/wxLogin/impl/WxLoginSMOImpl.java
@@ -2,15 +2,22 @@
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.core.context.IPageData;
+import com.java110.core.context.PageData;
+import com.java110.core.factory.AuthenticationFactory;
+import com.java110.core.factory.CallApiServiceFactory;
+import com.java110.dto.owner.OwnerAppUserDto;
+import com.java110.dto.smallWeChat.SmallWeChatDto;
 import com.java110.front.properties.WechatAuthProperties;
 import com.java110.front.smo.AppAbstractComponentSMO;
 import com.java110.front.smo.wxLogin.IWxLoginSMO;
-import com.java110.core.context.IPageData;
-import com.java110.core.factory.AuthenticationFactory;
-import com.java110.utils.cache.MappingCache;
-import com.java110.utils.constant.*;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.ServiceCodeConstant;
+import com.java110.utils.constant.ServiceConstant;
 import com.java110.utils.exception.SMOException;
 import com.java110.utils.util.Assert;
+import com.java110.utils.util.BeanConvertUtil;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,6 +28,7 @@
 import org.springframework.web.client.RestTemplate;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -58,13 +66,26 @@
     protected ResponseEntity<String> doBusinessProcess(IPageData pd, JSONObject paramIn) {
 
         logger.debug("doLogin鍏ュ弬锛�" + paramIn.toJSONString());
+
+        SmallWeChatDto smallWeChatDto = null;
+        if (paramIn.containsKey("appId") && !StringUtils.isEmpty(paramIn.getString("appId"))) {
+            smallWeChatDto = getSmallWechat(pd, paramIn);
+        }
+
+        if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
+            smallWeChatDto = new SmallWeChatDto();
+            smallWeChatDto.setAppId(wechatAuthProperties.getAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getSecret());
+            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+        }
         ResponseEntity<String> responseEntity;
         String code = paramIn.getString("code");
         String urlString = "?appid={appId}&secret={secret}&js_code={code}&grant_type={grantType}";
         String response = outRestTemplate.getForObject(
                 wechatAuthProperties.getSessionHost() + urlString, String.class,
-                wechatAuthProperties.getAppId(),
-                wechatAuthProperties.getSecret(),
+                smallWeChatDto.getAppId(),
+                smallWeChatDto.getAppSecret(),
                 code,
                 wechatAuthProperties.getGrantType());
 
@@ -82,64 +103,21 @@
         String openId = responseObj.getString("openid");
         String sessionKey = responseObj.getString("session_key");
 
-        responseEntity = super.getUserInfoByOpenId(pd, restTemplate, openId);
-
-        logger.debug("鏌ヨ鐢ㄦ埛淇℃伅杩斿洖鎶ユ枃锛�" + responseEntity);
-        if (responseEntity.getStatusCode() != HttpStatus.OK) {
-            throw new IllegalArgumentException("鏍圭粷openId 鏌ヨ鐢ㄦ埛淇℃伅寮傚父" + openId + ", " + responseEntity.getBody());
-        }
-
-        JSONObject userResult = JSONObject.parseObject(responseEntity.getBody());
-        int total = userResult.getIntValue("total");
+        //responseEntity = super.getUserInfoByOpenId(pd, restTemplate, openId);
+        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
+        ownerAppUserDto.setOpenId(openId);
+        List<OwnerAppUserDto> ownerAppUserDtos = CallApiServiceFactory.getForApis(pd, ownerAppUserDto, ServiceCodeConstant.LIST_APPUSERBINDINGOWNERS, OwnerAppUserDto.class);
         JSONObject paramOut = new JSONObject();
-        JSONObject userInfo = paramIn.getJSONObject("userInfo");
-
-        if (total == 0) {
-            //淇濆瓨鐢ㄦ埛淇℃伅
-            /*JSONObject registerInfo = new JSONObject();
-
-            //璁剧疆榛樿瀵嗙爜
-            String userDefaultPassword = MappingCache.getValue(MappingConstant.KEY_STAFF_DEFAULT_PASSWORD);
-            Assert.hasLength(userDefaultPassword, "鏄犲皠琛ㄤ腑鏈缃憳宸ラ粯璁ゅ瘑鐮侊紝璇锋鏌�" + MappingConstant.KEY_STAFF_DEFAULT_PASSWORD);
-            userDefaultPassword = AuthenticationFactory.passwdMd5(userDefaultPassword);
-
-            registerInfo.put("userId", "-1");
-            registerInfo.put("email", "");
-            registerInfo.put("address", userInfo.getString("country") + userInfo.getString("province") + userInfo.getString("city"));
-            registerInfo.put("locationCd", "001");
-            registerInfo.put("age", "1");
-            registerInfo.put("sex", "2".equals(userInfo.getString("gender")) ? "1" : "0");
-            registerInfo.put("tel", "-1");
-            registerInfo.put("level_cd", "1");
-            registerInfo.put("name", userInfo.getString("nickName"));
-            registerInfo.put("password", userDefaultPassword);
-            JSONArray userAttr = new JSONArray();
-            JSONObject userAttrObj = new JSONObject();
-            userAttrObj.put("attrId", "-1");
-            userAttrObj.put("specCd", "100201911001");
-            userAttrObj.put("value", openId);
-            userAttr.add(userAttrObj);
-            registerInfo.put("businessUserAttr", userAttr);
-            responseEntity = this.callCenterService(restTemplate, pd, registerInfo.toJSONString(), ServiceConstant.SERVICE_API_URL + "/api/user.service.register", HttpMethod.POST);
-            if (responseEntity.getStatusCode() != HttpStatus.OK) {
-                throw new IllegalArgumentException("淇濆瓨鐢ㄦ埛淇℃伅澶辫触");
-            }
-            responseEntity = super.getUserInfoByOpenId(pd, restTemplate, openId);
-
-            logger.debug("鏌ヨ鐢ㄦ埛淇℃伅杩斿洖鎶ユ枃锛�" + responseEntity);
-            if (responseEntity.getStatusCode() != HttpStatus.OK) {
-                throw new IllegalArgumentException("鏍圭粷openId 鏌ヨ鐢ㄦ埛淇℃伅寮傚父" + openId);
-            }
-            userResult = JSONObject.parseObject(responseEntity.getBody());*/
+        if (ownerAppUserDtos == null || ownerAppUserDtos.size() < 1) {
+            //灏唎penId鏀惧埌redis 缂撳瓨锛岀粰鍓嶆涓嬪彂涓存椂绁ㄦ嵁
             paramOut.put("openId", openId);
             paramOut.put("msg", "杩樻病鏈夋敞鍐岃鍏堟敞鍐�");
             responseEntity = new ResponseEntity<String>(paramOut.toJSONString(), HttpStatus.UNAUTHORIZED);
 
             return responseEntity;
         }
-
-        JSONObject realUserInfo = userResult.getJSONArray("users").getJSONObject(0);
-        userInfo.putAll(realUserInfo);
+        JSONObject userInfo = paramIn.getJSONObject("userInfo");
+        userInfo.putAll(BeanConvertUtil.beanCovertMap(ownerAppUserDtos.get(0)));
         userInfo.put("password", "");
 
         try {
@@ -162,6 +140,30 @@
         return responseEntity;
     }
 
+    private SmallWeChatDto getSmallWechat(IPageData pd, JSONObject paramIn) {
+
+        ResponseEntity responseEntity = null;
+
+        pd = PageData.newInstance().builder(pd.getUserId(), "", "", pd.getReqData(),
+                "", "", "", "",
+                pd.getAppId());
+        responseEntity = this.callCenterService(restTemplate, pd, "",
+                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
+                        + paramIn.getString("appId") + "&page=1&row=1", HttpMethod.GET);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return null;
+        }
+        JSONObject smallWechatObj = JSONObject.parseObject(responseEntity.getBody().toString());
+        JSONArray smallWeChats = smallWechatObj.getJSONArray("smallWeChats");
+
+        if (smallWeChats == null || smallWeChats.size() < 1) {
+            return null;
+        }
+
+        return BeanConvertUtil.covertBean(smallWeChats.get(0), SmallWeChatDto.class);
+    }
+
     public RestTemplate getRestTemplate() {
         return restTemplate;
     }

--
Gitblit v1.8.0