AdminLoginServiceImpl.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.haha.admin.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.haha.admin.service.AdminLoginService;
  5. import com.haha.common.vo.LoginVO;
  6. import com.haha.common.vo.Result;
  7. import com.haha.common.vo.UserVO;
  8. import com.haha.entity.Admin;
  9. import com.haha.mapper.AdminMapper;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  13. import org.springframework.stereotype.Service;
  14. import java.time.LocalDateTime;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * 管理员登录服务实现
  19. */
  20. @Slf4j
  21. @Service
  22. public class AdminLoginServiceImpl implements AdminLoginService {
  23. @Autowired
  24. private AdminMapper adminMapper;
  25. private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  26. @Override
  27. public Result<LoginVO> login(String username, String password) {
  28. try {
  29. log.info("管理员登录尝试: username={}", username);
  30. // 参数校验
  31. if (username == null || username.trim().isEmpty()) {
  32. return Result.error(400, "用户名不能为空");
  33. }
  34. if (password == null || password.trim().isEmpty()) {
  35. return Result.error(400, "密码不能为空");
  36. }
  37. // ====== 临时方案:硬编码测试账号(仅用于开发测试,生产环境必须移除) ======
  38. if ("admin".equals(username) && "admin123".equals(password)) {
  39. log.warn("【临时测试账号登录】username={} - 此逻辑仅用于开发测试,生产环境必须移除", username);
  40. // 创建临时管理员对象
  41. Admin admin = new Admin();
  42. admin.setId(999L);
  43. admin.setUsername("admin");
  44. admin.setRealName("临时测试管理员");
  45. admin.setPhone("13800138000");
  46. admin.setEmail("admin@haha.com");
  47. admin.setAvatar(null);
  48. admin.setDepartment("技术部");
  49. admin.setStatus(1);
  50. admin.setRoleIds("1");
  51. // 使用Sa-Token进行登录,生成token
  52. StpUtil.login(admin.getId());
  53. String token = StpUtil.getTokenValue();
  54. // 在Session中存储用户信息
  55. StpUtil.getSession().set("username", admin.getUsername());
  56. StpUtil.getSession().set("realName", admin.getRealName());
  57. StpUtil.getSession().set("roleIds", admin.getRoleIds());
  58. log.info("【临时测试账号】登录成功: username={}, userId={}", username, admin.getId());
  59. // 构建返回数据
  60. UserVO userVO = UserVO.builder()
  61. .id(admin.getId())
  62. .nickname(admin.getRealName())
  63. .avatar(admin.getAvatar())
  64. .phone(admin.getPhone())
  65. .build();
  66. LoginVO loginVO = LoginVO.builder()
  67. .token(token)
  68. .userInfo(userVO)
  69. .build();
  70. return Result.success("登录成功", loginVO);
  71. }
  72. // ====== 以上为临时测试逻辑,生产环境必须移除 ======
  73. // 查询管理员信息
  74. LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
  75. wrapper.eq(Admin::getUsername, username);
  76. Admin admin = adminMapper.selectOne(wrapper);
  77. if (admin == null) {
  78. log.warn("登录失败: 用户名不存在 username={}", username);
  79. return Result.error(401, "用户名或密码错误");
  80. }
  81. // 检查账号状态
  82. if (admin.getStatus() != 1) {
  83. log.warn("登录失败: 账号已禁用 username={}", username);
  84. return Result.error(403, "账号已被禁用,请联系管理员");
  85. }
  86. // 验证密码
  87. if (!passwordEncoder.matches(password, admin.getPassword())) {
  88. log.warn("登录失败: 密码错误 username={}", username);
  89. return Result.error(401, "用户名或密码错误");
  90. }
  91. // 更新最后登录时间和IP(这里简化处理,实际应从HttpServletRequest获取IP)
  92. admin.setLastLoginTime(LocalDateTime.now());
  93. admin.setLastLoginIp("127.0.0.1");
  94. adminMapper.updateById(admin);
  95. // 使用Sa-Token进行登录,生成token
  96. StpUtil.login(admin.getId());
  97. String token = StpUtil.getTokenValue();
  98. // 在Session中存储用户信息
  99. StpUtil.getSession().set("username", admin.getUsername());
  100. StpUtil.getSession().set("realName", admin.getRealName());
  101. StpUtil.getSession().set("roleIds", admin.getRoleIds());
  102. log.info("管理员登录成功: username={}, userId={}", username, admin.getId());
  103. // 构建返回数据
  104. UserVO userVO = UserVO.builder()
  105. .id(admin.getId())
  106. .nickname(admin.getRealName() != null ? admin.getRealName() : admin.getUsername())
  107. .avatar(admin.getAvatar())
  108. .phone(admin.getPhone())
  109. .build();
  110. LoginVO loginVO = LoginVO.builder()
  111. .token(token)
  112. .userInfo(userVO)
  113. .build();
  114. return Result.success("登录成功", loginVO);
  115. } catch (Exception e) {
  116. log.error("登录异常: username={}, error={}", username, e.getMessage(), e);
  117. return Result.error(500, "登录失败: " + e.getMessage());
  118. }
  119. }
  120. @Override
  121. public Result<Map<String, Object>> getAdminInfo(String adminId) {
  122. try {
  123. // 查询管理员信息
  124. Admin admin = adminMapper.selectById(adminId);
  125. if (admin == null) {
  126. return Result.error(404, "管理员不存在");
  127. }
  128. Map<String, Object> info = new HashMap<>();
  129. info.put("id", admin.getId());
  130. info.put("username", admin.getUsername());
  131. info.put("realName", admin.getRealName());
  132. info.put("phone", admin.getPhone());
  133. info.put("email", admin.getEmail());
  134. info.put("avatar", admin.getAvatar());
  135. info.put("department", admin.getDepartment());
  136. info.put("roleIds", admin.getRoleIds());
  137. return Result.success("获取成功", info);
  138. } catch (Exception e) {
  139. log.error("获取管理员信息异常: adminId={}, error={}", adminId, e.getMessage(), e);
  140. return Result.error(500, "获取信息失败: " + e.getMessage());
  141. }
  142. }
  143. }