deason 5 years ago
parent
commit
4900373d8d

+ 24 - 8
src/main/java/cn/com/qmth/examcloud/app/controller/UserAuthRestController.java

@@ -11,8 +11,8 @@ import cn.com.qmth.examcloud.app.model.LoginInfo;
 import cn.com.qmth.examcloud.app.model.LoginType;
 import cn.com.qmth.examcloud.app.model.Result;
 import cn.com.qmth.examcloud.app.model.UserInfo;
-import cn.com.qmth.examcloud.app.service.CoreBasicService;
 import cn.com.qmth.examcloud.app.service.CoreAuthService;
+import cn.com.qmth.examcloud.app.service.CoreBasicService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.slf4j.Logger;
@@ -26,7 +26,6 @@ import static cn.com.qmth.examcloud.app.model.Constants.PARAM_APP_TOKEN;
 /**
  * 认证中心相关接口
  *
- * @version v1.0
  * @author: fengdesheng
  * @since: 2018/7/16
  */
@@ -40,41 +39,58 @@ public class UserAuthRestController {
     @Autowired
     private CoreBasicService basicService;
 
-    @ApiOperation(value = "登录接口", notes = "参数accountType值说明:学生身份证号类型=STUDENT_IDENTITY_NUMBER,学生学号类型=STUDENT_CODE,学生手机号类型=STUDENT_PHONE")
+    @ApiOperation(value = "登录接口", notes = "参数accountType值:STUDENT_IDENTITY_NUMBER、STUDENT_CODE、STUDENT_PHONE")
     @RequestMapping(value = "/user/login", method = {RequestMethod.POST})
-    public Result<UserInfo> login(@RequestParam String account, @RequestParam String password, @RequestParam String accountType, @RequestParam(required = false) Long rootOrgId,
-                                  @RequestParam(required = false) String domain, @RequestHeader String deviceId) throws Exception {
+    public Result<UserInfo> login(@RequestParam String account,
+                                  @RequestParam String password,
+                                  @RequestParam String accountType,
+                                  @RequestParam(required = false) Long rootOrgId,
+                                  @RequestParam(required = false) String domain,
+                                  @RequestHeader String deviceId) throws Exception {
         LoginInfo loginInfo = new LoginInfo(account, password, accountType, rootOrgId, domain, deviceId, null);
         Result<UserInfo> result = authService.login(loginInfo);
+
         if (result.isSuccess() && result.getData() != null) {
             //登录成功后缓存Token信息
             UserInfo userInfo = result.getData();
+            loginInfo.setUserId(userInfo.getUserId());
+            loginInfo.setRootOrgId(userInfo.getRootOrgId());
             loginInfo.setUserName(userInfo.getDisplayName());
             loginInfo.setKey(userInfo.getKey());
             loginInfo.setToken(userInfo.getToken());
             loginInfo.setAppToken(userInfo.getToken());
+
             authService.cacheLoginInfo(loginInfo, userInfo.getKey());
             log.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken()));
         }
+
         return result;
     }
 
-    @ApiOperation(value = "验证码接口", notes = "参数accountType值说明:学生身份证号类型=STUDENT_IDENTITY_NUMBER,学生学号类型=STUDENT_CODE,学生手机号类型=STUDENT_PHONE")
+    @ApiOperation(value = "验证码登录接口")
     @RequestMapping(value = "/user/verify", method = {RequestMethod.POST})
-    public Result<UserInfo> verifyLogin(@RequestParam String account, @RequestParam String smsCode, @RequestParam(required = false) Long rootOrgId,
-                                        @RequestParam(required = false) String domain, @RequestHeader String deviceId) throws Exception {
+    public Result<UserInfo> verifyLogin(@RequestParam String account,
+                                        @RequestParam String smsCode,
+                                        @RequestParam(required = false) Long rootOrgId,
+                                        @RequestParam(required = false) String domain,
+                                        @RequestHeader String deviceId) throws Exception {
         LoginInfo loginInfo = new LoginInfo(account, null, LoginType.STUDENT_PHONE.name(), rootOrgId, domain, deviceId, smsCode);
         Result<UserInfo> result = authService.login(loginInfo);
+
         if (result.isSuccess() && result.getData() != null) {
             //登录成功后缓存Token信息
             UserInfo userInfo = result.getData();
+            loginInfo.setUserId(userInfo.getUserId());
+            loginInfo.setRootOrgId(userInfo.getRootOrgId());
             loginInfo.setUserName(userInfo.getDisplayName());
             loginInfo.setToken(userInfo.getToken());
             loginInfo.setKey(userInfo.getKey());
             loginInfo.setAppToken(userInfo.getToken());
+
             authService.cacheLoginInfo(loginInfo, userInfo.getKey());
             log.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken()));
         }
+
         return result;
     }
 

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/app/model/Constants.java

@@ -24,7 +24,7 @@ public interface Constants {
     /**
      * APP端用户登录信息的redis key前缀
      */
-    String APP_SESSION_USER_KEY_PREFIX = "appSessionUser:";
+    String APP_SESSION_USER_KEY_PREFIX = "APP:SESSION_";
 
     /**
      * 平台端的Token默认过期时间(秒)

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

@@ -19,6 +19,7 @@ import java.util.Date;
  */
 public class LoginInfo implements Serializable {
     private static final long serialVersionUID = 1L;
+    private Long userId;
     private String userName;
     private String account;
     private String password;
@@ -63,17 +64,28 @@ public class LoginInfo implements Serializable {
         if (createTime == null) {
             return true;
         }
+
         Calendar c = Calendar.getInstance();
         c.setTime(createTime);
         c.add(Calendar.SECOND, seconds - 60);
         //System.out.println(DateUtils.format(c.getTime()));
+
         //判断是否在n小时内
         if (c.getTime().after(new Date())) {
             return false;
         }
+
         return true;
     }
 
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
     public String getUserName() {
         return userName;
     }

+ 6 - 0
src/main/java/cn/com/qmth/examcloud/app/service/impl/CoreAuthServiceImpl.java

@@ -72,6 +72,7 @@ public class CoreAuthServiceImpl implements CoreAuthService {
                     .url(requestUrl)
                     .build();
             Response response = HttpClientBuilder.getClient().newCall(request).execute();
+
             String bodyStr = response.body().string();
             if (response.isSuccessful()) {
                 ExamInfo info = new JsonMapper().fromJson(bodyStr, ExamInfo.class);
@@ -91,6 +92,7 @@ public class CoreAuthServiceImpl implements CoreAuthService {
         if (StringUtils.isBlank(loginInfo.getAccountType())) {
             loginInfo.setAccountType(LoginType.STUDENT_PHONE.name());
         }
+
         //封装请求参数
         final String requestUrl = String.format("%s/api/ecs_core/auth/login", sysProperty.getCoreBasicUrl());
         Map<String, String> params = new HashMap<>();
@@ -101,11 +103,13 @@ public class CoreAuthServiceImpl implements CoreAuthService {
         params.put("domain", loginInfo.getDomain());
         params.put("smsCode", loginInfo.getSmsCode());
         String json = new JsonMapper().toJson(params);
+
         RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json);
         Request request = new Request.Builder()
                 .url(requestUrl)
                 .post(formBody)
                 .build();
+
         //执行请求
         Response response = HttpClientBuilder.getClient().newCall(request).execute();
         String bodyStr = response.body().string();
@@ -131,8 +135,10 @@ public class CoreAuthServiceImpl implements CoreAuthService {
                 .add(PARAM_KEY, key)
                 .add(PARAM_TOKEN, token)
                 .build();
+
         //清除缓存用户登录信息
         this.removeCacheLoginInfo(key);
+
         //退出登录
         return HttpUtils.doPost(requestUrl, formBody, key, token);
     }