|
@@ -1,14 +1,20 @@
|
|
package com.om.service.impl;
|
|
package com.om.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.om.constant.RedisConstant;
|
|
import com.om.constant.RedisConstant;
|
|
|
|
+import com.om.entity.dto.AdminAddDTO;
|
|
|
|
+import com.om.entity.dto.AdminQueryPageDTO;
|
|
import com.om.entity.po.Admin;
|
|
import com.om.entity.po.Admin;
|
|
|
|
+import com.om.entity.po.User;
|
|
import com.om.entity.vo.AdminLoginVO;
|
|
import com.om.entity.vo.AdminLoginVO;
|
|
|
|
+import com.om.entity.vo.AdminQueryPageVO;
|
|
import com.om.exception.BadReqException;
|
|
import com.om.exception.BadReqException;
|
|
import com.om.exception.BizException;
|
|
import com.om.exception.BizException;
|
|
import com.om.mapper.AdminMapper;
|
|
import com.om.mapper.AdminMapper;
|
|
import com.om.service.IAdminService;
|
|
import com.om.service.IAdminService;
|
|
|
|
+import com.om.utils.AdminContext;
|
|
import com.om.utils.JwtUtils;
|
|
import com.om.utils.JwtUtils;
|
|
import com.om.utils.Result;
|
|
import com.om.utils.Result;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -22,9 +28,8 @@ import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.util.Collection;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.util.*;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -46,7 +51,7 @@ public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public Result login(String username, String password) {
|
|
|
|
|
|
+ public Result<AdminLoginVO> login(String username, String password) {
|
|
if (StringUtils.isBlank(username)){
|
|
if (StringUtils.isBlank(username)){
|
|
throw new BadReqException("用户名不能为空!");
|
|
throw new BadReqException("用户名不能为空!");
|
|
}
|
|
}
|
|
@@ -87,6 +92,194 @@ public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements
|
|
return Result.ok(adminLoginVO);
|
|
return Result.ok(adminLoginVO);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public Result<AdminLoginVO> getManagerByToken(String token) {
|
|
|
|
+ //根据token获取管理员id
|
|
|
|
+ Integer adminId = jwtUtils.getAdminIdFromToken(token);
|
|
|
|
+
|
|
|
|
+ //根据id查询管理员信息
|
|
|
|
+ Admin admin = this.getById(adminId);
|
|
|
|
+ //判断是否为空
|
|
|
|
+ if (BeanUtil.isEmpty(admin)){
|
|
|
|
+ throw new BadReqException("该管理员不存在,请重试!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ AdminLoginVO adminLoginVO = BeanUtil.copyProperties(admin, AdminLoginVO.class);
|
|
|
|
+ adminLoginVO.setLoginToken(token);
|
|
|
|
+
|
|
|
|
+ return Result.ok(adminLoginVO);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result add(AdminAddDTO dto) {
|
|
|
|
+ //获取当前登录管理员id
|
|
|
|
+ Integer adminId = AdminContext.getAdminId();
|
|
|
|
+ String username = dto.getUsername();
|
|
|
|
+ String password = dto.getPassword();
|
|
|
|
+ //判断dto中的数据是否为空
|
|
|
|
+ if(StringUtils.isEmpty(username)){
|
|
|
|
+ throw new BadReqException("管理员用户名为空");
|
|
|
|
+ }
|
|
|
|
+ if(StringUtils.isEmpty(password)){
|
|
|
|
+ throw new BadReqException("管理员密码为空");
|
|
|
|
+ }
|
|
|
|
+ //判断dto中的用户名是否存在
|
|
|
|
+ Admin one = this.lambdaQuery()
|
|
|
|
+ .eq(Admin::getUsername, username)
|
|
|
|
+ .one();
|
|
|
|
+ if (!BeanUtil.isEmpty(one)){
|
|
|
|
+ throw new BadReqException("该用户已经存在,请检查用户名");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
+ //密码加密
|
|
|
|
+ password = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
|
|
+ Admin admin = BeanUtil.copyProperties(dto, Admin.class);
|
|
|
|
+ admin.setPassword(password);
|
|
|
|
+ admin.setOperatorId(adminId);
|
|
|
|
+ admin.setCreateTime(now);
|
|
|
|
+ admin.setUpdateTime(now);
|
|
|
|
+ //保存到数据库
|
|
|
|
+ this.save(admin);
|
|
|
|
+
|
|
|
|
+ return Result.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result delete(Integer id) {
|
|
|
|
+ //根据id查询管理员
|
|
|
|
+ Admin admin = this.getById(id);
|
|
|
|
+ //判断该管理员是否存在
|
|
|
|
+ if (BeanUtil.isEmpty(admin)){
|
|
|
|
+ throw new BadReqException("该管理员不存在");
|
|
|
|
+ }
|
|
|
|
+ //删除该管理员
|
|
|
|
+ this.removeById(id);
|
|
|
|
+ return Result.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result<AdminLoginVO> get(Integer id) {
|
|
|
|
+ //根据id查询管理员
|
|
|
|
+ Admin admin = this.getById(id);
|
|
|
|
+ //判断该管理员是否存在
|
|
|
|
+ if (BeanUtil.isEmpty(admin)){
|
|
|
|
+ throw new BadReqException("该管理员不存在");
|
|
|
|
+ }
|
|
|
|
+ AdminLoginVO adminLoginVO = BeanUtil.copyProperties(admin, AdminLoginVO.class);
|
|
|
|
+
|
|
|
|
+ //从redis中获取token
|
|
|
|
+ String tokenKey = RedisConstant.ADMIN_TOKEN_PREFIX +id;
|
|
|
|
+ String redisToken = (String) redisTemplate.opsForValue().get(tokenKey);
|
|
|
|
+ adminLoginVO.setLoginToken(redisToken);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return Result.ok(adminLoginVO);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result<AdminLoginVO> updateAdmin(AdminAddDTO dto) {
|
|
|
|
+ //获取当前登录管理员id
|
|
|
|
+ Integer adminId = AdminContext.getAdminId();
|
|
|
|
+ String username = dto.getUsername();
|
|
|
|
+ String password = dto.getPassword();
|
|
|
|
+ //判断dto中的数据是否为空
|
|
|
|
+ if(StringUtils.isEmpty(username)){
|
|
|
|
+ throw new BadReqException("管理员用户名为空");
|
|
|
|
+ }
|
|
|
|
+ //判断dto中的用户名是否存在
|
|
|
|
+ Admin one = this.lambdaQuery()
|
|
|
|
+ .eq(Admin::getUsername, username)
|
|
|
|
+ .one();
|
|
|
|
+ if (BeanUtil.isEmpty(one)){
|
|
|
|
+ throw new BadReqException("该用户不存在,请检查用户名");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
+ //密码加密
|
|
|
|
+ //如果密码密码为空则 不修改密码 使用原密码
|
|
|
|
+ if (StringUtils.isEmpty(password)){
|
|
|
|
+ password = one.getPassword();
|
|
|
|
+ }else {
|
|
|
|
+ password = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
|
|
+ }
|
|
|
|
+ Admin admin = BeanUtil.copyProperties(dto, Admin.class);
|
|
|
|
+ admin.setPassword(password);
|
|
|
|
+ admin.setOperatorId(adminId);
|
|
|
|
+ admin.setUpdateTime(now);
|
|
|
|
+ //更新到数据库
|
|
|
|
+ this.updateById(admin);
|
|
|
|
+
|
|
|
|
+ //封装vo
|
|
|
|
+ AdminLoginVO adminLoginVO = BeanUtil.copyProperties(admin, AdminLoginVO.class);
|
|
|
|
+ //从redis中获取token
|
|
|
|
+ String tokenKey = RedisConstant.ADMIN_TOKEN_PREFIX +adminId;
|
|
|
|
+ String redisToken = (String) redisTemplate.opsForValue().get(tokenKey);
|
|
|
|
+ adminLoginVO.setLoginToken(redisToken);
|
|
|
|
+
|
|
|
|
+ return Result.ok(adminLoginVO);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result updateState(Integer id) {
|
|
|
|
+ //根据id查询管理员
|
|
|
|
+ Admin admin = this.getById(id);
|
|
|
|
+ //判断该管理员是否存在
|
|
|
|
+ if (BeanUtil.isEmpty(admin)){
|
|
|
|
+ throw new BadReqException("该管理员不存在");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ admin.setEnabled(!admin.getEnabled());
|
|
|
|
+ this.updateById(admin);
|
|
|
|
+ return Result.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result logout(Integer id) {
|
|
|
|
+ //把redis中的token删除
|
|
|
|
+ //从redis中获取token
|
|
|
|
+ String tokenKey = RedisConstant.ADMIN_TOKEN_PREFIX +id;
|
|
|
|
+ if (!redisTemplate.hasKey(tokenKey)) {
|
|
|
|
+ throw new BadReqException("该管理员的token不存在");
|
|
|
|
+ }
|
|
|
|
+ redisTemplate.delete(tokenKey);
|
|
|
|
+
|
|
|
|
+ return Result.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result<AdminQueryPageVO> getPageList(AdminQueryPageDTO dto) {
|
|
|
|
+ //获取dto中的数据
|
|
|
|
+ Integer pageIndex = dto.getPageIndex();
|
|
|
|
+ Integer pageSize = dto.getPageSize();
|
|
|
|
+ //分页查询
|
|
|
|
+ Page<Admin> page = this.lambdaQuery()
|
|
|
|
+ .like(dto.getSearch() != null, Admin::getUsername, dto.getSearch())
|
|
|
|
+ .orderByDesc(Admin::getCreateTime)
|
|
|
|
+ .page(new Page<>(pageIndex, pageSize));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ AdminQueryPageVO adminQueryPageVO = new AdminQueryPageVO();
|
|
|
|
+ adminQueryPageVO.setCurrent((int) page.getCurrent());
|
|
|
|
+ adminQueryPageVO.setSize(pageSize);
|
|
|
|
+ adminQueryPageVO.setPages((int) page.getPages());
|
|
|
|
+ if (dto.getSearch() != null){
|
|
|
|
+ adminQueryPageVO.setSearchCount(true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<Admin> records = page.getRecords();
|
|
|
|
+ List<AdminLoginVO> list = new ArrayList<>();
|
|
|
|
+ for (Admin record : records) {
|
|
|
|
+ AdminLoginVO adminLoginVO = BeanUtil.copyProperties(record, AdminLoginVO.class);
|
|
|
|
+
|
|
|
|
+ list.add(adminLoginVO);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ adminQueryPageVO.setRecords(list);
|
|
|
|
+ adminQueryPageVO.setTotal((int) page.getTotal());
|
|
|
|
+
|
|
|
|
+ return Result.ok(adminQueryPageVO);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|