浏览代码

update login api.

deason 5 年之前
父节点
当前提交
9b77e09000

+ 34 - 4
src/main/java/cn/com/qmth/examcloud/app/core/config/TokenFilter.java

@@ -7,10 +7,7 @@
 
 package cn.com.qmth.examcloud.app.core.config;
 
-import cn.com.qmth.examcloud.app.model.Constants;
-import cn.com.qmth.examcloud.app.model.LoginInfo;
-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 org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -88,6 +85,11 @@ public class TokenFilter implements Filter {
                 //ignore
             }
 
+            //先获取学校ID,下面“检查考试状态接口”必须带学校ID
+            if (rootOrgId == null) {
+                rootOrgId = this.loadRootOrgId(request);
+            }
+
             boolean isDoing = authService.isDoingExam(rootOrgId, accountType, account);
             log.info("[Check Doing Exam] Result is " + isDoing);
             if (isDoing) {
@@ -149,6 +151,34 @@ public class TokenFilter implements Filter {
         return customRequest;
     }
 
+    private Long loadRootOrgId(HttpServletRequest request) {
+        String accountType;
+        if (request.getServletPath().contains("/user/verify")) {
+            accountType = LoginType.STUDENT_PHONE.name();
+        } else {
+            accountType = request.getParameter("accountType");
+        }
+
+        String account = request.getParameter("account");
+        String password = request.getParameter("password");
+        String smsCode = request.getParameter("smsCode");
+        String domain = request.getParameter("domain");
+
+        LoginInfo loginInfo = new LoginInfo();
+        loginInfo.setNoSession(true);
+        loginInfo.setAccountType(accountType);
+        loginInfo.setAccount(account);
+        loginInfo.setPassword(password);
+        loginInfo.setSmsCode(smsCode);
+        loginInfo.setDomain(domain);
+
+        Result<UserInfo> result = authService.login(loginInfo);
+        if (result.isSuccess() && result.getData() != null) {
+            return result.getData().getRootOrgId();
+        }
+        return null;
+    }
+
     private void reLogin(LoginInfo loginInfo) {
         try {
             Result<UserInfo> result = authService.login(loginInfo);

+ 9 - 0
src/main/java/cn/com/qmth/examcloud/app/model/LoginInfo.java

@@ -32,6 +32,7 @@ public class LoginInfo implements Serializable {
     private String deviceId;
     private Date createTime;
     private String smsCode;
+    private Boolean noSession;
 
     public LoginInfo(String account, String password, String accountType, Long rootOrgId, String domain, String key, String token, String deviceId) {
         this.account = account;
@@ -182,4 +183,12 @@ public class LoginInfo implements Serializable {
         this.smsCode = smsCode;
     }
 
+    public Boolean getNoSession() {
+        return noSession;
+    }
+
+    public void setNoSession(Boolean noSession) {
+        this.noSession = noSession;
+    }
+
 }

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/app/service/CoreAuthService.java

@@ -31,7 +31,7 @@ public interface CoreAuthService {
      * @return
      * @throws Exception
      */
-    Result<UserInfo> login(LoginInfo loginInfo) throws Exception;
+    Result<UserInfo> login(LoginInfo loginInfo);
 
     /**
      * 用户退出登录

+ 25 - 16
src/main/java/cn/com/qmth/examcloud/app/service/impl/CoreAuthServiceImpl.java

@@ -48,11 +48,12 @@ public class CoreAuthServiceImpl implements CoreAuthService {
     public boolean isDoingExam(Long rootOrgId, String accountType, String account) {
         log.warn(String.format("[Check Doing Exam] rootOrgId=%s, account=%s, accountType=%s", rootOrgId, account, accountType));
 
-        Map<String, String> params = new HashMap<>();
-        if (rootOrgId != null) {
-            params.put("rootOrgId", rootOrgId.toString());
+        if (rootOrgId == null) {
+            throw new StatusException("400", "学校ID为不能空!");
         }
 
+        Map<String, String> params = new HashMap<>();
+        params.put("rootOrgId", rootOrgId.toString());
         if (LoginType.STUDENT_CODE.name().equals(accountType)) {
             params.put("studentCode", account);
         } else if (LoginType.STUDENT_IDENTITY_NUMBER.name().equals(accountType)) {
@@ -84,11 +85,12 @@ public class CoreAuthServiceImpl implements CoreAuthService {
         } catch (Exception e) {
             log.error(e.getMessage(), e);
         }
+
         throw new StatusException("500", "考试状态检查异常!");
     }
 
     @Override
-    public Result<UserInfo> login(LoginInfo loginInfo) throws Exception {
+    public Result<UserInfo> login(LoginInfo loginInfo) {
         Assert.notNull(loginInfo, "LoginInfo must be not null.");
         if (StringUtils.isBlank(loginInfo.getAccountType())) {
             loginInfo.setAccountType(LoginType.STUDENT_PHONE.name());
@@ -103,6 +105,9 @@ public class CoreAuthServiceImpl implements CoreAuthService {
         params.put("rootOrgId", loginInfo.getRootOrgId() != null ? loginInfo.getRootOrgId().toString() : "");
         params.put("domain", loginInfo.getDomain());
         params.put("smsCode", loginInfo.getSmsCode());
+        if (loginInfo.getNoSession() != null && loginInfo.getNoSession()) {
+            params.put("noSession", loginInfo.getNoSession().toString());
+        }
         String json = new JsonMapper().toJson(params);
 
         RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json);
@@ -112,19 +117,23 @@ public class CoreAuthServiceImpl implements CoreAuthService {
                 .build();
 
         //执行请求
-        Response response = HttpClientBuilder.getClient().newCall(request).execute();
-        String bodyStr = response.body().string();
-        if (response.isSuccessful()) {
-            //获取用户信息
-            UserInfo userInfo = new JsonMapper().fromJson(bodyStr, UserInfo.class);
-            return new Result().success(userInfo);
-        } else {
-            log.warn("Http response is " + bodyStr);
-            ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
-            if (body != null && body.getCode() != null) {
-                return new Result().error(body.getDesc());
+        try (Response response = HttpClientBuilder.getClient().newCall(request).execute();) {
+            String bodyStr = response.body().string();
+            if (response.isSuccessful()) {
+                //获取用户信息
+                UserInfo userInfo = new JsonMapper().fromJson(bodyStr, UserInfo.class);
+                return new Result().success(userInfo);
+            } else {
+                log.warn("Http response is " + bodyStr);
+                ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
+                if (body != null && body.getCode() != null) {
+                    return new Result().error(body.getDesc());
+                }
+                return new Result().error(bodyStr);
             }
-            return new Result().error(bodyStr);
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            throw new StatusException("500", "登录异常!");
         }
     }