|
@@ -4,10 +4,7 @@ import cn.minbb.edu.model.User;
|
|
|
import cn.minbb.edu.service.UserService;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.HashMap;
|
|
@@ -17,8 +14,12 @@ import java.util.Map;
|
|
|
@RequestMapping(value = "/user")
|
|
|
public class UserController {
|
|
|
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
@Autowired
|
|
|
- UserService userService;
|
|
|
+ public UserController(UserService userService) {
|
|
|
+ this.userService = userService;
|
|
|
+ }
|
|
|
|
|
|
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
|
public String login(
|
|
@@ -45,4 +46,36 @@ public class UserController {
|
|
|
}
|
|
|
return JSONObject.toJSONString(result);
|
|
|
}
|
|
|
+
|
|
|
+ @RequestMapping(value = "/register", method = RequestMethod.POST)
|
|
|
+ public String register(@RequestBody String userJSON, HttpServletRequest request) {
|
|
|
+ User userClient = JSONObject.parseObject(userJSON, User.class);
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ if (userClient != null) {
|
|
|
+ String username = userClient.getUsername();
|
|
|
+ if (userService.findUserByUsername(username) == null) {
|
|
|
+ // 用户不存在 - 可以注册
|
|
|
+ User user = userService.save(new User(username, userClient.getPassword()));
|
|
|
+ if (user != null) {
|
|
|
+ // 注册成功
|
|
|
+ result.put("result", 200);
|
|
|
+ result.put("summary", "注册成功");
|
|
|
+ result.put("data", user);
|
|
|
+ } else {
|
|
|
+ // 注册失败
|
|
|
+ result.put("result", 404);
|
|
|
+ result.put("summary", "注册失败");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 用户存在 - 不可以注册
|
|
|
+ result.put("result", 404);
|
|
|
+ result.put("summary", "用户已存在");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 错误的注册请求
|
|
|
+ result.put("result", 404);
|
|
|
+ result.put("summary", "错误的注册请求");
|
|
|
+ }
|
|
|
+ return JSONObject.toJSONString(result);
|
|
|
+ }
|
|
|
}
|