|
|
|
@ -0,0 +1,121 @@
|
|
|
|
|
package com.example.api.controller;
|
|
|
|
|
|
|
|
|
|
import com.example.api.exception.AccountAndPasswordError;
|
|
|
|
|
import com.example.api.model.dto.LoginDto;
|
|
|
|
|
import com.example.api.model.entity.Admin;
|
|
|
|
|
import com.example.api.model.entity.LoginLog;
|
|
|
|
|
import com.example.api.model.enums.Role;
|
|
|
|
|
import com.example.api.model.support.ResponseResult;
|
|
|
|
|
import com.example.api.repository.AdminRepository;
|
|
|
|
|
import com.example.api.service.AdminService;
|
|
|
|
|
import com.example.api.service.LoginLogService;
|
|
|
|
|
import com.example.api.utils.JwtTokenUtil;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
// 定义RestController注解,表示该类是一个控制器,用于处理HTTP请求
|
|
|
|
|
@RestController
|
|
|
|
|
// 定义RequestMapping注解,设置该控制器的基础URL路径
|
|
|
|
|
@RequestMapping("/api/admin")
|
|
|
|
|
// 使用@Slf4j注解来自动生成日志对象
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class AdminController {
|
|
|
|
|
|
|
|
|
|
// 使用LoggerFactory获取当前类的日志对象
|
|
|
|
|
Logger logger = LoggerFactory.getLogger(AdminController.class);
|
|
|
|
|
|
|
|
|
|
// 通过@Resource注解自动注入AdminService
|
|
|
|
|
@Resource
|
|
|
|
|
private AdminService adminService;
|
|
|
|
|
|
|
|
|
|
// 通过@Resource注解自动注入AdminRepository
|
|
|
|
|
@Resource
|
|
|
|
|
private AdminRepository adminRepository;
|
|
|
|
|
|
|
|
|
|
// 通过@Resource注解自动注入LoginLogService
|
|
|
|
|
@Resource
|
|
|
|
|
private LoginLogService loginLogService;
|
|
|
|
|
|
|
|
|
|
// 检查是否有初始化管理员账号
|
|
|
|
|
@GetMapping("hasInit")
|
|
|
|
|
public boolean hasInit() {
|
|
|
|
|
return adminRepository.existsAdminByRoles(Role.ROLE_SUPER_ADMIN.getValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化管理员账号
|
|
|
|
|
@PostMapping("/init")
|
|
|
|
|
public Admin init(@RequestBody Admin admin) throws Exception {
|
|
|
|
|
admin.setRoles(Role.ROLE_SUPER_ADMIN.getValue());
|
|
|
|
|
return adminService.save(admin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取所有管理员账号信息
|
|
|
|
|
@GetMapping("")
|
|
|
|
|
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN' ,'ROLE_ADMIN')")
|
|
|
|
|
public List<Admin> findAll() {
|
|
|
|
|
return adminService.findAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除管理员账号
|
|
|
|
|
@DeleteMapping("")
|
|
|
|
|
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN' ,'ROLE_ADMIN')")
|
|
|
|
|
public void delete(String id) {
|
|
|
|
|
adminService.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存管理员账号信息
|
|
|
|
|
@PostMapping("")
|
|
|
|
|
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN' ,'ROLE_ADMIN')")
|
|
|
|
|
public Admin save(@RequestBody Admin admin) throws Exception {
|
|
|
|
|
return adminService.save(admin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 管理员登录
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
public Map<String, Object> loginByEmail(String type, @RequestBody LoginDto dto, HttpServletRequest request) throws Exception {
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
Admin admin = null;
|
|
|
|
|
String token = null;
|
|
|
|
|
try {
|
|
|
|
|
// 根据登录类型(邮箱或密码)进行登录
|
|
|
|
|
admin = type.equals("email") ? adminService.loginByEmail(dto) : adminService.loginByPassword(dto);
|
|
|
|
|
// 创建JWT令牌
|
|
|
|
|
token = adminService.createToken(admin,
|
|
|
|
|
dto.isRemember() ? JwtTokenUtil.REMEMBER_EXPIRATION_TIME : JwtTokenUtil.EXPIRATION_TIME);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 抛出异常,表示邮箱或密码错误
|
|
|
|
|
throw new Exception("邮箱或密码错误");
|
|
|
|
|
} finally {
|
|
|
|
|
// 记录登录日志
|
|
|
|
|
loginLogService.recordLog(dto, admin, request);
|
|
|
|
|
}
|
|
|
|
|
// 将管理员信息和令牌放入返回结果
|
|
|
|
|
map.put("admin", admin);
|
|
|
|
|
map.put("token", token);
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送邮箱验证码
|
|
|
|
|
@GetMapping("/sendEmail")
|
|
|
|
|
public ResponseResult sendEmail(String email) throws Exception {
|
|
|
|
|
Boolean flag = adminService.sendEmail(email);
|
|
|
|
|
ResponseResult res = new ResponseResult();
|
|
|
|
|
if (flag) {
|
|
|
|
|
res.setMsg("发送成功,请登录邮箱查看");
|
|
|
|
|
} else {
|
|
|
|
|
res.setMsg("发送验证码失败,请检查邮箱服务");
|
|
|
|
|
}
|
|
|
|
|
res.setStatus(flag);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|