From 30a6547bd6050459526780d85eb86cdccf9095d2 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期六, 28 十二月 2019 16:56:31 +0800
Subject: [PATCH] 打印pd 日志

---
 AppFrontService/src/main/java/com/java110/app/smo/wxLogin/impl/WxLoginSMOImpl.java |  137 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 122 insertions(+), 15 deletions(-)

diff --git a/AppFrontService/src/main/java/com/java110/app/smo/wxLogin/impl/WxLoginSMOImpl.java b/AppFrontService/src/main/java/com/java110/app/smo/wxLogin/impl/WxLoginSMOImpl.java
index e12f35e..1117bc2 100644
--- a/AppFrontService/src/main/java/com/java110/app/smo/wxLogin/impl/WxLoginSMOImpl.java
+++ b/AppFrontService/src/main/java/com/java110/app/smo/wxLogin/impl/WxLoginSMOImpl.java
@@ -1,58 +1,157 @@
 package com.java110.app.smo.wxLogin.impl;
 
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.app.properties.WechatAuthProperties;
+import com.java110.app.smo.AppAbstractComponentSMO;
 import com.java110.app.smo.wxLogin.IWxLoginSMO;
 import com.java110.core.component.AbstractComponentSMO;
 import com.java110.core.context.IPageData;
-import com.java110.entity.component.ComponentValidateResult;
-import com.java110.utils.constant.PrivilegeCodeConstant;
-import com.java110.utils.constant.ServiceConstant;
+import com.java110.core.factory.AuthenticationFactory;
+import com.java110.utils.cache.MappingCache;
+import com.java110.utils.constant.*;
 import com.java110.utils.exception.SMOException;
-import com.java110.utils.util.BeanConvertUtil;
+import com.java110.utils.util.Assert;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
+import java.util.HashMap;
 import java.util.Map;
 
 /**
  * wx鐧诲綍
  */
 @Service("wxLoginSMOImpl")
-public class WxLoginSMOImpl extends AbstractComponentSMO implements IWxLoginSMO {
+public class WxLoginSMOImpl extends AppAbstractComponentSMO implements IWxLoginSMO {
+
+    private final static Logger logger = LoggerFactory.getLogger(WxLoginSMOImpl.class);
 
     @Autowired
     private RestTemplate restTemplate;
 
+    @Autowired
+    private WechatAuthProperties wechatAuthProperties;
+
     @Override
-    public ResponseEntity<String> getSessionInfo(IPageData pd) throws SMOException {
+    public ResponseEntity<String> doLogin(IPageData pd) throws SMOException {
         return businessProcess(pd);
     }
 
     @Override
     protected void validate(IPageData pd, JSONObject paramIn) {
 
-        super.validatePageInfo(pd);
+        //super.validatePageInfo(pd);
 
-        super.checkUserHasPrivilege(pd, restTemplate, PrivilegeCodeConstant.LIST_ORG);
+        Assert.hasKeyAndValue(paramIn, "code", "璇锋眰鎶ユ枃涓湭鍖呭惈code淇℃伅");
+        //super.checkUserHasPrivilege(pd, restTemplate, PrivilegeCodeConstant.LIST_ORG);
     }
 
     @Override
     protected ResponseEntity<String> doBusinessProcess(IPageData pd, JSONObject paramIn) {
-        ComponentValidateResult result = super.validateStoreStaffCommunityRelationship(pd, restTemplate);
 
-        Map paramMap = BeanConvertUtil.beanCovertMap(result);
-        paramIn.putAll(paramMap);
+        logger.debug("doLogin鍏ュ弬锛�" + paramIn.toJSONString());
+        ResponseEntity<String> responseEntity;
+        String code = paramIn.getString("code");
+        String urlString = "?appid={appId}&secret={secret}&js_code={code}&grant_type={grantType}";
+        String response = restTemplate.getForObject(
+                wechatAuthProperties.getSessionHost() + urlString, String.class,
+                wechatAuthProperties.getAppId(),
+                wechatAuthProperties.getSecret(),
+                code,
+                wechatAuthProperties.getGrantType());
 
-        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/org.listOrgs" + mapToUrlParam(paramIn);
+        logger.debug("wechatAuthProperties:" + JSONObject.toJSONString(wechatAuthProperties));
 
+        logger.debug("寰俊杩斿洖鎶ユ枃锛�" + response);
 
-        ResponseEntity<String> responseEntity = this.callCenterService(restTemplate, pd, "",
-                apiUrl,
-                HttpMethod.GET);
+        //Assert.jsonObjectHaveKey(response, "errcode", "杩斿洖鎶ユ枃涓湭鍖呭惈 閿欒缂栫爜锛屾帴鍙e嚭閿�");
+        JSONObject responseObj = JSONObject.parseObject(response);
 
+        if (responseObj.containsKey("errcode") && !"0".equals(responseObj.getString("errcode"))) {
+            throw new IllegalArgumentException("寰俊楠岃瘉澶辫触锛屽彲鑳芥槸code澶辨晥" + responseObj);
+        }
+
+        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);
+        }
+
+        JSONObject userResult = JSONObject.parseObject(responseEntity.getBody());
+        int total = userResult.getIntValue("total");
+
+        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());
+        }
+
+        JSONObject realUserInfo = userResult.getJSONArray("users").getJSONObject(0);
+        userInfo.putAll(realUserInfo);
+        userInfo.put("password", "");
+
+        try {
+            Map userMap = new HashMap();
+            userMap.put(CommonConstant.LOGIN_USER_ID, userInfo.getString("userId"));
+            userMap.put(CommonConstant.LOGIN_USER_NAME, userInfo.getString("name"));
+            String token = AuthenticationFactory.createAndSaveToken(userMap);
+            JSONObject paramOut = new JSONObject();
+            paramOut.put("result", 0);
+            paramOut.put("userInfo", userInfo);
+            paramOut.put("token", token);
+            paramOut.put("sessionKey", sessionKey);
+            pd.setToken(token);
+            responseEntity = new ResponseEntity<String>(paramOut.toJSONString(), HttpStatus.OK);
+        } catch (Exception e) {
+            logger.error("鐧诲綍寮傚父锛�", e);
+            throw new IllegalArgumentException("閴存潈澶辫触");
+        }
+        //鏍规嵁openId 鏌ヨ鐢ㄦ埛淇℃伅锛屾槸鍚﹀瓨鍦ㄧ敤鎴�
         return responseEntity;
     }
 
@@ -63,4 +162,12 @@
     public void setRestTemplate(RestTemplate restTemplate) {
         this.restTemplate = restTemplate;
     }
+
+    public WechatAuthProperties getWechatAuthProperties() {
+        return wechatAuthProperties;
+    }
+
+    public void setWechatAuthProperties(WechatAuthProperties wechatAuthProperties) {
+        this.wechatAuthProperties = wechatAuthProperties;
+    }
 }

--
Gitblit v1.8.0