From 185b8b43483229eb500f33b4c6522ad85a4fceb1 Mon Sep 17 00:00:00 2001 From: YangZX1428 <60736007+YangZX1428@users.noreply.github.com> Date: Sat, 27 Mar 2021 21:36:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=8E=A7=E5=88=B6=E5=99=A8?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E8=AE=BE=E8=AE=A1=E4=BB=A5=E5=8F=8A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=BC=A0=E9=80=81=E6=A8=A1=E5=9E=8B=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/AuthController.java | 79 +++++++++++++++++ .../demo/controller/DefaultController.java | 84 +++++++++++++++++++ .../controller/NotificationController.java | 76 +++++++++++++++++ .../com/eepractice/demo/forms/AuthForm.java | 17 ++++ .../com/eepractice/demo/forms/InfoForm.java | 19 +++++ .../demo/forms/NotificationForm.java | 18 ++++ 6 files changed, 293 insertions(+) create mode 100644 web_backend/src/main/java/com/eepractice/demo/controller/AuthController.java create mode 100644 web_backend/src/main/java/com/eepractice/demo/controller/DefaultController.java create mode 100644 web_backend/src/main/java/com/eepractice/demo/controller/NotificationController.java create mode 100644 web_backend/src/main/java/com/eepractice/demo/forms/AuthForm.java create mode 100644 web_backend/src/main/java/com/eepractice/demo/forms/InfoForm.java create mode 100644 web_backend/src/main/java/com/eepractice/demo/forms/NotificationForm.java diff --git a/web_backend/src/main/java/com/eepractice/demo/controller/AuthController.java b/web_backend/src/main/java/com/eepractice/demo/controller/AuthController.java new file mode 100644 index 0000000..4e871e7 --- /dev/null +++ b/web_backend/src/main/java/com/eepractice/demo/controller/AuthController.java @@ -0,0 +1,79 @@ +package com.eepractice.demo.controller; + +import com.eepractice.demo.bean.AuthResponse; +import com.eepractice.demo.bean.User; +import com.eepractice.demo.forms.AuthForm; +import com.eepractice.demo.properties.CodeProperty; +import com.eepractice.demo.service.AuthServiceImpl; +import com.eepractice.demo.utils.CommonUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@RestController +@RequestMapping("/auth") +@CrossOrigin(origins = "*") +public class AuthController { + + + @Autowired + CodeProperty codeProperty; + + + @Autowired + AuthServiceImpl authService; + + @Autowired + CommonUtils commonUtils; + + /** + * 处理登录的控制器 + * @param authForm + * @return + */ + @PostMapping(value = "/login",produces = "application/json") + public ResponseEntity processLogin(@RequestBody AuthForm authForm){ + + System.out.println(authForm); + // authForm为登录数据表单 + Map responseMap = new HashMap<>(); + // 用户名或密码为空 + if(authForm.empty()){ + responseMap.put("result", new AuthResponse(null,codeProperty.PARAMETER_ERROR,"参数错误",null)); + }else{ + Optional userOptional = authService.login(authForm.getUsername(), authForm.getPassword(),authForm.getRole()); + // 登录失败 + if (!userOptional.isPresent()){ + responseMap.put("result",new AuthResponse(null,codeProperty.LOGIN_FAILED,"用户名不存在或密码错误",null)); + }else{ + // 登录成功 + User user = userOptional.get(); + //生成token + responseMap.put("result",new AuthResponse(user,codeProperty.SUCCESS,"登录成功!", commonUtils.generateJwtToken(user))); + } + } + return ResponseEntity.ok(responseMap); + } + + + @PostMapping(value = "/register",produces = "application/json") + public ResponseEntity processRegister(@RequestBody AuthForm authForm){ + // authForm为登录数据表单 + Map responseMap = new HashMap<>(); + // 用户名或密码为空 + if(authForm.empty()){ + responseMap.put("result", new AuthResponse(null,codeProperty.PARAMETER_ERROR,"参数错误",null)); + }else{ + boolean registerResult = authService.register(authForm.getUsername(), authForm.getPassword()); + if (!registerResult) responseMap.put("result",new AuthResponse(null,codeProperty.REGISTER_FAILED,"注册失败",null)); + else{ + responseMap.put("result",new AuthResponse(null,codeProperty.SUCCESS,"注册成功",null)); + } + } + return ResponseEntity.ok(responseMap); + } +} diff --git a/web_backend/src/main/java/com/eepractice/demo/controller/DefaultController.java b/web_backend/src/main/java/com/eepractice/demo/controller/DefaultController.java new file mode 100644 index 0000000..e52b871 --- /dev/null +++ b/web_backend/src/main/java/com/eepractice/demo/controller/DefaultController.java @@ -0,0 +1,84 @@ +package com.eepractice.demo.controller; + +import com.eepractice.demo.bean.CommonResponse; +import com.eepractice.demo.bean.Notification; +import com.eepractice.demo.bean.SubForum; +import com.eepractice.demo.bean.User; +import com.eepractice.demo.forms.InfoForm; +import com.eepractice.demo.properties.CodeProperty; +import com.eepractice.demo.properties.RoleProperty; +import com.eepractice.demo.repository.NotificationRepository; +import com.eepractice.demo.repository.SubForumRepository; +import com.eepractice.demo.utils.CommonUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.*; + +@RestController +@RequestMapping("/default") +@CrossOrigin(origins = "*") +public class DefaultController { + + @Autowired + CodeProperty codeProperty; + + @Autowired + RoleProperty roleProperty; + + @Autowired + SubForumRepository subForumRepository; + + @Autowired + CommonUtils commonUtils; + + @Autowired + NotificationRepository notificationRepository; + /** + * 获取首页需要的数据 + * @return + */ + + @GetMapping(value = "/info",produces = "application/json") + public ResponseEntity processInfo(){ + List infoFormList = new ArrayList<>(); + Map responseMap = new HashMap<>(); + // 分论坛信息 + Iterable subForums = subForumRepository.findAll(); + for (SubForum subForum : subForums) { + infoFormList.add(commonUtils.transformSubForum(subForum)); + } + responseMap.put("data",infoFormList); + return ResponseEntity.ok(responseMap); + } + + + /** + * 发布通知,只有主席或秘书才能发布 + */ + @PostMapping(value = "/add/{id}",consumes = "application/json") + public ResponseEntity processAddNotification(@RequestBody Notification notification, + @RequestHeader("Authorization") String token, + @PathVariable("id") Integer id) { + String newToken = token.replaceAll("Basic ",""); + // 用户验证信息无效 + User user = commonUtils.parseJwtToken(newToken); + System.out.println(user.getId()); + if (user == null) return CommonResponse.getMap(codeProperty.TOKEN_INVALID,"用户验证信息无效",null); + SubForum subForum = subForumRepository.findById(id).get(); + // 只有分论坛的主席和秘书能发布通知 + if (user.getId().equals(subForum.getChairManId()) || user.getId().equals(subForum.getSecretaryId())) { + // 用户有效,发布通知 + notification.setSubForumId(id); + List members = subForum.getMembers(); + String unreadUsers = commonUtils.parseUserList(members,subForum.getChairManId(),subForum.getSecretaryId()); + notification.setUnreadUsers(unreadUsers); + notificationRepository.save(notification); + return CommonResponse.getMap(codeProperty.SUCCESS,"发布成功!",null); + } + return CommonResponse.getMap(codeProperty.NO_PERMISSIONS,"用户权限不足",null); + } + + +} diff --git a/web_backend/src/main/java/com/eepractice/demo/controller/NotificationController.java b/web_backend/src/main/java/com/eepractice/demo/controller/NotificationController.java new file mode 100644 index 0000000..803329c --- /dev/null +++ b/web_backend/src/main/java/com/eepractice/demo/controller/NotificationController.java @@ -0,0 +1,76 @@ +package com.eepractice.demo.controller; + +import com.eepractice.demo.bean.CommonResponse; +import com.eepractice.demo.bean.Notification; +import com.eepractice.demo.bean.SubForum; +import com.eepractice.demo.bean.User; +import com.eepractice.demo.forms.NotificationForm; +import com.eepractice.demo.properties.CodeProperty; +import com.eepractice.demo.repository.NotificationRepository; +import com.eepractice.demo.repository.SubForumRepository; +import com.eepractice.demo.repository.UserRepository; +import com.eepractice.demo.utils.CommonUtils; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/note") +@CrossOrigin(origins = "*") +public class NotificationController { + + + @Autowired + NotificationRepository notificationRepository; + + @Autowired + UserRepository userRepository; + + @Autowired + SubForumRepository subForumRepository; + + @Autowired + CommonUtils commonUtils; + + @Autowired + CodeProperty codeProperty; + + /** + * 接受用户id查看是否有通知 + * @return + */ + @GetMapping(value = "/has_note/{userId}",produces = "application/json") + public ResponseEntity processHasNote(@PathVariable("userId") Integer userId){ + String uId = String.valueOf(userId); + List notifications = notificationRepository.findNotificationsByUnreadUsersContains(uId); + List resultList = new ArrayList<>(); + for (Notification notification : notifications) { + NotificationForm notificationForm = new NotificationForm(); + notificationForm.setTitle(notification.getTitle()).setContent(notification.getContent()); + SubForum subForum = subForumRepository.findById(notification.getSubForumId()).get(); + User chairman = userRepository.findById(subForum.getId()).get(); + notificationForm.setBelongSubForum(subForum.getTitle()).setBelongChairMan(chairman.getUsername()) + .setId(notification.getId()); + resultList.add(notificationForm); + } + return ResponseEntity.ok(resultList); + } + + + @GetMapping(value = "/read/{noteId}",produces = "application/json") + public ResponseEntity processReadNote(@PathVariable("noteId") Integer noteId,@RequestHeader("Authorization") String token){ + User user = commonUtils.parseJwtToken(token); + if (user == null) return CommonResponse.getMap(codeProperty.TOKEN_INVALID,"用户验证信息无效",null); + // 该用户已读通知 + Notification notification = notificationRepository.findById(noteId).get(); + notification.handleMsg(user.getId()); + notificationRepository.save(notification); + return CommonResponse.getMap(codeProperty.SUCCESS,"操作成功!",null); + } + +} diff --git a/web_backend/src/main/java/com/eepractice/demo/forms/AuthForm.java b/web_backend/src/main/java/com/eepractice/demo/forms/AuthForm.java new file mode 100644 index 0000000..383eb85 --- /dev/null +++ b/web_backend/src/main/java/com/eepractice/demo/forms/AuthForm.java @@ -0,0 +1,17 @@ +package com.eepractice.demo.forms; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class AuthForm { + private String username; + private String password; + private String role; + public boolean empty(){ + return this.username.trim().equals("") || this.password.trim().equals("") || this.role.equals(""); + } +} diff --git a/web_backend/src/main/java/com/eepractice/demo/forms/InfoForm.java b/web_backend/src/main/java/com/eepractice/demo/forms/InfoForm.java new file mode 100644 index 0000000..89318ba --- /dev/null +++ b/web_backend/src/main/java/com/eepractice/demo/forms/InfoForm.java @@ -0,0 +1,19 @@ +package com.eepractice.demo.forms; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Accessors(chain = true) +public class InfoForm { + private Integer id; + private String title; + private String chairman; + private String secretary; + private Integer memberNum; + private String startDate; +} diff --git a/web_backend/src/main/java/com/eepractice/demo/forms/NotificationForm.java b/web_backend/src/main/java/com/eepractice/demo/forms/NotificationForm.java new file mode 100644 index 0000000..c545aef --- /dev/null +++ b/web_backend/src/main/java/com/eepractice/demo/forms/NotificationForm.java @@ -0,0 +1,18 @@ +package com.eepractice.demo.forms; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Accessors(chain = true) +public class NotificationForm { + Integer id; + String title; + String content; + String belongSubForum; + String belongChairMan; +}