Compare commits
7 Commits
71f2b5e30a
...
13bd3dc21e
Author | SHA1 | Date |
---|---|---|
|
13bd3dc21e | |
|
3a1faa5ac2 | |
|
4afb69f034 | |
|
cc6d1b8e5b | |
|
7b0bd0e175 | |
|
00b26569ff | |
|
42d05894fa |
|
@ -24,4 +24,5 @@ public class MessageConstant {
|
|||
public static final String ORDER_STATUS_ERROR = "订单状态错误";
|
||||
public static final String ORDER_NOT_FOUND = "订单不存在";
|
||||
|
||||
public static final String ALREADY_EXISTS = "已存在了";
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.sky.dto;
|
||||
|
||||
import com.sky.entity.Employee;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
@ -15,5 +16,4 @@ public class EmployeePageQueryDTO implements Serializable {
|
|||
|
||||
//每页显示记录数
|
||||
private int pageSize;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package com.sky.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
@ -32,7 +30,7 @@ public class Employee implements Serializable {
|
|||
|
||||
private Integer status;
|
||||
|
||||
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy年MM月dd日 HH:mm:ss")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
|
@ -41,5 +39,4 @@ public class Employee implements Serializable {
|
|||
private Long createUser;
|
||||
|
||||
private Long updateUser;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
package com.sky.config;
|
||||
|
||||
import ch.qos.logback.classic.pattern.MessageConverter;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
||||
import com.sky.json.JacksonObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
@ -15,6 +20,9 @@ import springfox.documentation.service.ApiInfo;
|
|||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 配置类,注册web层相关组件
|
||||
*/
|
||||
|
@ -22,13 +30,13 @@ import springfox.documentation.spring.web.plugins.Docket;
|
|||
@Slf4j
|
||||
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
|
||||
|
||||
/**
|
||||
* 注册自定义拦截器
|
||||
*
|
||||
* @param registry
|
||||
* @param registry InterceptorRegistry
|
||||
*/
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
log.info("开始注册自定义拦截器...");
|
||||
|
@ -39,7 +47,8 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
|||
|
||||
/**
|
||||
* 通过knife4j生成接口文档
|
||||
* @return
|
||||
*
|
||||
* @return Docket
|
||||
*/
|
||||
@Bean
|
||||
public Docket docket() {
|
||||
|
@ -59,10 +68,25 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
|||
|
||||
/**
|
||||
* 设置静态资源映射
|
||||
* @param registry
|
||||
*
|
||||
* @param registry ResourceHandlerRegistry
|
||||
*/
|
||||
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
}
|
||||
|
||||
/**
|
||||
* 扩展Spring MVC框架转换器
|
||||
*
|
||||
* @param converters List<HttpMessageConverter<?>>
|
||||
*/
|
||||
@Override
|
||||
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
super.extendMessageConverters(converters);
|
||||
log.info("扩展消息转换器...");
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||
converter.setObjectMapper(new JacksonObjectMapper());
|
||||
converters.add(0,converter);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,29 @@
|
|||
package com.sky.controller.admin;
|
||||
|
||||
import com.sky.constant.JwtClaimsConstant;
|
||||
import com.sky.constant.PasswordConstant;
|
||||
import com.sky.constant.StatusConstant;
|
||||
import com.sky.dto.EmployeeDTO;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.properties.JwtProperties;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.EmployeeService;
|
||||
import com.sky.utils.JwtUtil;
|
||||
import com.sky.vo.EmployeeLoginVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -24,11 +33,12 @@ import java.util.Map;
|
|||
@RestController
|
||||
@RequestMapping("/admin/employee")
|
||||
@Slf4j
|
||||
@Api(tags = "员工相关接口")
|
||||
public class EmployeeController {
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private EmployeeService employeeService;
|
||||
@Autowired
|
||||
@Resource
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
/**
|
||||
|
@ -37,6 +47,7 @@ public class EmployeeController {
|
|||
* @param employeeLoginDTO
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "员工登录接口")
|
||||
@PostMapping("/login")
|
||||
public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
|
||||
log.info("员工登录:{}", employeeLoginDTO);
|
||||
|
@ -66,9 +77,36 @@ public class EmployeeController {
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "员工退出登录")
|
||||
@PostMapping("/logout")
|
||||
public Result<String> logout() {
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求新增员工
|
||||
*
|
||||
* @param employeeDTO 请求新增员工参数
|
||||
* @return Result
|
||||
*/
|
||||
@Operation(summary = "新增员工")
|
||||
@PostMapping
|
||||
public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {
|
||||
log.info("新增员工:{}", employeeDTO);
|
||||
employeeService.insert(employeeDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 员工分页查询
|
||||
*
|
||||
* @return Result<PageResult>
|
||||
*/
|
||||
@Operation(summary = "员工分页查询")
|
||||
@GetMapping("/page")
|
||||
public Result<PageResult> pageResultResult(EmployeePageQueryDTO employeePageQueryDTO) {
|
||||
log.info("员工查找,参数为:{}", employeeService);
|
||||
PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package com.sky.handler;
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.exception.BaseException;
|
||||
import com.sky.result.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.sql.SQLIntegrityConstraintViolationException;
|
||||
|
||||
/**
|
||||
* 全局异常处理器,处理项目中抛出的业务异常
|
||||
*/
|
||||
|
@ -24,4 +27,21 @@ public class GlobalExceptionHandler {
|
|||
return Result.error(ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理SQL异常
|
||||
* @param exception SQLIntegrityConstraintViolationException
|
||||
* @return Result<String>
|
||||
*/
|
||||
@ExceptionHandler
|
||||
public Result<String> exceptionHandler(SQLIntegrityConstraintViolationException exception) {
|
||||
String message = exception.getMessage();
|
||||
if (message.contains("Duplicate entry")) {
|
||||
String[] split = message.split(" ");
|
||||
String username = split[2];
|
||||
String msg = username + MessageConstant.ALREADY_EXISTS;
|
||||
return Result.error(msg);
|
||||
} else {
|
||||
return Result.error(MessageConstant.UNKNOWN_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.sky.interceptor;
|
||||
|
||||
import com.sky.constant.JwtClaimsConstant;
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.properties.JwtProperties;
|
||||
import com.sky.utils.JwtUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
|
@ -9,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -19,17 +22,17 @@ import javax.servlet.http.HttpServletResponse;
|
|||
@Slf4j
|
||||
public class JwtTokenAdminInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
/**
|
||||
* 校验jwt
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param handler
|
||||
* @return
|
||||
* @throws Exception
|
||||
* @param request HttpServletRequest
|
||||
* @param response HttpServletResponse
|
||||
* @param handler Object
|
||||
* @return boolean
|
||||
* @throws Exception Exception
|
||||
*/
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
//判断当前拦截到的是Controller的方法还是其他资源
|
||||
|
@ -46,7 +49,8 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
|
|||
log.info("jwt校验:{}", token);
|
||||
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
|
||||
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
|
||||
log.info("当前员工id:", empId);
|
||||
log.info("当前员工id:{}", empId);
|
||||
BaseContext.setCurrentId(empId);
|
||||
//3、通过,放行
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
|
@ -15,4 +18,16 @@ public interface EmployeeMapper {
|
|||
@Select("select * from employee where username = #{username}")
|
||||
Employee getByUsername(String username);
|
||||
|
||||
/**
|
||||
* 插入员工数据
|
||||
* @param employee 员工
|
||||
*/
|
||||
void insert(Employee employee);
|
||||
|
||||
/**
|
||||
* 员工分页查询
|
||||
* @param employeePageQueryDTO EmployeePageQueryDTO
|
||||
* @return Page<Employee>
|
||||
*/
|
||||
Page<Employee> pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
|
||||
}
|
||||
|
|
|
@ -1,15 +1,30 @@
|
|||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.EmployeeDTO;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.result.PageResult;
|
||||
|
||||
public interface EmployeeService {
|
||||
|
||||
/**
|
||||
* 员工登录
|
||||
* @param employeeLoginDTO
|
||||
* @return
|
||||
* @param employeeLoginDTO 员工请求
|
||||
* @return Employee
|
||||
*/
|
||||
Employee login(EmployeeLoginDTO employeeLoginDTO);
|
||||
|
||||
/**
|
||||
* 新增员工
|
||||
* @param employeeDTO 请求新增员工参数
|
||||
*/
|
||||
void insert(EmployeeDTO employeeDTO);
|
||||
|
||||
/**
|
||||
* 员工分页查询
|
||||
* @param employeeLoginDTO EmployeeService
|
||||
* @return PageResult
|
||||
*/
|
||||
PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
|
||||
}
|
||||
|
|
|
@ -1,29 +1,43 @@
|
|||
package com.sky.service.impl;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.constant.PasswordConstant;
|
||||
import com.sky.constant.StatusConstant;
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.dto.EmployeeDTO;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.exception.AccountLockedException;
|
||||
import com.sky.exception.AccountNotFoundException;
|
||||
import com.sky.exception.PasswordErrorException;
|
||||
import com.sky.mapper.EmployeeMapper;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.EmployeeService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private EmployeeMapper employeeMapper;
|
||||
|
||||
/**
|
||||
* 员工登录
|
||||
*
|
||||
* @param employeeLoginDTO
|
||||
* @return
|
||||
* @param employeeLoginDTO 登录信息
|
||||
* @return Employee
|
||||
*/
|
||||
public Employee login(EmployeeLoginDTO employeeLoginDTO) {
|
||||
String username = employeeLoginDTO.getUsername();
|
||||
|
@ -39,14 +53,14 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||
}
|
||||
|
||||
// 密码比对
|
||||
// TODO 后期需要进行md5加密,然后再进行比对
|
||||
password = DigestUtils.md5DigestAsHex(password.getBytes());// Md5加密
|
||||
if (!password.equals(employee.getPassword())) {
|
||||
// 密码错误
|
||||
throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
|
||||
}
|
||||
|
||||
if (employee.getStatus() == StatusConstant.DISABLE) {
|
||||
// 账号被锁定
|
||||
if (employee.getStatus() == StatusConstant.DISABLE) {
|
||||
throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
|
||||
}
|
||||
|
||||
|
@ -54,4 +68,48 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||
return employee;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 插入员工数据
|
||||
*
|
||||
* @param employeeDTO EmployeeDTO
|
||||
*/
|
||||
@Override
|
||||
public void insert(EmployeeDTO employeeDTO) {
|
||||
Employee employee = new Employee();
|
||||
// 对象属性拷贝
|
||||
BeanUtils.copyProperties(employeeDTO, employee);
|
||||
|
||||
// 设置状态默认1正常,0表示锁定
|
||||
employee.setStatus(StatusConstant.ENABLE);
|
||||
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
|
||||
employee.setIdNumber(employeeDTO.getIdNumber());
|
||||
|
||||
// 设置当前时间
|
||||
employee.setCreateTime(LocalDateTime.now());
|
||||
employee.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
// 设置当前记录创建人id
|
||||
employee.setCreateUser(BaseContext.getCurrentId());
|
||||
employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
|
||||
employeeMapper.insert(employee);
|
||||
BaseContext.removeCurrentId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 员工分页查询
|
||||
*
|
||||
* @param employeePageQueryDTO EmployeeService
|
||||
* @return PageResult
|
||||
*/
|
||||
@Override
|
||||
public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
|
||||
PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
|
||||
Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
|
||||
long total = page.getTotal();
|
||||
List<Employee> result = page.getResult();
|
||||
|
||||
return new PageResult(total, result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
sky:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
host: localhost
|
||||
port: 3306
|
||||
host: 106.15.251.123
|
||||
port: 3305
|
||||
database: sky_take_out
|
||||
username: root
|
||||
password: root
|
||||
password: "02120212"
|
||||
|
|
|
@ -2,4 +2,23 @@
|
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.EmployeeMapper">
|
||||
|
||||
<!-- 新增员工 -->
|
||||
<insert id="insert">
|
||||
insert into employee (name, username, password, phone, sex, id, id_number, status, create_time, update_time,
|
||||
create_user, update_user)
|
||||
values (#{name}, #{username}, #{password}, #{phone}, #{sex}, #{id}, #{idNumber}, #{status}, #{createTime},
|
||||
#{updateTime}, #{createUser}, #{updateUser});
|
||||
</insert>
|
||||
|
||||
<!-- 员工分页查询 -->
|
||||
<select id="pageQuery" resultType="com.sky.entity.Employee">
|
||||
select * from employee
|
||||
<where>
|
||||
<if test="name!='' and name!=null">
|
||||
and name like concat('%',#{name},'%')
|
||||
</if>
|
||||
</where>
|
||||
order by create_time desc
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue