苍穹外卖--新增员工功能完善二-通过ThreadLocal动态设置登录者ID

This commit is contained in:
2025-11-04 16:49:08 +08:00
parent 3cb7b4971a
commit 19c759c0ac
3 changed files with 16 additions and 2 deletions

View File

@@ -86,6 +86,7 @@ public class EmployeeController {
@PostMapping @PostMapping
public Result save(@RequestBody EmployeeDTO employeeDTO) { public Result save(@RequestBody EmployeeDTO employeeDTO) {
log.info("新增员工:{}",employeeDTO); log.info("新增员工:{}",employeeDTO);
log.info("新增员工Controller所处的线程id{}",Thread.currentThread().getId());
//调用业务层新增员工 //调用业务层新增员工
employeeService.save(employeeDTO); employeeService.save(employeeDTO);
return Result.success(); return Result.success();

View File

@@ -1,6 +1,7 @@
package com.sky.interceptor; package com.sky.interceptor;
import com.sky.constant.JwtClaimsConstant; import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.properties.JwtProperties; import com.sky.properties.JwtProperties;
import com.sky.utils.JwtUtil; import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
@@ -47,6 +48,12 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token); Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString()); Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
log.info("当前员工id", empId); log.info("当前员工id", empId);
log.info("登录校验拦截器所处的线程id{}",Thread.currentThread().getId());
//往ThreadLocal中保存当前登录的员工ID
BaseContext.setCurrentId(empId);
//3、通过放行 //3、通过放行
return true; return true;
} catch (Exception ex) { } catch (Exception ex) {

View File

@@ -3,6 +3,7 @@ package com.sky.service.impl;
import com.sky.constant.MessageConstant; import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant; import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant; import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO; import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO; import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee; import com.sky.entity.Employee;
@@ -11,6 +12,7 @@ import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException; import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper; import com.sky.mapper.EmployeeMapper;
import com.sky.service.EmployeeService; import com.sky.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -19,6 +21,7 @@ import org.springframework.util.DigestUtils;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Service @Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService { public class EmployeeServiceImpl implements EmployeeService {
@Autowired @Autowired
@@ -65,6 +68,7 @@ public class EmployeeServiceImpl implements EmployeeService {
*/ */
@Override @Override
public void save(EmployeeDTO employeeDTO) { public void save(EmployeeDTO employeeDTO) {
log.info("新增员工Service层所处的线程id{}",Thread.currentThread().getId());
//1.对象属性拷贝EmployeeDTO--->Employee //1.对象属性拷贝EmployeeDTO--->Employee
Employee employee = new Employee(); Employee employee = new Employee();
BeanUtils.copyProperties(employeeDTO,employee); BeanUtils.copyProperties(employeeDTO,employee);
@@ -75,8 +79,10 @@ public class EmployeeServiceImpl implements EmployeeService {
employee.setUpdateTime(LocalDateTime.now()); employee.setUpdateTime(LocalDateTime.now());
//4.设置当前记录创建人和修改人的id //4.设置当前记录创建人和修改人的id
//todo 后期要修改为当前登录用户的id //todo 后期要修改为当前登录用户的id
employee.setCreateUser(1L); //取出拦截器中保存的登录员工ID作为创建者和更新者ID数据
employee.setUpdateUser(1L); Long empId = BaseContext.getCurrentId();
employee.setCreateUser(empId);
employee.setUpdateUser(empId);
//添加默认密码 //添加默认密码
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes())); employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));