AdminServiceImpl.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.om.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.om.constant.RedisConstant;
  6. import com.om.entity.dto.AdminAddDTO;
  7. import com.om.entity.dto.AdminQueryPageDTO;
  8. import com.om.entity.po.Admin;
  9. import com.om.entity.po.User;
  10. import com.om.entity.vo.AdminLoginVO;
  11. import com.om.entity.vo.AdminQueryPageVO;
  12. import com.om.exception.BadReqException;
  13. import com.om.exception.BizException;
  14. import com.om.mapper.AdminMapper;
  15. import com.om.service.IAdminService;
  16. import com.om.utils.AdminContext;
  17. import com.om.utils.JwtUtils;
  18. import com.om.utils.Result;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.data.redis.core.RedisTemplate;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.DigestUtils;
  23. import javax.annotation.Resource;
  24. import javax.servlet.ServletException;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. import java.io.IOException;
  28. import java.time.LocalDateTime;
  29. import java.util.*;
  30. import java.util.concurrent.TimeUnit;
  31. /**
  32. * <p>
  33. * 服务实现类
  34. * </p>
  35. *
  36. * @author bmmx
  37. * @since 2024-01-26
  38. */
  39. @Service
  40. public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
  41. @Resource
  42. private JwtUtils jwtUtils;
  43. @Resource
  44. private RedisTemplate redisTemplate;
  45. @Override
  46. public Result<AdminLoginVO> login(String username, String password) {
  47. if (StringUtils.isBlank(username)){
  48. throw new BadReqException("用户名不能为空!");
  49. }
  50. if (StringUtils.isBlank(password)){
  51. throw new BadReqException("密码不能为空!");
  52. }
  53. //根据username 查询数据库
  54. Admin admin = this.lambdaQuery()
  55. .eq(Admin::getUsername, username)
  56. .one();
  57. //判断是否存在
  58. if (BeanUtil.isEmpty(admin)){
  59. throw new BadReqException("该管理员不存在!请检查用户名是否正确!");
  60. }
  61. //密码加密
  62. password = DigestUtils.md5DigestAsHex(password.getBytes());
  63. //判断密码是否正确
  64. if (!StringUtils.equals(password,admin.getPassword())){
  65. throw new BizException("密码不正确,请检查用户名和密码是否正确!");
  66. }
  67. //判断管理员状态
  68. if (!admin.getEnabled()){
  69. throw new BadReqException("该管理员已被禁用!请联系系统管理员!!!");
  70. }
  71. //封装vo返回
  72. AdminLoginVO adminLoginVO = BeanUtil.copyProperties(admin, AdminLoginVO.class);
  73. //生成token
  74. Map<String, Object> claims = new HashMap<>();
  75. claims.put("a_id",admin.getId());
  76. String token = jwtUtils.generateToken(claims);
  77. //把token存入到redis中
  78. String key = RedisConstant.ADMIN_TOKEN_PREFIX + admin.getId();
  79. redisTemplate.opsForValue().set(key,token,RedisConstant.ADMIN_TOKEN_TTL, TimeUnit.SECONDS);
  80. adminLoginVO.setLoginToken(token);
  81. return Result.ok(adminLoginVO);
  82. }
  83. @Override
  84. public Result<AdminLoginVO> getManagerByToken(String token) {
  85. //根据token获取管理员id
  86. Integer adminId = jwtUtils.getAdminIdFromToken(token);
  87. //根据id查询管理员信息
  88. Admin admin = this.getById(adminId);
  89. //判断是否为空
  90. if (BeanUtil.isEmpty(admin)){
  91. throw new BadReqException("该管理员不存在,请重试!");
  92. }
  93. AdminLoginVO adminLoginVO = BeanUtil.copyProperties(admin, AdminLoginVO.class);
  94. adminLoginVO.setLoginToken(token);
  95. return Result.ok(adminLoginVO);
  96. }
  97. @Override
  98. public Result add(AdminAddDTO dto) {
  99. //获取当前登录管理员id
  100. Integer adminId = AdminContext.getAdminId();
  101. String username = dto.getUsername();
  102. String password = dto.getPassword();
  103. //判断dto中的数据是否为空
  104. if(StringUtils.isEmpty(username)){
  105. throw new BadReqException("管理员用户名为空");
  106. }
  107. if(StringUtils.isEmpty(password)){
  108. throw new BadReqException("管理员密码为空");
  109. }
  110. //判断dto中的用户名是否存在
  111. Admin one = this.lambdaQuery()
  112. .eq(Admin::getUsername, username)
  113. .one();
  114. if (!BeanUtil.isEmpty(one)){
  115. throw new BadReqException("该用户已经存在,请检查用户名");
  116. }
  117. LocalDateTime now = LocalDateTime.now();
  118. //密码加密
  119. password = DigestUtils.md5DigestAsHex(password.getBytes());
  120. Admin admin = BeanUtil.copyProperties(dto, Admin.class);
  121. admin.setPassword(password);
  122. admin.setOperatorId(adminId);
  123. admin.setCreateTime(now);
  124. admin.setUpdateTime(now);
  125. //保存到数据库
  126. this.save(admin);
  127. return Result.ok();
  128. }
  129. @Override
  130. public Result delete(Integer id) {
  131. //根据id查询管理员
  132. Admin admin = this.getById(id);
  133. //判断该管理员是否存在
  134. if (BeanUtil.isEmpty(admin)){
  135. throw new BadReqException("该管理员不存在");
  136. }
  137. //删除该管理员
  138. this.removeById(id);
  139. return Result.ok();
  140. }
  141. @Override
  142. public Result<AdminLoginVO> get(Integer id) {
  143. //根据id查询管理员
  144. Admin admin = this.getById(id);
  145. //判断该管理员是否存在
  146. if (BeanUtil.isEmpty(admin)){
  147. throw new BadReqException("该管理员不存在");
  148. }
  149. AdminLoginVO adminLoginVO = BeanUtil.copyProperties(admin, AdminLoginVO.class);
  150. //从redis中获取token
  151. String tokenKey = RedisConstant.ADMIN_TOKEN_PREFIX +id;
  152. String redisToken = (String) redisTemplate.opsForValue().get(tokenKey);
  153. adminLoginVO.setLoginToken(redisToken);
  154. return Result.ok(adminLoginVO);
  155. }
  156. @Override
  157. public Result<AdminLoginVO> updateAdmin(AdminAddDTO dto) {
  158. //获取当前登录管理员id
  159. Integer adminId = AdminContext.getAdminId();
  160. String username = dto.getUsername();
  161. String password = dto.getPassword();
  162. //判断dto中的数据是否为空
  163. if(StringUtils.isEmpty(username)){
  164. throw new BadReqException("管理员用户名为空");
  165. }
  166. //判断dto中的用户名是否存在
  167. Admin one = this.lambdaQuery()
  168. .eq(Admin::getUsername, username)
  169. .one();
  170. if (BeanUtil.isEmpty(one)){
  171. throw new BadReqException("该用户不存在,请检查用户名");
  172. }
  173. LocalDateTime now = LocalDateTime.now();
  174. //密码加密
  175. //如果密码密码为空则 不修改密码 使用原密码
  176. if (StringUtils.isEmpty(password)){
  177. password = one.getPassword();
  178. }else {
  179. password = DigestUtils.md5DigestAsHex(password.getBytes());
  180. }
  181. Admin admin = BeanUtil.copyProperties(dto, Admin.class);
  182. admin.setPassword(password);
  183. admin.setOperatorId(adminId);
  184. admin.setUpdateTime(now);
  185. //更新到数据库
  186. this.updateById(admin);
  187. //封装vo
  188. AdminLoginVO adminLoginVO = BeanUtil.copyProperties(admin, AdminLoginVO.class);
  189. //从redis中获取token
  190. String tokenKey = RedisConstant.ADMIN_TOKEN_PREFIX +adminId;
  191. String redisToken = (String) redisTemplate.opsForValue().get(tokenKey);
  192. adminLoginVO.setLoginToken(redisToken);
  193. return Result.ok(adminLoginVO);
  194. }
  195. @Override
  196. public Result updateState(Integer id) {
  197. //根据id查询管理员
  198. Admin admin = this.getById(id);
  199. //判断该管理员是否存在
  200. if (BeanUtil.isEmpty(admin)){
  201. throw new BadReqException("该管理员不存在");
  202. }
  203. admin.setEnabled(!admin.getEnabled());
  204. this.updateById(admin);
  205. return Result.ok();
  206. }
  207. @Override
  208. public Result logout(Integer id) {
  209. //把redis中的token删除
  210. //从redis中获取token
  211. String tokenKey = RedisConstant.ADMIN_TOKEN_PREFIX +id;
  212. if (!redisTemplate.hasKey(tokenKey)) {
  213. throw new BadReqException("该管理员的token不存在");
  214. }
  215. redisTemplate.delete(tokenKey);
  216. return Result.ok();
  217. }
  218. @Override
  219. public Result<AdminQueryPageVO> getPageList(AdminQueryPageDTO dto) {
  220. //获取dto中的数据
  221. Integer pageIndex = dto.getPageIndex();
  222. Integer pageSize = dto.getPageSize();
  223. //分页查询
  224. Page<Admin> page = this.lambdaQuery()
  225. .like(dto.getSearch() != null, Admin::getUsername, dto.getSearch())
  226. .orderByDesc(Admin::getCreateTime)
  227. .page(new Page<>(pageIndex, pageSize));
  228. AdminQueryPageVO adminQueryPageVO = new AdminQueryPageVO();
  229. adminQueryPageVO.setCurrent((int) page.getCurrent());
  230. adminQueryPageVO.setSize(pageSize);
  231. adminQueryPageVO.setPages((int) page.getPages());
  232. if (dto.getSearch() != null){
  233. adminQueryPageVO.setSearchCount(true);
  234. }
  235. List<Admin> records = page.getRecords();
  236. List<AdminLoginVO> list = new ArrayList<>();
  237. for (Admin record : records) {
  238. AdminLoginVO adminLoginVO = BeanUtil.copyProperties(record, AdminLoginVO.class);
  239. list.add(adminLoginVO);
  240. }
  241. adminQueryPageVO.setRecords(list);
  242. adminQueryPageVO.setTotal((int) page.getTotal());
  243. return Result.ok(adminQueryPageVO);
  244. }
  245. }