deason 2 éve
szülő
commit
b603068b38

+ 52 - 59
src/main/java/cn/com/qmth/examcloud/app/service/impl/CoreAuthServiceImpl.java

@@ -7,35 +7,12 @@
 
 package cn.com.qmth.examcloud.app.service.impl;
 
-import static cn.com.qmth.examcloud.app.model.Constants.APP_SESSION_EXPIRE_TIME;
-import static cn.com.qmth.examcloud.app.model.Constants.APP_SESSION_USER_KEY_PREFIX;
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import org.springframework.util.Assert;
-
-import com.alibaba.fastjson.JSONObject;
-
 import cn.com.qmth.examcloud.app.core.SysProperty;
 import cn.com.qmth.examcloud.app.core.exception.ApiException;
 import cn.com.qmth.examcloud.app.core.utils.HttpClientBuilder;
 import cn.com.qmth.examcloud.app.core.utils.HttpUtils;
 import cn.com.qmth.examcloud.app.core.utils.ThreadUtils;
-import cn.com.qmth.examcloud.app.model.Constants;
-import cn.com.qmth.examcloud.app.model.ExamInfo;
-import cn.com.qmth.examcloud.app.model.LoginInfo;
-import cn.com.qmth.examcloud.app.model.LoginType;
-import cn.com.qmth.examcloud.app.model.ResBody;
-import cn.com.qmth.examcloud.app.model.Result;
-import cn.com.qmth.examcloud.app.model.UserInfo;
+import cn.com.qmth.examcloud.app.model.*;
 import cn.com.qmth.examcloud.app.service.CoreAuthService;
 import cn.com.qmth.examcloud.app.service.RedisService;
 import cn.com.qmth.examcloud.commons.util.JsonMapper;
@@ -44,11 +21,19 @@ import cn.com.qmth.examcloud.core.basic.api.request.GetStudentReq;
 import cn.com.qmth.examcloud.core.basic.api.response.GetStudentResp;
 import cn.com.qmth.examcloud.support.cache.CacheHelper;
 import cn.com.qmth.examcloud.support.cache.bean.OrgPropertyCacheBean;
-import okhttp3.FormBody;
-import okhttp3.MediaType;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
+import com.alibaba.fastjson.JSONObject;
+import okhttp3.*;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.Assert;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static cn.com.qmth.examcloud.app.model.Constants.*;
 
 /**
  * 认证中心业务服务接口
@@ -338,9 +323,9 @@ public class CoreAuthServiceImpl implements CoreAuthService {
         redisService.set(key, millis.toString(), 15);
     }
 
-	@Override
-	public Result register(String user_id,String client_type, String ip_address) throws Exception {
-		final String requestUrl = String.format("%s/api/ecs_core/verifyCode/register", sysProperty.getApiDomain());
+    @Override
+    public Result register(String user_id, String client_type, String ip_address) throws Exception {
+        final String requestUrl = String.format("%s/api/ecs_core/verifyCode/register", sysProperty.getApiDomain());
         Map<String, Object> params = new HashMap<>();
         params.put("user_id", user_id);
         params.put("client_type", client_type);
@@ -349,23 +334,22 @@ public class CoreAuthServiceImpl implements CoreAuthService {
         //发送短信
         Result<String> result = HttpUtils.doPost(requestUrl, formBody);
         return result;
-	}
+    }
 
-	@Override
-	public Result geetestLogin(String seccode, String validate, String challenge,
-   		 String user_id, String client_type, String ip_address,
-   		 String accountValue, String password, String accountType,
-   		Long rootOrgId) throws Exception {
+    @Override
+    public Result geetestLogin(String seccode, String validate, String challenge,
+                               String user_id, String client_type, String ip_address,
+                               String accountValue, String password, String accountType,
+                               Long rootOrgId) throws Exception {
         if (StringUtils.isBlank(accountType)) {
-        	accountType=LoginType.STUDENT_PHONE.name();
+            accountType = LoginType.STUDENT_PHONE.name();
         }
 
         //封装请求参数
         final String requestUrl = String.format("%s/api/ecs_core/verifyCode/gt/login", sysProperty.getApiDomain());
         Map<String, String> params = new HashMap<>();
-        
-        
-        
+
+
         params.put("seccode", seccode);
         params.put("validate", validate);
         params.put("challenge", challenge);
@@ -391,9 +375,14 @@ public class CoreAuthServiceImpl implements CoreAuthService {
             if (response.isSuccessful()) {
                 //获取用户信息
                 UserInfo userInfo = of(bodyStr);
+                if (userInfo == null) {
+                    log.warn("Login response is " + bodyStr);
+                    return new Result<>().error("登录失败!");
+                }
+
                 return new Result().success(userInfo);
             } else {
-                log.warn("Http response is " + bodyStr);
+                log.warn("Login response is " + bodyStr);
                 ResBody body = new JsonMapper().parseJson(bodyStr, ResBody.class);
                 if (body != null && body.getCode() != null) {
                     return new Result().error(body.getDesc());
@@ -402,22 +391,26 @@ public class CoreAuthServiceImpl implements CoreAuthService {
             }
         } catch (Exception e) {
             log.error(e.getMessage(), e);
-            return new Result().error("登录异常!");
+            return new Result().error("登录失败!");
         }
-	}
-	
-	private UserInfo of(String bodyStr) {
-		JSONObject job=JSONObject.parseObject(bodyStr);
-		JSONObject content=job.getJSONObject("content");
-		UserInfo userInfo = new UserInfo();
-		userInfo.setKey(content.getString("key"));
-		userInfo.setUserId(content.getLong("userId"));
-		userInfo.setUserType(content.getString("userType"));
-		userInfo.setDisplayName(content.getString("displayName"));
-		userInfo.setRootOrgId(content.getLong("rootOrgId"));
-		userInfo.setRootOrgName(content.getString("rootOrgName"));
-		userInfo.setToken(content.getString("token"));
-		return userInfo;
-	}
+    }
+
+    private UserInfo of(String bodyStr) {
+        JSONObject job = JSONObject.parseObject(bodyStr);
+        JSONObject content = job.getJSONObject("content");
+        if (content == null) {
+            return null;
+        }
+
+        UserInfo userInfo = new UserInfo();
+        userInfo.setKey(content.getString("key"));
+        userInfo.setUserId(content.getLong("userId"));
+        userInfo.setUserType(content.getString("userType"));
+        userInfo.setDisplayName(content.getString("displayName"));
+        userInfo.setRootOrgId(content.getLong("rootOrgId"));
+        userInfo.setRootOrgName(content.getString("rootOrgName"));
+        userInfo.setToken(content.getString("token"));
+        return userInfo;
+    }
 
 }