修改admin的role相关代码 #62

Merged
hnu202326010106 merged 1 commits from wanglei_branch into develop 1 month ago

@ -8,27 +8,25 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/web/admin")
@RequiredArgsConstructor
@Tag(name = "管理员管理接口", description = "Web管理端管理员列表查询接口")
@Tag(name = "管理员管理接口", description = "Web管理端管理员操作接口")
public class AdminController {
private final AdminService adminService;
/**
*
*
*/
@GetMapping("/list")
@PreAuthorize("hasRole('ADMIN')") // 仅管理员可访问
@Operation(summary = "获取管理员列表", description = "支持按姓名搜索管理员")
@PreAuthorize("hasRole('ADMIN')")
@Operation(summary = "获取管理员列表", description = "支持按姓名模糊搜索仅返回Admin角色管理员")
public ResponseEntity<ResultVO<List<Admin>>> getAdminList(
@RequestParam(required = false) String name
) {
@ -41,7 +39,54 @@ public class AdminController {
}
return ResponseEntity.ok(ResultVO.success(adminList));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, "查询管理员列表失败: " + e.getMessage()));
return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage()));
}
}
/**
* /
*/
@PostMapping("/save")
@PreAuthorize("hasRole('ADMIN')")
@Operation(summary = "保存管理员", description = "新增/编辑管理员角色强制为Admin")
public ResponseEntity<ResultVO<Admin>> saveAdmin(@RequestBody Admin admin) {
try {
Admin savedAdmin = adminService.saveAdmin(admin);
return ResponseEntity.ok(ResultVO.success(savedAdmin));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, "保存失败:" + e.getMessage()));
}
}
/**
*
*/
@DeleteMapping("/{adminId}")
@PreAuthorize("hasRole('ADMIN')")
@Operation(summary = "删除管理员", description = "按ID删除管理员")
public ResponseEntity<ResultVO<Void>> deleteAdmin(@PathVariable String adminId) {
try {
adminService.deleteAdmin(adminId);
return ResponseEntity.ok(ResultVO.success(null));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, "删除失败:" + e.getMessage()));
}
}
/**
*
*/
@PostMapping("/login")
@Operation(summary = "管理员登录", description = "用户名+密码验证")
public ResponseEntity<ResultVO<Admin>> login(
@RequestParam String adminName,
@RequestParam String password
) {
Optional<Admin> admin = adminService.login(adminName, password);
if (admin.isPresent()) {
return ResponseEntity.ok(ResultVO.success(admin.get()));
} else {
return ResponseEntity.ok(ResultVO.error(401, "用户名或密码错误"));
}
}
}

@ -28,11 +28,21 @@ public class Admin {
@Column(name = "phone", length = 20)
private String phone;
@Enumerated(EnumType.STRING)
@Column(name = "role", length = 50)
private AdminRole role = AdminRole.Admin;
@Column(name = "created_time")
private LocalDateTime createdTime = LocalDateTime.now();
@Column(name = "updated_time")
private LocalDateTime updatedTime = LocalDateTime.now();
public enum AdminRole {
Admin
}
}

@ -9,24 +9,23 @@ import java.util.Optional;
@Repository
public interface AdminRepository extends JpaRepository<Admin, String> {
// 登录核心方法
// 登录核心方法:按用户名查询
Optional<Admin> findByAdminName(String adminName);
// 根据管理员ID查询
// 管理员ID查询
Optional<Admin> findByAdminId(String adminId);
// 根据管理员姓名模糊查询
// 姓名模糊查询
List<Admin> findByAdminNameContaining(String adminName);
// 根据手机号查询管理员
// 按手机号查询
Optional<Admin> findByPhone(String phone);
// 检查管理员ID是否存在
// 检查唯一约束
boolean existsByAdminId(String adminId);
// 检查手机号是否存在
boolean existsByPhone(String phone);
// 检查用户名是否存在
boolean existsByAdminName(String adminName);
// 可选若需按角色过滤仅Admin角色保留此方法单角色下可省略
List<Admin> findByRole(Admin.AdminRole role);
}

@ -5,7 +5,9 @@ import com.campus.water.mapper.AdminRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor
@ -14,16 +16,53 @@ public class AdminService {
private final AdminRepository adminRepository;
/**
*
* Admin
*/
public List<Admin> getAdminList() {
// 单角色下直接查全部也可调用findByRole(Admin.AdminRole.Admin)
return adminRepository.findAll();
}
/**
*
*
*/
public List<Admin> searchAdminsByName(String name) {
return adminRepository.findByAdminNameContaining(name);
}
/**
* ID
*/
public Optional<Admin> getAdminById(String adminId) {
return adminRepository.findByAdminId(adminId);
}
/**
* /Admin
*/
public Admin saveAdmin(Admin admin) {
// 强制设置为Admin角色避免手动修改
admin.setRole(Admin.AdminRole.Admin);
admin.setUpdatedTime(LocalDateTime.now());
if (admin.getCreatedTime() == null) {
admin.setCreatedTime(LocalDateTime.now());
}
return adminRepository.save(admin);
}
/**
*
*/
public void deleteAdmin(String adminId) {
adminRepository.deleteById(adminId);
}
/**
*
*/
public Optional<Admin> login(String adminName, String password) {
Optional<Admin> admin = adminRepository.findByAdminName(adminName);
// 此处仅示例实际需结合密码加密如BCrypt验证
return admin.filter(a -> a.getPassword().equals(password));
}
}

@ -12,6 +12,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
@Service
public class RegisterService {
@ -50,17 +51,28 @@ public class RegisterService {
}
// 修正管理员注册逻辑适配新实体Admin
// 原RegisterService中handleAdminRegister方法修改
private void handleAdminRegister(String username, String password, RegisterRequest request) {
// 检查用户名是否已存在
// 检查用户名/ID/手机号是否已存在
if (adminRepository.existsByAdminName(username)) {
throw new RuntimeException("管理员用户名已存在");
}
if (adminRepository.existsByAdminId(request.getAdminId())) {
throw new RuntimeException("管理员ID已存在");
}
if (request.getPhone() != null && adminRepository.existsByPhone(request.getPhone())) {
throw new RuntimeException("手机号已被注册");
}
// 构建管理员对象默认角色为Admin
Admin admin = new Admin();
admin.setAdminId(request.getAdminId());
admin.setAdminName(username);
admin.setPassword(password);
admin.setPhone(request.getPhone()); // 假设请求中有电话字段
admin.setPassword(password); // 实际需加密如BCrypt
admin.setPhone(request.getPhone());
admin.setRole(Admin.AdminRole.Admin); // 强制设置为Admin角色
admin.setCreatedTime(LocalDateTime.now());
admin.setUpdatedTime(LocalDateTime.now());
adminRepository.save(admin);
}

Loading…
Cancel
Save