新增员工功能开发

master
hahameng 8 months ago
parent fcf802bff6
commit b3c6645d64

@ -7,6 +7,7 @@ public class MessageConstant {
public static final String PASSWORD_ERROR = "密码错误";
public static final String ACCOUNT_NOT_FOUND = "账号不存在";
public static final String ALREADY_EXISTS = "已存在";
public static final String ACCOUNT_LOCKED = "账号被锁定";
public static final String UNKNOWN_ERROR = "未知错误";
public static final String USER_NOT_LOGIN = "用户未登录";

@ -1,6 +1,7 @@
package com.sky.controller.admin;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
@ -31,6 +32,7 @@ public class EmployeeController {
@Autowired
private JwtProperties jwtProperties;
/**
*
*
@ -47,8 +49,8 @@ public class EmployeeController {
Map<String, Object> claims = new HashMap<>();
claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
String token = JwtUtil.createJWT(
jwtProperties.getAdminSecretKey(),
jwtProperties.getAdminTtl(),
jwtProperties.getAdminSecretKey(), //设置jwt签名加密时使用的秘钥
jwtProperties.getAdminTtl(), //设置jwt过期时间
claims);
EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
@ -71,4 +73,18 @@ public class EmployeeController {
return Result.success();
}
/**
*
*
* @param employeeDTO
* @return
*/
@PostMapping
public Result save(@RequestBody EmployeeDTO employeeDTO) {
log.info("新增员工,员工数据:{}", employeeDTO);
employeeService.save(employeeDTO);
return Result.success();
}
}

@ -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,22 @@ public class GlobalExceptionHandler {
return Result.error(ex.getMessage());
}
/**
* SQL
* @param sqlIntegrityConstraintViolationException
* @return
*/
@ExceptionHandler
public Result exceptionHander(SQLIntegrityConstraintViolationException sqlIntegrityConstraintViolationException){
log.error("异常信息:{}", sqlIntegrityConstraintViolationException.getMessage());
String message = sqlIntegrityConstraintViolationException.getMessage();
if (message.contains("Duplicate entry")){
String[] arr = message.split(" ");
String username = arr[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;
@ -47,6 +48,13 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
log.info("当前员工id", empId);
/////将用户id存储到ThreadLocal////////
BaseContext.setCurrentId(empId);
////////////////////////////////////
//3、通过放行
return true;
} catch (Exception ex) {

@ -1,6 +1,7 @@
package com.sky.mapper;
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 +16,12 @@ public interface EmployeeMapper {
@Select("select * from employee where username = #{username}")
Employee getByUsername(String username);
/**
*
* @param employee
*/
@Insert("insert into employee (name, username, password, phone, sex, id_number, create_time, update_time, create_user, update_user,status) " +
"values " +
"(#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})")
void insert(Employee employee);
}

@ -1,5 +1,6 @@
package com.sky.service;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
@ -12,4 +13,9 @@ public interface EmployeeService {
*/
Employee login(EmployeeLoginDTO employeeLoginDTO);
/**
*
* @param employeeDTO
*/
void save(EmployeeDTO employeeDTO);
}

@ -1,18 +1,25 @@
package com.sky.service.impl;
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.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.BaseException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
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 java.time.LocalDateTime;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@ -39,7 +46,9 @@ public class EmployeeServiceImpl implements EmployeeService {
}
//密码比对
// TODO 后期需要进行md5加密然后再进行比对
//进行md5加密
password = DigestUtils.md5DigestAsHex(password.getBytes());
if (!password.equals(employee.getPassword())) {
//密码错误
throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
@ -54,4 +63,31 @@ public class EmployeeServiceImpl implements EmployeeService {
return employee;
}
/**
*
*
* @param employeeDTO
*/
public void save(EmployeeDTO employeeDTO) {
Employee employee = new Employee();
//对象属性拷贝
BeanUtils.copyProperties(employeeDTO, employee);
//设置账号的状态,默认正常状态 1表示正常 0表示锁定
employee.setStatus(StatusConstant.ENABLE);
//设置密码默认密码123456
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
//设置当前记录的创建时间和修改时间
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
//设置当前记录创建人id和修改人id
employee.setCreateUser(BaseContext.getCurrentId());
employee.setUpdateUser(BaseContext.getCurrentId());
employeeMapper.insert(employee);//后续步骤定义
}
}

@ -5,4 +5,4 @@ sky:
port: 3306
database: sky_take_out
username: root
password: root
password: 1234

@ -21,6 +21,7 @@ mybatis:
#开启驼峰命名
map-underscore-to-camel-case: true
logging:
level:
com:

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d 表示日期,%thread 表示线程名,%-5level表示级别从左显示5个字符宽度%msg表示日志消息%n表示换行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}-%msg%n</pattern>
</encoder>
</appender>
<!--系统文件输出 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 日志文件输出的文件名, %i表示序号 -->
<FileNamePattern>D:/tlias-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
<!-- 最多保留的历史日志文件数量 -->
<MaxHistory>30</MaxHistory>
<!-- 最大文件大小,超过这个大小会触发滚动到新文件,默认为 10MB -->
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}-%msg%n</pattern>
</encoder>
</appender>
<!--日志输出级别-->
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Loading…
Cancel
Save