|
@@ -1,19 +1,25 @@
|
|
|
package cn.minbb.evaluationsystemserver.controller;
|
|
|
|
|
|
+import cn.minbb.evaluationsystemserver.entity.Logs;
|
|
|
import cn.minbb.evaluationsystemserver.entity.User;
|
|
|
+import cn.minbb.evaluationsystemserver.service.LogsService;
|
|
|
import cn.minbb.evaluationsystemserver.service.UserService;
|
|
|
import cn.minbb.evaluationsystemserver.stroage.StorageService;
|
|
|
+import cn.minbb.evaluationsystemserver.util.IPUtils;
|
|
|
import cn.minbb.evaluationsystemserver.util.ResponseMap;
|
|
|
import cn.minbb.evaluationsystemserver.util.Result;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
@Controller
|
|
|
@RequestMapping("/user")
|
|
|
public class UserController {
|
|
@@ -23,11 +29,13 @@ public class UserController {
|
|
|
|
|
|
private final UserService userService;
|
|
|
private final StorageService storageService;
|
|
|
+ private final LogsService logsService;
|
|
|
|
|
|
@Autowired
|
|
|
- public UserController(UserService userService, StorageService storageService) {
|
|
|
+ public UserController(UserService userService, StorageService storageService, LogsService logsService) {
|
|
|
this.userService = userService;
|
|
|
this.storageService = storageService;
|
|
|
+ this.logsService = logsService;
|
|
|
}
|
|
|
|
|
|
@GetMapping(value = "/login")
|
|
@@ -39,15 +47,16 @@ public class UserController {
|
|
|
/**
|
|
|
* 用户注册
|
|
|
*
|
|
|
+ * @param userJSON
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
@PostMapping(value = "/register", produces = "text/plain; charset=UTF-8")
|
|
|
@ResponseBody
|
|
|
- public String register(@RequestBody String request) {
|
|
|
+ public String register(@RequestBody String userJSON, HttpServletRequest request) {
|
|
|
ResponseMap responseMap = new ResponseMap();
|
|
|
try {
|
|
|
- User user = JSONObject.parseObject(request, User.class);
|
|
|
+ User user = JSONObject.parseObject(userJSON, User.class);
|
|
|
if (user != null) {
|
|
|
if (userService.findUserByUsername(user.getUsername()) == null) {
|
|
|
User u = userService.save(user);
|
|
@@ -56,18 +65,21 @@ public class UserController {
|
|
|
responseMap.putResult(Result.SUCCESS);
|
|
|
responseMap.putSummary("注册成功");
|
|
|
responseMap.putData(u);
|
|
|
+ logsService.save(new Logs("用户注册", "注册成功", user, IPUtils.getIPAddress(request)));
|
|
|
} else {
|
|
|
// 注册失败
|
|
|
responseMap.putResult(Result.FAILED);
|
|
|
responseMap.putSummary("注册失败");
|
|
|
+ logsService.save(new Logs("用户注册", "注册失败,用户未成功保存", user, IPUtils.getIPAddress(request)));
|
|
|
}
|
|
|
} else {
|
|
|
// 用户名已存在
|
|
|
responseMap.putResult(Result.FAILED);
|
|
|
responseMap.putSummary("用户名已存在");
|
|
|
+ logsService.save(new Logs("用户注册", "注册失败,用户名已存在", user, IPUtils.getIPAddress(request)));
|
|
|
}
|
|
|
} else {
|
|
|
- System.out.println("错误注册请求 = " + request);
|
|
|
+ logsService.save(new Logs("用户注册", "错误的注册请求 = " + userJSON, null, IPUtils.getIPAddress(request)));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -80,11 +92,15 @@ public class UserController {
|
|
|
*
|
|
|
* @param username 用户名
|
|
|
* @param password 密码
|
|
|
+ * @param request
|
|
|
* @return
|
|
|
*/
|
|
|
@PostMapping(value = "/login", produces = "text/plain; charset=UTF-8")
|
|
|
@ResponseBody
|
|
|
- public String login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
|
|
|
+ public String login(
|
|
|
+ @RequestParam(value = "username") String username,
|
|
|
+ @RequestParam(value = "password") String password,
|
|
|
+ HttpServletRequest request) {
|
|
|
User user = userService.findUserByUsername(username);
|
|
|
ResponseMap responseMap = new ResponseMap();
|
|
|
if (user != null) {
|
|
@@ -100,25 +116,27 @@ public class UserController {
|
|
|
user.setAvatar(USER_AVATAR_URL + userAvatar);
|
|
|
}
|
|
|
responseMap.putData(user);
|
|
|
+ logsService.save(new Logs("用户登录", "登陆成功", user, IPUtils.getIPAddress(request)));
|
|
|
} else {
|
|
|
// 密码错误
|
|
|
responseMap.putResult(Result.FAILED);
|
|
|
responseMap.putSummary("密码错误");
|
|
|
+ logsService.save(new Logs("用户登录", "登陆失败,密码错误", user, IPUtils.getIPAddress(request)));
|
|
|
}
|
|
|
} else {
|
|
|
// 此用户名的用户不存在
|
|
|
responseMap.putResult(Result.FAILED);
|
|
|
responseMap.putSummary("用户不存在");
|
|
|
+ logsService.save(new Logs("用户登录", "登陆失败,用户名不存在", null, IPUtils.getIPAddress(request)));
|
|
|
}
|
|
|
return JSONObject.toJSONString(responseMap.getMap());
|
|
|
}
|
|
|
|
|
|
- @GetMapping(value = "/avatars/{filename:.+}")
|
|
|
- @ResponseBody
|
|
|
+ @GetMapping(value = "/avatars/{filename:.+}/download")
|
|
|
public ResponseEntity<Resource> serveAvatars(@PathVariable String filename) {
|
|
|
Resource file = storageService.loadAvatarsAsResource(filename);
|
|
|
- // return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"").body(file);
|
|
|
- return ResponseEntity.ok().body(file);
|
|
|
+ return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"").body(file);
|
|
|
+ // return ResponseEntity.ok().body(file);
|
|
|
}
|
|
|
|
|
|
@PostMapping(value = "/alter/avatar", produces = "text/plain; charset=UTF-8")
|
|
@@ -126,8 +144,8 @@ public class UserController {
|
|
|
public String alterUserAvatar(
|
|
|
@RequestParam(value = "username") String username,
|
|
|
@RequestParam(value = "password") String password,
|
|
|
- @RequestParam(value = "avatar", required = true) MultipartFile avatar) {
|
|
|
- System.out.println(username + " / " + password);
|
|
|
+ @RequestParam(value = "avatar", required = true) MultipartFile avatar,
|
|
|
+ HttpServletRequest request) {
|
|
|
User user = userService.findUserByUsername(username);
|
|
|
ResponseMap responseMap = new ResponseMap();
|
|
|
if (user != null) {
|
|
@@ -148,15 +166,18 @@ public class UserController {
|
|
|
user.setAvatar(USER_AVATAR_URL + userAvatar);
|
|
|
}
|
|
|
responseMap.putData(user);
|
|
|
+ logsService.save(new Logs("用户修改头像", "修改成功,新头像地址 = " + user.getAvatar(), user, IPUtils.getIPAddress(request)));
|
|
|
} else {
|
|
|
// 密码错误
|
|
|
responseMap.putResult(Result.FAILED);
|
|
|
responseMap.putSummary("密码错误");
|
|
|
+ logsService.save(new Logs("用户修改头像", "修改失败,密码错误", user, IPUtils.getIPAddress(request)));
|
|
|
}
|
|
|
} else {
|
|
|
// 此用户名的用户不存在
|
|
|
responseMap.putResult(Result.FAILED);
|
|
|
responseMap.putSummary("用户不存在");
|
|
|
+ logsService.save(new Logs("用户修改头像", "修改失败,用户名不存在", null, IPUtils.getIPAddress(request)));
|
|
|
}
|
|
|
return JSONObject.toJSONString(responseMap.getMap());
|
|
|
}
|