Compare commits

..

9 Commits

141 changed files with 206 additions and 427 deletions

View File

@ -1,109 +0,0 @@
package org.cmh.backend.Authentication.controller;
import org.cmh.backend.Utils.JwtUtil;
import org.cmh.backend.Utils.JwtVerify;
import org.cmh.backend.Authentication.dto.*;
import org.cmh.backend.Authentication.model.UserHS;
import org.cmh.backend.Authentication.service.UserService;
import org.cmh.backend.Authentication.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
class AuthenticationController {
@Autowired
private UserService userService;
@Autowired
private VerificationCodeService verificationCodeService;
@GetMapping("/hello")
public String hello() {
return "Hello SpringBoot!";
}
@PostMapping("/register")
public ResponseEntity<RegisterResponse> register(@RequestBody RegisterRequest request) {
try {
boolean isRegistered = userService.registerUser(request);
if (isRegistered) {
return new ResponseEntity<>(new RegisterResponse("注册成功"), HttpStatus.OK);
} else {
return new ResponseEntity<>(new RegisterResponse("注册失败:用户已存在"), HttpStatus.BAD_REQUEST);
}
} catch (IllegalArgumentException e) {
return new ResponseEntity<>(new RegisterResponse("注册失败:输入格式有误"), HttpStatus.BAD_REQUEST);
} catch (Exception e) {
return new ResponseEntity<>(new RegisterResponse("注册失败:服务器错误"), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/login")
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
boolean isValidUser = userService.loginUser(loginRequest.getUsername(), loginRequest.getPassword());
if (isValidUser) {
return new ResponseEntity<>(new LoginResponse("登录成功", JwtUtil.generateToken(loginRequest.getUsername())), HttpStatus.OK);
} else {
return new ResponseEntity<>(new LoginResponse("用户名或密码错误", null), HttpStatus.UNAUTHORIZED);
}
}
@GetMapping("/userProfile")
public ResponseEntity<UserProfileResponse> getUserProfile(@RequestParam String token) {
if (JwtUtil.isTokenValid(token)) {
UserHS user = userService.getUserByUsername(JwtUtil.extractUsername(token));
if (user != null) {
UserProfileResponse response = new UserProfileResponse(
user.getUsername(),
user.getNickname(),
user.getGender(),
user.getPhoneNumber(),
user.getEmail(),
user.getDepartment(),
user.getRole(),
user.getCreatedAt()
);
if (user.getSuperAdmin()) {
response.setDepartment("超级管理员");
response.setRole("超级管理员");
}
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
@PostMapping("/manageUserProfile")
@JwtVerify
public ResponseEntity<Object> manageUserProfile(@RequestBody ManageUserProfileRequest userProfileRequest) {
String username = JwtUtil.extractUsername(userProfileRequest.getToken());
boolean succeeded = userService.updateUserProfile(username, userProfileRequest);
if (succeeded) {
return new ResponseEntity<>(HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
@PostMapping("/changePassword")
@JwtVerify
public ResponseEntity<Object> changePassword(@RequestBody ChangePasswordRequest changePasswordRequest) {
if (userService.changePassword(JwtUtil.extractUsername(changePasswordRequest.getToken()), changePasswordRequest)) {
return new ResponseEntity<>(HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
@GetMapping("/getVerificationCode")
public ResponseEntity<VerificationCodeResponse> getVerificationCode() {
return new ResponseEntity<>(verificationCodeService.provideVerificationCode(), HttpStatus.OK);
}
}

View File

@ -1,12 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.Getter;
import lombok.Setter;
import org.cmh.backend.Utils.JwtRequest;
@Getter
@Setter
public class ChangePasswordRequest extends JwtRequest {
private String currentPassword;
private String newPassword;
}

View File

@ -1,12 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class LoginRequest {
private String username;
private String password;
private String verificationCode;
}

View File

@ -1,13 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class LoginResponse {
private String message;
private String token;
}

View File

@ -1,16 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.Getter;
import lombok.Setter;
import org.cmh.backend.Utils.JwtRequest;
@Getter
@Setter
public class ManageUserProfileRequest extends JwtRequest {
private String nickname;
private String gender;
private String phoneNumber;
private String email;
private String department;
private String role;
}

View File

@ -1,13 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class RegisterRequest {
private String username;
private String password;
private String phoneNumber;
private String verificationCode;
}

View File

@ -1,12 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class RegisterResponse {
private String message;
}

View File

@ -1,21 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Getter
@Setter
@AllArgsConstructor
public class UserProfileResponse {
private String username;
private String nickname;
private String gender;
private String phoneNumber;
private String email;
private String department;
private String role;
private LocalDateTime createdAt;
}

View File

@ -1,13 +0,0 @@
package org.cmh.backend.Authentication.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class VerificationCodeResponse {
private String code;
private String path;
}

View File

@ -1,29 +0,0 @@
package org.cmh.backend.Authentication.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Setter
@Getter
@Entity
public class UserHS {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String nickname;
private String gender;
private String phoneNumber;
private String email;
private String department;
private String role;
private LocalDateTime createdAt;
private Boolean superAdmin = false;
}

View File

@ -1,8 +0,0 @@
package org.cmh.backend.Authentication.repository;
import org.cmh.backend.Authentication.model.UserHS;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<UserHS, Long> {
UserHS findByUsername(String username);
}

View File

@ -1,117 +0,0 @@
package org.cmh.backend.Authentication.service;
import org.cmh.backend.Authentication.dto.ChangePasswordRequest;
import org.cmh.backend.Authentication.dto.ManageUserProfileRequest;
import org.cmh.backend.Authentication.dto.RegisterRequest;
import org.cmh.backend.Authentication.model.UserHS;
import org.cmh.backend.Authentication.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.regex.Pattern;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public UserHS getUserByUsername(String username) {
return userRepository.findByUsername(username);
}
public boolean registerUser(RegisterRequest request) {
String username = request.getUsername();
String password = request.getPassword();
String phoneNumber = request.getPhoneNumber();
// 验证用户名是否已存在
if (userRepository.findByUsername(username) != null) {
return false; // 用户已存在
}
// 验证输入格式
if (!isValidUsername(username) || !isValidPassword(password) || !isValidContactInfo(phoneNumber)) {
throw new IllegalArgumentException();
}
// 加密密码
String encodedPassword = passwordEncoder.encode(password);
// 创建新用户
UserHS newUser = new UserHS();
newUser.setUsername(username);
newUser.setPassword(encodedPassword);
newUser.setPhoneNumber(phoneNumber);
newUser.setCreatedAt(LocalDateTime.now());
userRepository.save(newUser);
return true;
}
public boolean loginUser(String username, String password) {
UserHS user = userRepository.findByUsername(username);
return user != null && passwordEncoder.matches(password, user.getPassword());
}
public boolean updateUserProfile(String username, ManageUserProfileRequest request) {
UserHS user = userRepository.findByUsername(username);
if (user != null) {
user.setNickname(request.getNickname());
user.setGender(request.getGender());
user.setPhoneNumber(request.getPhoneNumber());
user.setEmail(request.getEmail());
user.setDepartment(request.getDepartment());
user.setRole(request.getRole());
try {
userRepository.save(user);
} catch (Exception e) {
return false;
}
return true;
} else {
return false;
}
}
public boolean changePassword(String username, ChangePasswordRequest request) {
UserHS user = userRepository.findByUsername(username);
if (user != null) {
if (passwordEncoder.matches(request.getCurrentPassword(), user.getPassword())) {
if (isValidPassword(request.getNewPassword())) {
String encodedPassword = passwordEncoder.encode(request.getNewPassword());
user.setPassword(encodedPassword);
try {
userRepository.save(user);
} catch (Exception e) {
return false;
}
return true;
}
}
}
return false;
}
// 验证用户名格式
private boolean isValidUsername(String username) {
return username != null && username.length() >= 3 && username.length() <= 20;
}
// 验证密码格式
private boolean isValidPassword(String password) {
return password != null && password.length() >= 4;
}
// 验证联系方式格式假设为电话号码
private boolean isValidContactInfo(String contactInfo) {
String regex = "^\\+?[0-9. ()-]{7,25}$"; // 电话号码验证
return contactInfo != null && Pattern.matches(regex, contactInfo);
}
}

View File

@ -1,51 +0,0 @@
package org.cmh.backend.Authentication.service;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.cmh.backend.Authentication.dto.VerificationCodeResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
@Service
public class VerificationCodeService {
@Value("${verification.code.images.path}")
private String verificationCodeImagesPath;
@Getter
private List<String> verificationCodeList = new ArrayList<>();
@PostConstruct
public void init() {
loadVerificationCodeImages();
}
private void loadVerificationCodeImages() {
try (Stream<Path> paths = Files.list(Paths.get(verificationCodeImagesPath))) {
paths.filter(path -> path.toString().endsWith(".png"))
.forEach(path -> verificationCodeList.add(path.getFileName().toString()));
} catch (IOException e) {
e.printStackTrace();
}
}
public VerificationCodeResponse provideVerificationCode() {
if (verificationCodeList.isEmpty()) {
return null;
}
Random random = new Random();
int code = random.nextInt(random.nextInt(verificationCodeList.size()));
String selectedCode = verificationCodeList.get(code);
return new VerificationCodeResponse(selectedCode.split("\\.")[0], "/verificationCodeImages/" + selectedCode);
}
}

View File

@ -0,0 +1,79 @@
package org.cmh.backend.OrganizationManagement.controller;
import org.aspectj.weaver.ast.Or;
import org.cmh.backend.OrganizationManagement.model.Organization;
import org.cmh.backend.OrganizationManagement.service.OrganizationService;
import org.cmh.backend.authentication.model.User;
import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import static org.springframework.http.ResponseEntity.ok;
@RestController
@RequestMapping("/organizations")
public class OrganizationController {
@Autowired
private OrganizationService organizationService;
@GetMapping("/listAll")
public ResponseEntity<List<Organization>> listAll() {
List<Organization> data = organizationService.listAll();
// System.out.println(data);
return ok(data);
}
@PostMapping("/addOrganization")
public Organization add(@RequestBody Map<String, String> credentials) {
Organization organization = new Organization();
if(credentials.get("organizationId") != null){
organization.setOrganizationId(Long.parseLong(credentials.get("organizationId")));
}
organization.setOrganizationName(credentials.get("organizationName"));
organization.setOrganizationStatus(Boolean.parseBoolean(credentials.get("organizationStatus")));
if(Integer.parseInt(credentials.get("parentOrganization")) != 0){
organization.setParentOrganization(organizationService.getById(Long.parseLong(credentials.get("parentOrganization"))));
}else{
organization.setParentOrganization(null);
}
organization.setEmail(credentials.get("email"));
organization.setLeader(credentials.get("leader"));
organization.setContactPhone(credentials.get("contactPhone"));
organization.setDisplayOrder(Integer.parseInt(credentials.get("displayOrder")));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String currentTime = sdf.format(new Date());
System.out.println(currentTime);
organization.setCreatedTime(currentTime);
Organization organizationAdd = organizationService.add(organization);
System.out.println(organizationAdd);
return organizationAdd;
}
@PostMapping("/deleteOrganization")
public String delete(@RequestBody Map<String, String> credentials) {
organizationService.delete(Long.valueOf(credentials.get("organizationId")));
return "success";
}
@PostMapping("/update")
public Organization update(@RequestBody Organization organization) {
return organizationService.update(organization);
}
@PostMapping("/getOrganizationById")
public Organization getById(@RequestBody Map<String, String> credentials) {
return organizationService.getById(Long.valueOf(credentials.get("organizationId")));
}
//ublic
}

View File

@ -0,0 +1,70 @@
package org.cmh.backend.OrganizationManagement.model;
import jakarta.persistence.*;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Table(name = "organizations")
@Entity
public class Organization {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter
@Getter
private long organizationId; // 部门ID作为数据库表的主键
@Setter
@Getter
@ManyToOne
@JoinColumn(name = "parent_organization_id")
private Organization parentOrganization; // 上级部门如果有的话
@Setter
@Getter
private String organizationName; // 部门名称
@Setter
@Getter
private int displayOrder; // 显示排序
@Setter
@Getter
private String leader; // 负责人
@Setter
@Getter
private String contactPhone; // 联系电话
@Setter
@Getter
private String email; // 邮箱
@Setter
@Getter
private boolean organizationStatus;
@Setter
@Getter
private String createdTime;
public Organization() {
}
public Organization(Organization organization, String organizationName, int displayOrder, String leader, String contactPhone, String email, boolean organizationStatus) {
this.parentOrganization = organization;
this.organizationName = organizationName;
this.displayOrder = displayOrder;
this.leader = leader;
this.contactPhone = contactPhone;
this.email = email;
this.organizationStatus = organizationStatus;
}
@Override
public String toString() {
return "Organization{" +
"organizationId=" + organizationId +
", parentOrganization='" + parentOrganization + '\'' +
", organizationName='" + organizationName + '\'' +
", displayOrder=" + displayOrder +
", leader='" + leader + '\'' +
", contactPhone='" + contactPhone + '\'' +
", email='" + email + '\'' +
", organizationStatus='" + organizationStatus + '\'' +
'}';
}
}

View File

@ -0,0 +1,20 @@
package org.cmh.backend.OrganizationManagement.repository;
import org.aspectj.weaver.ast.Or;
import org.cmh.backend.OrganizationManagement.model.Organization;
import org.cmh.backend.OrganizationManagement.service.OrganizationService;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface OrganizationRepository extends JpaRepository<Organization, Long> {
List<Organization> findAll();
Organization save(Organization organization);
void deleteById(Long id);
Organization findByOrganizationId(Long id);
Organization findByOrganizationName(String name);
}

View File

@ -0,0 +1,36 @@
package org.cmh.backend.OrganizationManagement.service;
import org.cmh.backend.OrganizationManagement.model.Organization;
import org.cmh.backend.OrganizationManagement.repository.OrganizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrganizationService {
@Autowired
private OrganizationRepository organizationRepository;
public List<Organization> listAll(){
return organizationRepository.findAll();
}
public Organization add(Organization organization){
return organizationRepository.save(organization);
}
public Organization update(Organization organization){
return organizationRepository.save(organization);
}
public String delete(Long id){
organizationRepository.deleteById(id);
return "success";
}
public Organization getById(Long organizationId){
return organizationRepository.findByOrganizationId(organizationId);
}
public Organization getByName(String name){
return organizationRepository.findByOrganizationName(name);
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Some files were not shown because too many files have changed in this diff Show More