Procházet zdrojové kódy

登录和注册接口合二为一

詹子聪 před 5 roky
rodič
revize
ae8124ce95

+ 48 - 23
src/main/java/com/miekir/shibei/controller/api/UserController.java

@@ -43,7 +43,7 @@ public class UserController {
     HttpServletRequest request;
 
     // 每隔5分钟去清理验证码
-    @Scheduled(cron = "* 0/5 * * * *")
+    //@Scheduled(cron = "* 0/5 * * * *")
     public void clearMapTask() {
         Enumeration<String> keys = mCodeMap.keys();
         while (keys.hasMoreElements()) {
@@ -176,39 +176,64 @@ public class UserController {
     /**
      * 登录:email+加密的密码
      */
-    @RequestMapping(value = "/api/login/normal", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
+    @RequestMapping(value = "/api/loginOrRegister", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
     @ResponseBody
-    public String loginNormal(String email, String password) {
+    public String loginOrRegister(String email, String password) {
         ResponseResult<User> responseResult = new ResponseResult<User>();
         responseResult.setMessage("登录失败");
         User user = userRepository.findUserByEmail(email);
         if (user == null) {
-            responseResult.setMessage("用户不存在");
-            return JSON.toJSONString(responseResult);
-        }
+            // 用户不存在,注册
+            user = new User();
+            user.setEmail(email);
+            user.setNickName(email);
+            user.setRegisterTimeMillis(System.currentTimeMillis());
+            user.setPassword(password);
+            try {
+                String token = TokenGenerator.getInstance().getToken(email);
+                if (token == null || token.equals("")) {
+                    responseResult.setMessage("注册失败,请检查数据格式");
+                    return JSON.toJSONString(responseResult);
+                }
 
-        String encryptPassword = user.getPassword();
+                // 注册成功,生成token给用户
+                user.setToken(token);
+                userRepository.save(user);
 
-        if (!password.equals(encryptPassword)) {
-            responseResult.setMessage("请检查账号和密码");
-            return JSON.toJSONString(responseResult);
-        }
+                user.setPassword("");
+                responseResult.setCode(ResultCode.SUCCESS);
+                responseResult.setMessage("注册成功");
+                responseResult.setContent(user);
+            } catch (Exception e) {
+                responseResult.setMessage("注册失败,请检查数据格式");
+                e.printStackTrace();
+            }
 
-        // 登录成功,生成token给用户
-        String token = TokenGenerator.getInstance().getToken(email);
-        if (token == null || token.equals("")) {
-            responseResult.setMessage("登录失败,请检查数据格式");
             return JSON.toJSONString(responseResult);
-        }
-        user.setToken(token);
-        userRepository.save(user);
+        } else {
+            String encryptPassword = user.getPassword();
 
-        user.setPassword("");
-        responseResult.setCode(ResultCode.SUCCESS);
-        responseResult.setMessage("登录成功");
-        responseResult.setContent(user);
+            if (!password.equals(encryptPassword)) {
+                responseResult.setMessage("请检查账号和密码");
+                return JSON.toJSONString(responseResult);
+            }
 
-        return JSON.toJSONString(responseResult);
+            // 登录成功,生成token给用户
+            String token = TokenGenerator.getInstance().getToken(email);
+            if (token == null || token.equals("")) {
+                responseResult.setMessage("登录失败,请检查数据格式");
+                return JSON.toJSONString(responseResult);
+            }
+            user.setToken(token);
+            userRepository.save(user);
+
+            user.setPassword("");
+            responseResult.setCode(ResultCode.SUCCESS);
+            responseResult.setMessage("登录成功");
+            responseResult.setContent(user);
+
+            return JSON.toJSONString(responseResult);
+        }
     }
 
     /**