|
|
|
|
@ -36,6 +36,17 @@ public class WebController {
|
|
|
|
|
private final AIService aiService;
|
|
|
|
|
|
|
|
|
|
private static final int PAGE_SIZE = 10;
|
|
|
|
|
|
|
|
|
|
// Model attribute constants
|
|
|
|
|
private static final String BOOKS_ATTR = "books";
|
|
|
|
|
private static final String TOTAL_PAGES_ATTR = "totalPages";
|
|
|
|
|
private static final String LOGIN_VIEW = "login";
|
|
|
|
|
private static final String REDIRECT_HOME = "redirect:/";
|
|
|
|
|
private static final String ERROR_ATTR = "error";
|
|
|
|
|
private static final String BUILD_LIBS_PATH = "/build/libs/";
|
|
|
|
|
private static final String MODEL_ATTR = "model";
|
|
|
|
|
private static final String BOOKS_VIEW = "books";
|
|
|
|
|
private static final String LOANS_VIEW = "loans";
|
|
|
|
|
|
|
|
|
|
public WebController() {
|
|
|
|
|
this.bookService = new BookService();
|
|
|
|
|
@ -84,19 +95,19 @@ public class WebController {
|
|
|
|
|
// 分页
|
|
|
|
|
int total = allBooks.size();
|
|
|
|
|
int totalPages = (int) Math.ceil((double) total / PAGE_SIZE);
|
|
|
|
|
page = Math.max(1, Math.min(page, Math.max(1, totalPages)));
|
|
|
|
|
page = Math.clamp(page, 1, Math.max(1, totalPages));
|
|
|
|
|
int start = (page - 1) * PAGE_SIZE;
|
|
|
|
|
int end = Math.min(start + PAGE_SIZE, total);
|
|
|
|
|
List<Book> books = allBooks.subList(start, end);
|
|
|
|
|
|
|
|
|
|
model.addAttribute("books", books);
|
|
|
|
|
model.addAttribute(BOOKS_ATTR, books);
|
|
|
|
|
model.addAttribute("currentPage", page);
|
|
|
|
|
model.addAttribute("totalPages", totalPages);
|
|
|
|
|
model.addAttribute(TOTAL_PAGES_ATTR, totalPages);
|
|
|
|
|
model.addAttribute("totalBooks", total);
|
|
|
|
|
model.addAttribute("category", category);
|
|
|
|
|
model.addAttribute("keyword", keyword);
|
|
|
|
|
model.addAttribute("user", session.getAttribute("user"));
|
|
|
|
|
return "books";
|
|
|
|
|
return BOOKS_VIEW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/loans")
|
|
|
|
|
@ -106,23 +117,23 @@ public class WebController {
|
|
|
|
|
|
|
|
|
|
int total = allLoans.size();
|
|
|
|
|
int totalPages = (int) Math.ceil((double) total / PAGE_SIZE);
|
|
|
|
|
page = Math.max(1, Math.min(page, Math.max(1, totalPages)));
|
|
|
|
|
page = Math.clamp(page, 1, Math.max(1, totalPages));
|
|
|
|
|
int start = (page - 1) * PAGE_SIZE;
|
|
|
|
|
int end = Math.min(start + PAGE_SIZE, total);
|
|
|
|
|
List<Loan> loans = allLoans.subList(start, end);
|
|
|
|
|
|
|
|
|
|
model.addAttribute("loans", loans);
|
|
|
|
|
model.addAttribute("currentPage", page);
|
|
|
|
|
model.addAttribute("totalPages", totalPages);
|
|
|
|
|
model.addAttribute(TOTAL_PAGES_ATTR, totalPages);
|
|
|
|
|
model.addAttribute("user", session.getAttribute("user"));
|
|
|
|
|
return "loans";
|
|
|
|
|
return LOANS_VIEW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 用户登录 ====================
|
|
|
|
|
|
|
|
|
|
@GetMapping("/login")
|
|
|
|
|
public String loginPage() {
|
|
|
|
|
return "login";
|
|
|
|
|
return LOGIN_VIEW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
@ -132,10 +143,10 @@ public class WebController {
|
|
|
|
|
User user = userService.login(email, password);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
session.setAttribute("user", user);
|
|
|
|
|
return "redirect:/";
|
|
|
|
|
return REDIRECT_HOME;
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("error", "邮箱或密码错误");
|
|
|
|
|
return "login";
|
|
|
|
|
model.addAttribute(ERROR_ATTR, "邮箱或密码错误");
|
|
|
|
|
return LOGIN_VIEW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/login/quick/{type}")
|
|
|
|
|
@ -150,7 +161,7 @@ public class WebController {
|
|
|
|
|
if (user != null) {
|
|
|
|
|
session.setAttribute("user", user);
|
|
|
|
|
}
|
|
|
|
|
return "redirect:/";
|
|
|
|
|
return REDIRECT_HOME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/logout")
|
|
|
|
|
@ -177,7 +188,7 @@ public class WebController {
|
|
|
|
|
model.addAttribute("success", "注册成功,请等待审核");
|
|
|
|
|
return "login";
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("error", "注册失败,邮箱可能已存在");
|
|
|
|
|
model.addAttribute(ERROR_ATTR, "注册失败,邮箱可能已存在");
|
|
|
|
|
return "register";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -238,7 +249,7 @@ public class WebController {
|
|
|
|
|
if (success) {
|
|
|
|
|
return "redirect:/books?success=true";
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("error", "图书添加失败");
|
|
|
|
|
model.addAttribute(ERROR_ATTR, "图书添加失败");
|
|
|
|
|
return "add-book";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -267,7 +278,7 @@ public class WebController {
|
|
|
|
|
if (success) {
|
|
|
|
|
return "redirect:/loans?borrowSuccess=true";
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("error", "借阅失败,图书可能不存在或已被借出");
|
|
|
|
|
model.addAttribute(ERROR_ATTR, "借阅失败,图书可能不存在或已被借出");
|
|
|
|
|
return "borrow-book";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -283,7 +294,7 @@ public class WebController {
|
|
|
|
|
if (success) {
|
|
|
|
|
return "redirect:/loans?returnSuccess=true";
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("error", "归还失败");
|
|
|
|
|
model.addAttribute(ERROR_ATTR, "归还失败");
|
|
|
|
|
return "return-book";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -330,9 +341,9 @@ public class WebController {
|
|
|
|
|
private ResponseEntity<Resource> downloadJar(String module, String filename) {
|
|
|
|
|
// 尝试多个可能的JAR路径
|
|
|
|
|
String[] possiblePaths = {
|
|
|
|
|
module + "/build/libs/" + filename,
|
|
|
|
|
module + "/build/libs/" + module + "-1.0.0-all.jar",
|
|
|
|
|
module + "/build/libs/" + module + ".jar"
|
|
|
|
|
module + BUILD_LIBS_PATH + filename,
|
|
|
|
|
module + BUILD_LIBS_PATH + module + "-1.0.0-all.jar",
|
|
|
|
|
module + BUILD_LIBS_PATH + module + ".jar"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (String pathStr : possiblePaths) {
|
|
|
|
|
@ -365,10 +376,10 @@ public class WebController {
|
|
|
|
|
int end = Math.min(start + size, total);
|
|
|
|
|
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
result.put("books", allBooks.subList(start, end));
|
|
|
|
|
result.put(BOOKS_ATTR, allBooks.subList(start, end));
|
|
|
|
|
result.put("total", total);
|
|
|
|
|
result.put("page", page);
|
|
|
|
|
result.put("totalPages", (int) Math.ceil((double) total / size));
|
|
|
|
|
result.put(TOTAL_PAGES_ATTR, (int) Math.ceil((double) total / size));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -456,9 +467,9 @@ public class WebController {
|
|
|
|
|
public Map<String, Object> getAiProviders() {
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
result.put("providers", List.of(
|
|
|
|
|
Map.of("id", "deepseek", "name", "DeepSeek", "model", "deepseek-chat"),
|
|
|
|
|
Map.of("id", "zhipu", "name", "智谱AI", "model", "GLM-4"),
|
|
|
|
|
Map.of("id", "mock", "name", "演示模式", "model", "Mock")
|
|
|
|
|
Map.of("id", "deepseek", "name", "DeepSeek", MODEL_ATTR, "deepseek-chat"),
|
|
|
|
|
Map.of("id", "zhipu", "name", "智谱AI", MODEL_ATTR, "GLM-4"),
|
|
|
|
|
Map.of("id", "mock", "name", "演示模式", MODEL_ATTR, "Mock")
|
|
|
|
|
));
|
|
|
|
|
result.put("default", "deepseek");
|
|
|
|
|
return result;
|
|
|
|
|
|