This commit is contained in:
高子兴 2024-07-06 07:19:31 +08:00
parent f51a66073b
commit 5747e255e9
3 changed files with 37 additions and 68 deletions

View File

@ -9,6 +9,7 @@ import java.util.stream.Collectors;
public class UserHS2User { public class UserHS2User {
public static User convert(UserHS userHS) { public static User convert(UserHS userHS) {
User user = new User(); User user = new User();
user.setId(userHS.getId());
user.setAccount(userHS.getUsername()); user.setAccount(userHS.getUsername());
user.setPassword(userHS.getPassword()); user.setPassword(userHS.getPassword());
user.setName(userHS.getNickname()); user.setName(userHS.getNickname());

View File

@ -1,39 +0,0 @@
package org.cmh.backend.UserManagement.controller;
import org.cmh.backend.UserManagement.model.User;
import org.cmh.backend.UserManagement.service.UserServiceTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import static org.springframework.http.ResponseEntity.ok;
@RestController
public class UserController {
@Autowired
private UserServiceTemp userService;
@CrossOrigin(origins = "http://localhost:5173")
@PostMapping("/checkLogin")
public ResponseEntity<User> login(@RequestBody Map<String,String> credentials) {
String account = credentials.get("account");
String password = credentials.get("password");
User user = userService.getUserByAccountAndPassword(account, password);
return ok(user);
}
@CrossOrigin(origins = "http://localhost:5173")
@PostMapping("/checkRegister")
public String register(@RequestBody User user) {
if(userService.registerUser(user) != null){
return "注册成功";
}else
return "注册错误";
}
}

View File

@ -1,22 +1,22 @@
package org.cmh.backend.UserManagement.controller; package org.cmh.backend.UserManagement.controller;
import io.jsonwebtoken.JwtParser;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.cmh.backend.OrganizationManagement.service.OrganizationService; import org.cmh.backend.OrganizationManagement.service.OrganizationService;
import org.cmh.backend.UserManagement.adpter.User2UserHS; import org.cmh.backend.UserManagement.adpter.User2UserHS;
import org.cmh.backend.UserManagement.adpter.UserHS2User; import org.cmh.backend.UserManagement.adpter.UserHS2User;
import org.cmh.backend.UserManagement.service.UserManagementService;
import org.cmh.backend.UserManagement.model.User; import org.cmh.backend.UserManagement.model.User;
import org.cmh.backend.UserManagement.service.UserManagementService;
import org.cmh.backend.Utils.JwtUtil; import org.cmh.backend.Utils.JwtUtil;
import org.cmh.backend.Utils.JwtVerify; import org.cmh.backend.Utils.JwtVerify;
import org.cmh.backend.authentication.dto.UserProfileResponse; import org.cmh.backend.authentication.dto.UserProfileResponse;
import org.cmh.backend.authentication.model.UserHS; import org.cmh.backend.authentication.model.UserHS;
import org.cmh.backend.authentication.repository.UserRepository;
import org.cmh.backend.authentication.service.UserService; import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@ -28,6 +28,8 @@ public class UserManagementController {
private OrganizationService organizationService; private OrganizationService organizationService;
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private UserRepository userRepository;
@PostMapping("/addUser") @PostMapping("/addUser")
public ResponseEntity<UserProfileResponse> addUser(@RequestBody User user) { public ResponseEntity<UserProfileResponse> addUser(@RequestBody User user) {
@ -56,42 +58,47 @@ public class UserManagementController {
public List<User> getAll(@RequestParam String token) { public List<User> getAll(@RequestParam String token) {
String username = JwtUtil.extractUsername(token); String username = JwtUtil.extractUsername(token);
UserHS userHS = userService.getUserByUsername(username); UserHS userHS = userService.getUserByUsername(username);
List<UserHS> userHSList = userService.getAllUsers();
//这里分权限进行不同请求
//超级管理员
if (userHS.getSuperAdmin()) { if (userHS.getSuperAdmin()) {
return UserHS2User.convertList(userHSList); return UserHS2User.convertList(userService.getAllUsers());
} else { } else {
return null; return UserHS2User.convertList(userService.getUsersByTenant(userHS.getTenant()));
} }
} }
@PostMapping("/update") @PostMapping("/update")
public ResponseEntity<UserProfileResponse> update(@RequestBody User user) { public ResponseEntity<UserProfileResponse> update(@RequestBody User user) {
UserHS newuser = User2UserHS.convert(user); UserHS reqUser = User2UserHS.convert(user);
UserHS tarUser = userService.getUserByUsername(reqUser.getUsername());
if (tarUser != null) {
tarUser.setNickname(reqUser.getNickname());
tarUser.setGender(reqUser.getGender());
tarUser.setPhoneNumber(reqUser.getPhoneNumber());
tarUser.setEmail(reqUser.getEmail());
tarUser.setRole(reqUser.getRole());
UserProfileResponse response = new UserProfileResponse( UserProfileResponse response = new UserProfileResponse(
newuser.getUsername(), reqUser.getUsername(),
newuser.getNickname(), reqUser.getNickname(),
newuser.getGender(), reqUser.getGender(),
newuser.getPhoneNumber(), reqUser.getPhoneNumber(),
newuser.getEmail(), reqUser.getEmail(),
newuser.getDepartment(), reqUser.getDepartment(),
newuser.getRole(), reqUser.getRole(),
newuser.getCreatedAt() reqUser.getCreatedAt()
); );
if(organizationService.getByName(newuser.getDepartment()) != null){ if (organizationService.getByName(reqUser.getDepartment()) != null) {
userService.addUser(newuser); tarUser.setDepartment(reqUser.getDepartment());
}
userRepository.save(tarUser);
return new ResponseEntity<>(response, HttpStatus.OK); return new ResponseEntity<>(response, HttpStatus.OK);
} else { } else {
return null; return new ResponseEntity<>(null, HttpStatus.OK);
} }
} }
@PostMapping("/delete") @PostMapping("/delete")
@Transactional @Transactional
//不确定这里返回值应该是什么
public void delete(@RequestBody User user) { public void delete(@RequestBody User user) {
UserHS userHS = userService.getUserByUsername(user.getName()); UserHS userHS = userService.getUserByUsername(user.getName());
if (userHS != null) { if (userHS != null) {