This commit is contained in:
王子琦
2026-01-20 14:05:18 +08:00
parent 438eb0b635
commit ec6ec210d2
15 changed files with 132 additions and 28 deletions

View File

@@ -14,11 +14,11 @@ public class ApiResponse<T> {
}
public static <T> ApiResponse<T> success(T data) {
return new ApiResponse<>(0, "ok", data);
return new ApiResponse<>(0, "成功", data);
}
public static <T> ApiResponse<T> success() {
return new ApiResponse<>(0, "ok", null);
return new ApiResponse<>(0, "成功", null);
}
public static <T> ApiResponse<T> error(String message) {

View File

@@ -14,7 +14,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ApiResponse<Void> handleValid(MethodArgumentNotValidException ex) {
String msg = ex.getBindingResult().getAllErrors().isEmpty()
? "validation error" : ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
? "参数校验失败" : ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
return ApiResponse.error(msg);
}

View File

@@ -31,13 +31,13 @@ public class AuthController {
public ApiResponse<Map<String, Object>> login(@Valid @RequestBody LoginRequest request) {
User user = userService.findByUsername(request.getUsername());
if (user == null) {
throw new ApiException("user not found");
throw new ApiException("账号不存在");
}
if (user.getStatus() != null && user.getStatus() == 0) {
throw new ApiException("user disabled");
throw new ApiException("账号已被禁用");
}
if (!PasswordUtil.matches(request.getPassword(), user.getPassword())) {
throw new ApiException("invalid password");
throw new ApiException("密码错误");
}
if (!PasswordUtil.isBcrypt(user.getPassword())) {
userService.updatePassword(user.getId(), PasswordUtil.hash(request.getPassword()));

View File

@@ -73,7 +73,7 @@ public class FamilyController {
}
}
if (!allowed) {
throw new ApiException("no access to this elder");
throw new ApiException("无权访问该亲属");
}
return ApiResponse.success(careRecordMapper.listByElderId(elderId));
}
@@ -89,7 +89,7 @@ public class FamilyController {
}
}
if (!allowed) {
throw new ApiException("no access to this elder");
throw new ApiException("无权访问该亲属");
}
return ApiResponse.success(healthRecordMapper.listByElderId(elderId));
}
@@ -105,7 +105,7 @@ public class FamilyController {
}
}
if (!allowed) {
throw new ApiException("no access to this elder");
throw new ApiException("无权访问该亲属");
}
return ApiResponse.success(billService.listByElderId(elderId));
}
@@ -115,7 +115,7 @@ public class FamilyController {
Long familyId = Long.valueOf(StpUtil.getLoginId().toString());
Bill bill = billMapper.findById(id);
if (bill == null) {
throw new ApiException("bill not found");
throw new ApiException("账单不存在");
}
boolean allowed = false;
for (FamilyElder relation : familyService.listRelations(familyId)) {
@@ -125,10 +125,10 @@ public class FamilyController {
}
}
if (!allowed) {
throw new ApiException("no access to this bill");
throw new ApiException("无权访问该账单");
}
if ("PAID".equals(bill.getStatus())) {
throw new ApiException("bill already paid");
throw new ApiException("账单已支付");
}
BigDecimal amount = bill.getTotal() == null ? BigDecimal.ZERO : bill.getTotal();
billService.payBill(id, familyId, request.getMethod(), amount);

View File

@@ -21,7 +21,7 @@ public class FileController {
@PostMapping("/upload")
public ApiResponse<Map<String, Object>> upload(@RequestParam("file") MultipartFile file) throws IOException {
if (file.isEmpty()) {
return ApiResponse.error("empty file");
return ApiResponse.error("文件为空");
}
String original = file.getOriginalFilename();
String ext = StringUtils.getFilenameExtension(original);

View File

@@ -3,9 +3,9 @@ package com.nursinghome.dto;
import jakarta.validation.constraints.NotBlank;
public class LoginRequest {
@NotBlank(message = "username required")
@NotBlank(message = "账号不能为空")
private String username;
@NotBlank(message = "password required")
@NotBlank(message = "密码不能为空")
private String password;
public String getUsername() {

View File

@@ -3,14 +3,14 @@ package com.nursinghome.dto;
import jakarta.validation.constraints.NotBlank;
public class RegisterRequest {
@NotBlank(message = "username required")
@NotBlank(message = "账号不能为空")
private String username;
@NotBlank(message = "password required")
@NotBlank(message = "密码不能为空")
private String password;
@NotBlank(message = "name required")
@NotBlank(message = "姓名不能为空")
private String name;
private String phone;
@NotBlank(message = "elderIdCard required")
@NotBlank(message = "亲属身份证号不能为空")
private String elderIdCard;
private String relationship;

View File

@@ -29,7 +29,7 @@ public class ElderService {
public Elder create(Elder elder) {
if (elderMapper.findByIdCard(elder.getIdCard()) != null) {
throw new ApiException("id card already exists");
throw new ApiException("身份证号已存在");
}
elderMapper.insert(elder);
return elder;

View File

@@ -25,7 +25,7 @@ public class FamilyService {
public User registerFamily(String username, String password, String name, String phone, String elderIdCard, String relationship) {
Elder elder = elderService.findByIdCard(elderIdCard);
if (elder == null) {
throw new ApiException("elder not found");
throw new ApiException("未找到该亲属");
}
User user = new User();
user.setUsername(username);

View File

@@ -30,7 +30,7 @@ public class UserService {
public User createUser(User user, boolean hashPassword) {
if (userMapper.findByUsername(user.getUsername()) != null) {
throw new ApiException("username already exists");
throw new ApiException("账号已存在");
}
if (hashPassword) {
user.setPassword(PasswordUtil.hash(user.getPassword()));