Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba886c7191 | |||
| 05f3af0c77 | |||
| c42a3a55ec | |||
| d027e26f8c | |||
| f9272335f2 | |||
| 5885f365db | |||
| 780e4793de | |||
| f6d5112ad5 | |||
| dafdf82f5d | |||
| aae8147143 | |||
| fe58248efc | |||
| 9569a30a8a | |||
| ef10067345 | |||
| 09ebd82dac | |||
| 94a7b1bc13 |
5
pom.xml
@ -93,11 +93,6 @@
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.h2database</groupId>-->
|
||||
<!-- <artifactId>h2</artifactId>-->
|
||||
<!-- <scope>runtime</scope>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
|
||||
@ -0,0 +1,138 @@
|
||||
package org.cmh.backend.CourseManagement.controller;
|
||||
|
||||
import org.cmh.backend.CourseManagement.dto.GetCourseListResponse;
|
||||
import org.cmh.backend.CourseManagement.dto.MessageResponse;
|
||||
import org.cmh.backend.CourseManagement.dto.CourseRequest;
|
||||
import org.cmh.backend.CourseManagement.dto.SearchCourseRequest;
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.cmh.backend.CourseManagement.service.CourseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/courses")
|
||||
public class CourseController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CourseController.class);
|
||||
|
||||
@Autowired
|
||||
private CourseService courseService;
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<GetCourseListResponse> getCoursesByRange(@RequestParam Integer start, @RequestParam Integer end, @RequestParam String token) {
|
||||
if (start >= end) {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
long courseCount = courseService.getCourseCount();
|
||||
return new ResponseEntity<>(new GetCourseListResponse(courseCount, courseService.getCoursesByRange(start, end)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Course> getCoursePage(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
return new ResponseEntity<>(courseService.getCourseById(id), HttpStatus.OK);
|
||||
} catch (EntityNotFoundException e) {
|
||||
logger.error("Course not found with id: {}", id, e);
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<MessageResponse> createCourse(@RequestBody CourseRequest request) {
|
||||
try {
|
||||
courseService.createCourse(request);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
logger.error("Create course failed: Data integrity violation", e);
|
||||
return new ResponseEntity<>(new MessageResponse("创建失败,课程已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
logger.error("Create course failed: {}", e.getMessage(), e);
|
||||
return new ResponseEntity<>(new MessageResponse("创建失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("创建成功"), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<MessageResponse> updateCourse(@PathVariable Long id, @RequestBody CourseRequest request) {
|
||||
try {
|
||||
courseService.updateCourse(id, request);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
logger.error("Update course failed: Data integrity violation", e);
|
||||
return new ResponseEntity<>(new MessageResponse("修改失败,新标题已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
||||
} catch (EntityNotFoundException e) {
|
||||
logger.error("Course not found with id: {}", id, e);
|
||||
return new ResponseEntity<>(new MessageResponse("修改失败: 课程不存在"), HttpStatus.NOT_FOUND);
|
||||
} catch (Exception e) {
|
||||
logger.error("Update course failed: {}", e.getMessage(), e);
|
||||
return new ResponseEntity<>(new MessageResponse("修改失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("修改成功"), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<MessageResponse> deleteCourse(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
courseService.deleteCourse(id);
|
||||
} catch (EntityNotFoundException e) {
|
||||
logger.error("Course not found with id: {}", id, e);
|
||||
return new ResponseEntity<>(new MessageResponse("删除失败,课程不存在"), HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
logger.error("Delete course failed: {}", e.getMessage(), e);
|
||||
return new ResponseEntity<>(new MessageResponse("删除失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("删除成功"), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
public ResponseEntity<GetCourseListResponse> searchCourses(@RequestParam String token,
|
||||
@RequestParam(required = false) String title,
|
||||
@RequestParam(required = false) String author,
|
||||
@RequestParam(required = false) String description,
|
||||
@RequestParam(required = false) String sortOrder,
|
||||
@RequestParam Integer start,
|
||||
@RequestParam Integer end) {
|
||||
if (start >= end) {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
SearchCourseRequest request = new SearchCourseRequest(token, title, author, description, sortOrder, start, end);
|
||||
List<Course> courses = courseService.searchCourses(request);
|
||||
long courseCount = courseService.getCourseCountByCriteria(title, author, description, sortOrder);
|
||||
|
||||
return new ResponseEntity<>(new GetCourseListResponse(courseCount, courses), HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
logger.error("Search courses failed with parameters: title={}, author={}, description={}, sortOrder={}, start={}, end={}",
|
||||
title, author, description, sortOrder, start, end, e);
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
@GetMapping("/sort")
|
||||
public ResponseEntity<GetCourseListResponse> sortCourses(@RequestParam String token,
|
||||
@RequestParam String sortField,
|
||||
@RequestParam String sortDirection,
|
||||
@RequestParam Integer start,
|
||||
@RequestParam Integer end) {
|
||||
if (start >= end) {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
long courseCount = courseService.getCourseCount();
|
||||
List<Course> sortedCourses = courseService.sortCourses(sortField, sortDirection, start, end);
|
||||
|
||||
return new ResponseEntity<>(new GetCourseListResponse(courseCount, sortedCourses), HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
logger.error("Sort courses failed with parameters: sortField={}, sortDirection={}, start={}, end={}",
|
||||
sortField, sortDirection, start, end, e);
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,26 +1,26 @@
|
||||
package org.cmh.backend.NewsManagement.controller;
|
||||
package org.cmh.backend.CourseManagement.controller;
|
||||
|
||||
import org.cmh.backend.NewsManagement.dto.UploadFileResponse;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.cmh.backend.CourseManagement.dto.UploadFileResponse;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin // 如果前端和后端不在同一个域名或端口下,需要启用跨域
|
||||
@RequestMapping("/courses")
|
||||
public class FileController {
|
||||
|
||||
private static final String UPLOAD_DIR = "uploads/";
|
||||
|
||||
@PostMapping("/news/uploadPic")
|
||||
@PostMapping("/upload")
|
||||
public ResponseEntity<UploadFileResponse> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
return new ResponseEntity<>(new UploadFileResponse("文件不能为空", null), HttpStatus.BAD_REQUEST);
|
||||
@ -39,13 +39,12 @@ public class FileController {
|
||||
Files.write(path, bytes);
|
||||
|
||||
// 返回成功信息
|
||||
return new ResponseEntity<>(new UploadFileResponse("文件上传成功", "/api/news/files/" + file.getOriginalFilename()), HttpStatus.OK);
|
||||
return new ResponseEntity<>(new UploadFileResponse("文件上传成功", "/api/courses/files/" + file.getOriginalFilename()), HttpStatus.OK);
|
||||
} catch (IOException e) {
|
||||
return new ResponseEntity<>(new UploadFileResponse("文件上传失败", null), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/news/files/{filename}")
|
||||
@GetMapping("/files/{filename}")
|
||||
public ResponseEntity<Resource> getFile(@PathVariable String filename) {
|
||||
try {
|
||||
Path filePath = Paths.get(UPLOAD_DIR).resolve(filename).normalize();
|
||||
@ -1,4 +1,4 @@
|
||||
package org.cmh.backend.NewsManagement.dto;
|
||||
package org.cmh.backend.CourseManagement.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@ -6,11 +6,11 @@ import org.cmh.backend.Utils.JwtRequest;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class NewsRequest extends JwtRequest {
|
||||
public class CourseRequest extends JwtRequest {
|
||||
private String title;
|
||||
private String summary;
|
||||
private String content;
|
||||
private String imagePath;
|
||||
private String description;
|
||||
private String orderNo;
|
||||
private String author;
|
||||
private String tenant;
|
||||
private String videoPath;
|
||||
private String imagePath;
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package org.cmh.backend.CourseManagement.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class GetCourseListResponse {
|
||||
Long courseCount;
|
||||
List<Course> courseList;
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package org.cmh.backend.NewsManagement.dto;
|
||||
package org.cmh.backend.CourseManagement.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
@ -0,0 +1,18 @@
|
||||
package org.cmh.backend.CourseManagement.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class SearchCourseRequest {
|
||||
private String token;
|
||||
private String title;
|
||||
private String author;
|
||||
private String description;
|
||||
private String sortOrder;
|
||||
private int start;
|
||||
private int end;
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
package org.cmh.backend.NewsManagement.dto;
|
||||
|
||||
package org.cmh.backend.CourseManagement.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@ -0,0 +1,21 @@
|
||||
package org.cmh.backend.CourseManagement.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
public class Course {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
private String description;
|
||||
private String orderNo;
|
||||
private String author;
|
||||
private String videoPath;
|
||||
private String imagePath;
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package org.cmh.backend.CourseManagement.repository;
|
||||
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
@Repository
|
||||
public interface CourseRepository extends JpaRepository<Course, Long>, JpaSpecificationExecutor<Course> {
|
||||
Page<Course> findAllByOrderByIdDesc(Pageable pageable);
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
package org.cmh.backend.CourseManagement.service;
|
||||
|
||||
import org.cmh.backend.CourseManagement.dto.CourseRequest;
|
||||
import org.cmh.backend.CourseManagement.dto.SearchCourseRequest;
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.cmh.backend.CourseManagement.repository.CourseRepository;
|
||||
import org.cmh.backend.CourseManagement.specification.CourseSpecification;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CourseService {
|
||||
|
||||
@Autowired
|
||||
private CourseRepository courseRepository;
|
||||
|
||||
public long getCourseCount() {
|
||||
return courseRepository.count();
|
||||
}
|
||||
|
||||
public List<Course> getCoursesByRange(int start, int end) {
|
||||
Pageable pageable = PageRequest.of(start, end - start);
|
||||
return courseRepository.findAll(pageable).getContent();
|
||||
}
|
||||
|
||||
public Course getCourseById(Long id) {
|
||||
return courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
|
||||
}
|
||||
|
||||
public void createCourse(CourseRequest request) {
|
||||
Course course = new Course();
|
||||
course.setTitle(request.getTitle());
|
||||
course.setAuthor(request.getAuthor());
|
||||
course.setDescription(request.getDescription());
|
||||
course.setOrderNo(request.getOrderNo());
|
||||
course.setVideoPath(request.getVideoPath()); // 确保保存视频路径
|
||||
course.setImagePath(request.getImagePath()); // 确保保存图片路径
|
||||
courseRepository.save(course);
|
||||
}
|
||||
|
||||
public void updateCourse(Long id, CourseRequest request) {
|
||||
Course course = courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
|
||||
course.setTitle(request.getTitle());
|
||||
course.setAuthor(request.getAuthor());
|
||||
course.setDescription(request.getDescription());
|
||||
course.setOrderNo(request.getOrderNo());
|
||||
course.setVideoPath(request.getVideoPath()); // 确保更新视频路径
|
||||
course.setImagePath(request.getImagePath()); // 确保更新图片路径
|
||||
courseRepository.save(course);
|
||||
}
|
||||
|
||||
public void deleteCourse(Long id) {
|
||||
courseRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public List<Course> searchCourses(SearchCourseRequest request) {
|
||||
Pageable pageable = PageRequest.of(request.getStart(), request.getEnd() - request.getStart());
|
||||
return courseRepository.findAll(
|
||||
CourseSpecification.searchCourses(
|
||||
request.getTitle(),
|
||||
request.getAuthor(),
|
||||
request.getDescription(),
|
||||
request.getSortOrder()
|
||||
),
|
||||
pageable
|
||||
).getContent();
|
||||
}
|
||||
public long getCourseCountByCriteria(String title, String author, String description, String sortOrder) {
|
||||
return courseRepository.count(CourseSpecification.searchCourses(title, author, description, sortOrder));
|
||||
}
|
||||
public List<Course> sortCourses(String sortField, String sortDirection, int start, int end) {
|
||||
Pageable pageable = PageRequest.of(start, end - start);
|
||||
return courseRepository.findAll(
|
||||
CourseSpecification.sortCourses(sortField, sortDirection),
|
||||
pageable
|
||||
).getContent();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package org.cmh.backend.CourseManagement.specification;
|
||||
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CourseSpecification {
|
||||
|
||||
public static Specification<Course> searchCourses(
|
||||
String title, String author, String description, String sortOrder) {
|
||||
|
||||
return (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
if (title != null && !title.isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("title"), "%" + title + "%"));
|
||||
}
|
||||
if (author != null && !author.isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("author"), "%" + author + "%"));
|
||||
}
|
||||
if (description != null && !description.isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("description"), "%" + description + "%"));
|
||||
}
|
||||
if (sortOrder != null && !sortOrder.isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("orderNo"), "%" + sortOrder+ "%"));
|
||||
}
|
||||
|
||||
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
|
||||
};
|
||||
}
|
||||
public static Specification<Course> sortCourses(String sortField, String sortDirection) {
|
||||
return (root, query, cb) -> {
|
||||
if ("asc".equalsIgnoreCase(sortDirection)) {
|
||||
query.orderBy(cb.asc(root.get(sortField)));
|
||||
} else if ("desc".equalsIgnoreCase(sortDirection)) {
|
||||
query.orderBy(cb.desc(root.get(sortField)));
|
||||
}
|
||||
return cb.conjunction();
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,104 +0,0 @@
|
||||
package org.cmh.backend.NewsManagement.controller;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.cmh.backend.NewsManagement.dto.GetNewsListResponse;
|
||||
import org.cmh.backend.NewsManagement.dto.MessageResponse;
|
||||
import org.cmh.backend.NewsManagement.dto.NewsRequest;
|
||||
import org.cmh.backend.NewsManagement.dto.SearchNewsRequest;
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
import org.cmh.backend.NewsManagement.service.NewsService;
|
||||
import org.cmh.backend.Utils.JwtUtil;
|
||||
import org.cmh.backend.Utils.JwtVerify;
|
||||
import org.cmh.backend.authentication.model.UserHS;
|
||||
import org.cmh.backend.authentication.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/news")
|
||||
public class NewsController {
|
||||
@Autowired
|
||||
private NewsService newsService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping
|
||||
@JwtVerify
|
||||
public ResponseEntity<GetNewsListResponse> getNewsByRange(@RequestParam Integer start, @RequestParam Integer end, @RequestParam String token) {
|
||||
String username = JwtUtil.extractUsername(token);
|
||||
UserHS user = userService.getUserByUsername(username);
|
||||
// TODO:完善用户权限
|
||||
if (user.getSuperAdmin()) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
if (start >= end) {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
long newsCount = newsService.getNewsCount();
|
||||
return new ResponseEntity<>(new GetNewsListResponse(newsCount, newsService.getNewsByRange(start, end, user)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/search")
|
||||
@JwtVerify
|
||||
public ResponseEntity<GetNewsListResponse> searchNews(@RequestBody SearchNewsRequest request) {
|
||||
List<News> newsList = newsService.searchNews(request);
|
||||
long newsCount = newsList.size();
|
||||
return new ResponseEntity<>(new GetNewsListResponse(newsCount, newsList), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<News> getNewsPage(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
return new ResponseEntity<>(newsService.getNewsById(id), HttpStatus.OK);
|
||||
} catch (EntityNotFoundException e) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> createNews(@RequestBody NewsRequest request) {
|
||||
try {
|
||||
newsService.createNews(request);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("创建失败,文章已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(new MessageResponse("创建失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("创建成功"), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> updateNews(@PathVariable Long id, @RequestBody NewsRequest request) {
|
||||
try {
|
||||
newsService.updateNews(id, request);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("修改失败,新标题已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(new MessageResponse("创建失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("修改成功"), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> deleteNews(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
newsService.deleteNews(id);
|
||||
} catch (EntityNotFoundException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("删除失败,文章不存在"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("删除成功"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package org.cmh.backend.NewsManagement.dto;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class GetNewsListResponse {
|
||||
Long newsCount;
|
||||
List<News> newsList;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
package org.cmh.backend.NewsManagement.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.cmh.backend.Utils.JwtRequest;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class SearchNewsRequest extends JwtRequest {
|
||||
private String author;
|
||||
private String title;
|
||||
private String imagePath;
|
||||
private String summary;
|
||||
private String sortBy;
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package org.cmh.backend.NewsManagement.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@Entity
|
||||
public class News {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String summary;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String content;
|
||||
|
||||
private String imagePath;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String author;
|
||||
|
||||
@CreationTimestamp
|
||||
private LocalDateTime createdAt;
|
||||
// TODO:添加外键绑定
|
||||
private String tenant;
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package org.cmh.backend.NewsManagement.repository;
|
||||
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface NewsRepository extends JpaRepository<News, Long> {
|
||||
Page<News> findAllByOrderByIdDesc(Pageable pageable);
|
||||
|
||||
Page<News> findByTenantOrderByIdDesc(String tenant, Pageable pageable);
|
||||
|
||||
List<News> findByTitleContainingOrSummaryContainingOrAuthorContainingOrImagePathContainingAndTenantEquals(
|
||||
String title,
|
||||
String summary,
|
||||
String author,
|
||||
String imagePath,
|
||||
String tenant);
|
||||
|
||||
List<News> findByTitleContainingOrSummaryContainingOrAuthorContainingOrImagePathContaining(String title, String summary, String author, String imagePath);
|
||||
}
|
||||
@ -1,175 +0,0 @@
|
||||
package org.cmh.backend.NewsManagement.service;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import org.cmh.backend.NewsManagement.dto.NewsRequest;
|
||||
import org.cmh.backend.NewsManagement.dto.SearchNewsRequest;
|
||||
import org.cmh.backend.NewsManagement.model.News;
|
||||
import org.cmh.backend.NewsManagement.repository.NewsRepository;
|
||||
import org.cmh.backend.Utils.JwtUtil;
|
||||
import org.cmh.backend.authentication.model.UserHS;
|
||||
import org.cmh.backend.authentication.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class NewsService {
|
||||
@Autowired
|
||||
private NewsRepository newsRepository;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
public List<News> getNewsByRange(int start, int end, UserHS user) {
|
||||
if (start < 0 || end <= start) {
|
||||
throw new IllegalArgumentException("Invalid start or end range");
|
||||
}
|
||||
|
||||
int pageSize = end - start; // 计算每页的大小
|
||||
int startPageNumber = start / pageSize; // 计算起始页码
|
||||
int endPageNumber = (end - 1) / pageSize; // 计算结束页码
|
||||
|
||||
List<News> result = new ArrayList<>();
|
||||
|
||||
for (int pageNumber = startPageNumber; pageNumber <= endPageNumber; pageNumber++) {
|
||||
Pageable pageable = PageRequest.of(pageNumber, pageSize);
|
||||
Page<News> newsPage = null;
|
||||
if (user.getSuperAdmin()) {
|
||||
newsPage = newsRepository.findAllByOrderByIdDesc(pageable);
|
||||
} else {
|
||||
newsPage = newsRepository.findByTenantOrderByIdDesc(user.getUsername(), pageable);
|
||||
}
|
||||
|
||||
if (newsPage.hasContent()) {
|
||||
result.addAll(newsPage.getContent());
|
||||
} else {
|
||||
break; // 如果没有更多内容,提前退出
|
||||
}
|
||||
}
|
||||
|
||||
int startIndex = start % pageSize;
|
||||
int endIndex = startIndex + (end - start);
|
||||
|
||||
if (endIndex > result.size()) {
|
||||
endIndex = result.size();
|
||||
}
|
||||
|
||||
return result.subList(startIndex, endIndex);
|
||||
}
|
||||
|
||||
|
||||
public void createNews(NewsRequest request) {
|
||||
News news = new News();
|
||||
news.setTitle(request.getTitle());
|
||||
news.setSummary(request.getSummary());
|
||||
news.setContent(request.getContent());
|
||||
news.setAuthor(request.getAuthor());
|
||||
news.setImagePath(request.getImagePath());
|
||||
news.setTenant(request.getTenant());
|
||||
newsRepository.save(news);
|
||||
}
|
||||
|
||||
public void updateNews(Long id, NewsRequest request) {
|
||||
News news = newsRepository.findById(id).orElse(null);
|
||||
if (news != null) {
|
||||
news.setTitle(request.getTitle());
|
||||
news.setSummary(request.getSummary());
|
||||
news.setContent(request.getContent());
|
||||
news.setAuthor(request.getAuthor());
|
||||
news.setImagePath(request.getImagePath());
|
||||
news.setTenant(request.getTenant());
|
||||
newsRepository.save(news);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteNews(Long id) {
|
||||
if (!newsRepository.existsById(id)) {
|
||||
throw new EntityNotFoundException();
|
||||
}
|
||||
newsRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public News getNewsById(Long id) {
|
||||
if (!newsRepository.existsById(id)) {
|
||||
throw new EntityNotFoundException();
|
||||
}
|
||||
return newsRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public long getNewsCount() {
|
||||
return newsRepository.count();
|
||||
}
|
||||
|
||||
// TODO:完善用户权限
|
||||
// public List<News> searchNews(SearchNewsRequest request) {
|
||||
// String username = JwtUtil.extractUsername(request.getToken());
|
||||
// UserHS user = userService.getUserByUsername(username);
|
||||
// if (user.getSuperAdmin()) {
|
||||
// return newsRepository.findByTitleContainingOrSummaryContainingOrAuthorContainingOrImagePathContaining(
|
||||
// request.getTitle(),
|
||||
// request.getSummary(),
|
||||
// request.getAuthor(),
|
||||
// request.getImagePath()
|
||||
// );
|
||||
// }
|
||||
// return newsRepository.findByTitleContainingOrSummaryContainingOrAuthorContainingOrImagePathContainingAndTenantEquals(
|
||||
// request.getTitle(),
|
||||
// request.getSummary(),
|
||||
// request.getAuthor(),
|
||||
// request.getImagePath(),
|
||||
// user.getUsername()
|
||||
// );
|
||||
// }
|
||||
public List<News> searchNews(SearchNewsRequest request) {
|
||||
String username = JwtUtil.extractUsername(request.getToken());
|
||||
UserHS user = userService.getUserByUsername(username);
|
||||
|
||||
// Create a list to hold predicates
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
// Create the query
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<News> criteriaQuery = criteriaBuilder.createQuery(News.class);
|
||||
Root<News> root = criteriaQuery.from(News.class);
|
||||
|
||||
// Build the query conditionally based on non-empty fields
|
||||
if (!request.getTitle().isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("title"), "%" + request.getTitle() + "%"));
|
||||
}
|
||||
if (!request.getSummary().isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("summary"), "%" + request.getSummary() + "%"));
|
||||
}
|
||||
if (!request.getAuthor().isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("author"), "%" + request.getAuthor() + "%"));
|
||||
}
|
||||
if (!request.getImagePath().isEmpty()) {
|
||||
predicates.add(criteriaBuilder.like(root.get("imagePath"), "%" + request.getImagePath() + "%"));
|
||||
}
|
||||
|
||||
if (user.getSuperAdmin()) {
|
||||
// Combine predicates with OR
|
||||
criteriaQuery.where(criteriaBuilder.or(predicates.toArray(new Predicate[0])));
|
||||
} else {
|
||||
// Combine predicates with OR and add tenant condition
|
||||
Predicate tenantPredicate = criteriaBuilder.equal(root.get("tenant"), user.getUsername());
|
||||
predicates.add(tenantPredicate);
|
||||
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
|
||||
}
|
||||
|
||||
// Execute the query
|
||||
return entityManager.createQuery(criteriaQuery).getResultList();
|
||||
}
|
||||
}
|
||||
@ -2,15 +2,9 @@ package org.cmh.backend.Utils;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ -18,17 +12,4 @@ public class GlobalExceptionHandler {
|
||||
public ResponseEntity<Object> handleJwtInvalidException(JwtValidationException ex) {
|
||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
||||
public ResponseEntity<Map<String, String>> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) {
|
||||
HashMap<String, String> response = new HashMap<>();
|
||||
response.put("error", ex.getMessage());
|
||||
response.put("stackTrace", Arrays.toString(ex.getStackTrace()));
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
public ResponseEntity<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
|
||||
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package org.cmh.backend.Utils;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -8,26 +7,14 @@ import org.springframework.stereotype.Component;
|
||||
@Aspect
|
||||
@Component
|
||||
public class JwtVerifyAspect {
|
||||
@Before("@annotation(JwtVerify)&&args(..)")
|
||||
public void verifyJwtToken(JoinPoint joinPoint) throws JwtValidationException {
|
||||
Object[] args = joinPoint.getArgs();
|
||||
for (Object arg : args) {
|
||||
if (arg instanceof JwtRequest jwtRequest) {
|
||||
String token = jwtRequest.getToken();
|
||||
@Before("@annotation(JwtVerify) && args(request,..)")
|
||||
public void verifyJwtToken(Object request) throws JwtValidationException {
|
||||
if (request instanceof JwtRequest) {
|
||||
String token = ((JwtRequest) request).getToken();
|
||||
if (!JwtUtil.isTokenValid(token)) {
|
||||
throw new JwtValidationException("请求未正确携带身份令牌");
|
||||
}
|
||||
return; // 只接受第一个 JwtRequest 对象,收到后不再校验其他参数
|
||||
}
|
||||
// JWTRequest对象优先,否则再检查其他字符串参数
|
||||
if (arg instanceof String token){
|
||||
if (JwtUtil.isTokenValid(token)){
|
||||
// 验证成功就直接退出。
|
||||
return;
|
||||
throw new JwtValidationException("JWT token is invalid");
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new JwtValidationException("请求未正确携带身份令牌");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
package org.cmh.backend.authentication.controller;
|
||||
|
||||
import org.cmh.backend.Utils.JwtUtil;
|
||||
import org.cmh.backend.authentication.dto.*;
|
||||
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.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<RegisterResponse> register(@RequestBody User user) {
|
||||
User registeredUser = userService.register(user);
|
||||
if (registeredUser == null) {
|
||||
return new ResponseEntity<>(new RegisterResponse("用户已存在"),HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new RegisterResponse("注册成功"),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
|
||||
User user = userService.login(request.getUsername(), request.getPassword());
|
||||
if (user != null) {
|
||||
return new ResponseEntity<>(new LoginResponse("登录成功", JwtUtil.generateToken(user.getUsername())), HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>(new LoginResponse("登录失败", null), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public ResponseEntity<?> updateUserInfo(@RequestBody UpdateRequest user) {
|
||||
userService.updateUserInfo(user);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@PostMapping("/changePassword")
|
||||
public ResponseEntity<?> changePassword(@RequestParam String username, @RequestParam String oldPassword, @RequestParam String newPassword) {
|
||||
boolean isChanged = userService.changePassword(username, oldPassword, newPassword);
|
||||
if (isChanged) {
|
||||
return ResponseEntity.ok("Password changed successfully");
|
||||
}
|
||||
return ResponseEntity.status(400).body("Old password is incorrect");
|
||||
}
|
||||
|
||||
@PostMapping("/getVerificationCode")
|
||||
public String getVerificationCode(@RequestBody String contact) {
|
||||
return "Verification code sent to " + contact;
|
||||
}
|
||||
|
||||
@GetMapping("/profile")
|
||||
public ResponseEntity<ProfileResponse> getUserInfo(@RequestParam String token) {
|
||||
if (!JwtUtil.isTokenValid(token)) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
}
|
||||
String username = JwtUtil.extractUsername(token);
|
||||
return new ResponseEntity<>(userService.getUserInfo(username),HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
@ -8,5 +8,4 @@ import lombok.Setter;
|
||||
public class LoginRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
private String verificationCode;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
@ -9,13 +9,11 @@ import java.time.LocalDateTime;
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class UserProfileResponse {
|
||||
public class ProfileResponse {
|
||||
private String username;
|
||||
private String nickname;
|
||||
private String gender;
|
||||
private String phoneNumber;
|
||||
private String email;
|
||||
private String department;
|
||||
private String role;
|
||||
private LocalDateTime createdAt;
|
||||
private String phoneNumber;
|
||||
private String company;
|
||||
private LocalDateTime createdDate;
|
||||
}
|
||||
@ -1,13 +1,15 @@
|
||||
package org.cmh.backend.authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class RegisterRequest {
|
||||
@AllArgsConstructor
|
||||
public class UpdateRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
private String email;
|
||||
private String phoneNumber;
|
||||
private String verificationCode;
|
||||
private String company;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
34
src/main/java/org/cmh/backend/authentication/model/User.java
Normal file
@ -0,0 +1,34 @@
|
||||
// User.java
|
||||
package org.cmh.backend.authentication.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "Chester")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String username;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String email;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String role;
|
||||
|
||||
private String phoneNumber;
|
||||
private String company;
|
||||
|
||||
@Column(nullable = false)
|
||||
private LocalDateTime createdDate;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
@ -1,8 +1,12 @@
|
||||
package org.cmh.backend.authentication.repository;
|
||||
|
||||
import org.cmh.backend.authentication.model.UserHS;
|
||||
import org.cmh.backend.authentication.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserHS, Long> {
|
||||
UserHS findByUsername(String username);
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
User findByUsername(String username);
|
||||
}
|
||||
@ -1,9 +1,8 @@
|
||||
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.dto.ProfileResponse;
|
||||
import org.cmh.backend.authentication.dto.UpdateRequest;
|
||||
import org.cmh.backend.authentication.model.User;
|
||||
import org.cmh.backend.authentication.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
@ -11,107 +10,63 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
// UserService.java
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
public UserHS getUserByUsername(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
public User register(User user) {
|
||||
User existingUser = userRepository.findByUsername(user.getUsername());
|
||||
if (existingUser != null) {
|
||||
return null;
|
||||
}
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
user.setCreatedDate(LocalDateTime.now());
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
public boolean registerUser(RegisterRequest request) {
|
||||
String username = request.getUsername();
|
||||
String password = request.getPassword();
|
||||
String phoneNumber = request.getPhoneNumber();
|
||||
// 验证用户名是否已存在
|
||||
if (userRepository.findByUsername(username) != null) {
|
||||
return false; // 用户已存在
|
||||
public User login(String username, String password) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
if (user != null && passwordEncoder.matches(password, user.getPassword())) {
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 验证输入格式
|
||||
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);
|
||||
public void updateUserInfo(UpdateRequest newUser) {
|
||||
User user = userRepository.findByUsername(newUser.getUsername());
|
||||
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 {
|
||||
user.setEmail(newUser.getEmail());
|
||||
user.setPhoneNumber(newUser.getPhoneNumber());
|
||||
user.setCompany(newUser.getCompany());
|
||||
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 {
|
||||
public boolean changePassword(String username, String oldPassword, String newPassword) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
if (user != null && passwordEncoder.matches(oldPassword, user.getPassword())) {
|
||||
user.setPassword(passwordEncoder.encode(newPassword));
|
||||
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;
|
||||
public ProfileResponse getUserInfo(String username) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
return new ProfileResponse(
|
||||
user.getUsername(),
|
||||
user.getEmail(),
|
||||
user.getRole(),
|
||||
user.getPhoneNumber(),
|
||||
user.getCompany(),
|
||||
user.getCreatedDate()
|
||||
);
|
||||
}
|
||||
|
||||
// 验证密码格式
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -20,10 +20,9 @@ spring.datasource.hikari.connection-timeout=30000
|
||||
server.servlet.encoding.enabled=true
|
||||
server.servlet.encoding.force=true
|
||||
server.servlet.encoding.charset=utf-8
|
||||
# verificationCode
|
||||
verification.code.images.path=src/main/resources/static/verificationCodeImages
|
||||
# set the max size of a single file
|
||||
spring.servlet.multipart.max-file-size=50MB
|
||||
spring.servlet.multipart.max-file-size=500MB
|
||||
# set the max size of the total request
|
||||
spring.servlet.multipart.max-request-size=50MB
|
||||
spring.servlet.multipart.max-request-size=500MB
|
||||
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.0 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |