初始代码
This commit is contained in:
@@ -43,14 +43,18 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
*/
|
||||
@Bean
|
||||
public Docket docket() {
|
||||
log.info("准备生成接口文档...");
|
||||
ApiInfo apiInfo = new ApiInfoBuilder()
|
||||
.title("苍穹外卖项目接口文档")
|
||||
.version("2.0")
|
||||
.description("苍穹外卖项目接口文档")
|
||||
.build();
|
||||
//指定版本
|
||||
Docket docket = new Docket(DocumentationType.SWAGGER_2)
|
||||
//上文设置的信息
|
||||
.apiInfo(apiInfo)
|
||||
.select()
|
||||
//指定生成接口需要扫描的包
|
||||
.apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
@@ -62,6 +66,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
* @param registry
|
||||
*/
|
||||
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
log.info("开始设置静态资源映射...");
|
||||
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -8,6 +9,8 @@ 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.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -24,6 +27,7 @@ import java.util.Map;
|
||||
@RestController
|
||||
@RequestMapping("/admin/employee")
|
||||
@Slf4j
|
||||
@Api(tags = "员工相关接口")
|
||||
public class EmployeeController {
|
||||
|
||||
@Autowired
|
||||
@@ -38,6 +42,7 @@ public class EmployeeController {
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
@ApiOperation(value = "员工登录")
|
||||
public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
|
||||
log.info("员工登录:{}", employeeLoginDTO);
|
||||
|
||||
@@ -67,8 +72,17 @@ public class EmployeeController {
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/logout")
|
||||
@ApiOperation("员工退出")
|
||||
public Result<String> logout() {
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ApiOperation("新增员工")
|
||||
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,17 @@ public class GlobalExceptionHandler {
|
||||
return Result.error(ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){
|
||||
//Duplicate entry 'lisi' for key 'employee.idx_username'
|
||||
String message = ex.getMessage();
|
||||
if (message.contains("Duplicate entry")){
|
||||
String username = message.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;
|
||||
@@ -46,7 +47,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);
|
||||
BaseContext.setCurrentId(empId);
|
||||
log.info("当前员工id:{}", 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,11 @@ 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, status, create_time, update_time, create_user, update_user) " +
|
||||
"values (#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
|
||||
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,7 +1,10 @@
|
||||
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;
|
||||
@@ -9,10 +12,13 @@ import com.sky.exception.AccountNotFoundException;
|
||||
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 +45,8 @@ 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 +61,29 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
return employee;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增员工
|
||||
* @param employeeDTO
|
||||
*/
|
||||
@Override
|
||||
public void save(EmployeeDTO employeeDTO) {
|
||||
Employee employee = new Employee();
|
||||
|
||||
//拷贝属性
|
||||
BeanUtils.copyProperties(employeeDTO, employee);
|
||||
|
||||
//设置属性
|
||||
employee.setStatus(StatusConstant.ENABLE);
|
||||
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
|
||||
employee.setCreateTime(LocalDateTime.now());
|
||||
employee.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
// 设置为当前登录用户的ID
|
||||
employee.setCreateUser(BaseContext.getCurrentId());
|
||||
employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
BaseContext.removeCurrentId();
|
||||
|
||||
employeeMapper.insert(employee);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user