| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package com.haha.admin.service.impl;
- import cn.dev33.satoken.stp.StpUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.haha.admin.service.AdminLoginService;
- import com.haha.common.vo.LoginVO;
- import com.haha.common.vo.Result;
- import com.haha.common.vo.UserVO;
- import com.haha.entity.Admin;
- import com.haha.mapper.AdminMapper;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.stereotype.Service;
- import java.time.LocalDateTime;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 管理员登录服务实现
- */
- @Slf4j
- @Service
- public class AdminLoginServiceImpl implements AdminLoginService {
-
- @Autowired
- private AdminMapper adminMapper;
-
- private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
-
- @Override
- public Result<LoginVO> login(String username, String password) {
- try {
- log.info("管理员登录尝试: username={}", username);
-
- // 参数校验
- if (username == null || username.trim().isEmpty()) {
- return Result.error(400, "用户名不能为空");
- }
- if (password == null || password.trim().isEmpty()) {
- return Result.error(400, "密码不能为空");
- }
-
- // ====== 临时方案:硬编码测试账号(仅用于开发测试,生产环境必须移除) ======
- if ("admin".equals(username) && "admin123".equals(password)) {
- log.warn("【临时测试账号登录】username={} - 此逻辑仅用于开发测试,生产环境必须移除", username);
-
- // 创建临时管理员对象
- Admin admin = new Admin();
- admin.setId(999L);
- admin.setUsername("admin");
- admin.setRealName("临时测试管理员");
- admin.setPhone("13800138000");
- admin.setEmail("admin@haha.com");
- admin.setAvatar(null);
- admin.setDepartment("技术部");
- admin.setStatus(1);
- admin.setRoleIds("1");
-
- // 使用Sa-Token进行登录,生成token
- StpUtil.login(admin.getId());
- String token = StpUtil.getTokenValue();
-
- // 在Session中存储用户信息
- StpUtil.getSession().set("username", admin.getUsername());
- StpUtil.getSession().set("realName", admin.getRealName());
- StpUtil.getSession().set("roleIds", admin.getRoleIds());
-
- log.info("【临时测试账号】登录成功: username={}, userId={}", username, admin.getId());
-
- // 构建返回数据
- UserVO userVO = UserVO.builder()
- .id(admin.getId())
- .nickname(admin.getRealName())
- .avatar(admin.getAvatar())
- .phone(admin.getPhone())
- .build();
-
- LoginVO loginVO = LoginVO.builder()
- .token(token)
- .userInfo(userVO)
- .build();
-
- return Result.success("登录成功", loginVO);
- }
- // ====== 以上为临时测试逻辑,生产环境必须移除 ======
-
- // 查询管理员信息
- LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(Admin::getUsername, username);
- Admin admin = adminMapper.selectOne(wrapper);
-
- if (admin == null) {
- log.warn("登录失败: 用户名不存在 username={}", username);
- return Result.error(401, "用户名或密码错误");
- }
-
- // 检查账号状态
- if (admin.getStatus() != 1) {
- log.warn("登录失败: 账号已禁用 username={}", username);
- return Result.error(403, "账号已被禁用,请联系管理员");
- }
-
- // 验证密码
- if (!passwordEncoder.matches(password, admin.getPassword())) {
- log.warn("登录失败: 密码错误 username={}", username);
- return Result.error(401, "用户名或密码错误");
- }
-
- // 更新最后登录时间和IP(这里简化处理,实际应从HttpServletRequest获取IP)
- admin.setLastLoginTime(LocalDateTime.now());
- admin.setLastLoginIp("127.0.0.1");
- adminMapper.updateById(admin);
-
- // 使用Sa-Token进行登录,生成token
- StpUtil.login(admin.getId());
- String token = StpUtil.getTokenValue();
-
- // 在Session中存储用户信息
- StpUtil.getSession().set("username", admin.getUsername());
- StpUtil.getSession().set("realName", admin.getRealName());
- StpUtil.getSession().set("roleIds", admin.getRoleIds());
-
- log.info("管理员登录成功: username={}, userId={}", username, admin.getId());
-
- // 构建返回数据
- UserVO userVO = UserVO.builder()
- .id(admin.getId())
- .nickname(admin.getRealName() != null ? admin.getRealName() : admin.getUsername())
- .avatar(admin.getAvatar())
- .phone(admin.getPhone())
- .build();
-
- LoginVO loginVO = LoginVO.builder()
- .token(token)
- .userInfo(userVO)
- .build();
-
- return Result.success("登录成功", loginVO);
-
- } catch (Exception e) {
- log.error("登录异常: username={}, error={}", username, e.getMessage(), e);
- return Result.error(500, "登录失败: " + e.getMessage());
- }
- }
-
- @Override
- public Result<Map<String, Object>> getAdminInfo(String adminId) {
- try {
- // 查询管理员信息
- Admin admin = adminMapper.selectById(adminId);
- if (admin == null) {
- return Result.error(404, "管理员不存在");
- }
-
- Map<String, Object> info = new HashMap<>();
- info.put("id", admin.getId());
- info.put("username", admin.getUsername());
- info.put("realName", admin.getRealName());
- info.put("phone", admin.getPhone());
- info.put("email", admin.getEmail());
- info.put("avatar", admin.getAvatar());
- info.put("department", admin.getDepartment());
- info.put("roleIds", admin.getRoleIds());
-
- return Result.success("获取成功", info);
-
- } catch (Exception e) {
- log.error("获取管理员信息异常: adminId={}, error={}", adminId, e.getMessage(), e);
- return Result.error(500, "获取信息失败: " + e.getMessage());
- }
- }
- }
|