当前访客信息
This commit is contained in:
1 parent
725a2e7de6
commit
2012b4cc8e
1 file changed
+71
-9
+71
-9
@@ -53,6 +53,14 @@ import org.springframework.security.oauth2.jwt.JwtClaimsSet;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
@@ -83,6 +91,58 @@ public class VisitorsServiceImpl extends ServiceImpl<VisitorsMapper, VisitorsEnt
|
||||
|
||||
private final RemoterVisitorService remoterVisitorService;
|
||||
|
||||
@Value("${visitor.jwt-secret}")
|
||||
private String visitorJwtSecret;
|
||||
|
||||
/**
|
||||
* 从当前请求的token中获取访客用户ID
|
||||
*/
|
||||
private Long getCurrentVisitorUserId() {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if (attributes == null) {
|
||||
throw new ServiceException("无法获取当前请求信息");
|
||||
}
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
String token = request.getHeader("token");
|
||||
if (token == null || token.isEmpty()) {
|
||||
throw new ServiceException("token不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
DecodedJWT jwt = JWT.require(Algorithm.HMAC256(visitorJwtSecret)).build().verify(token);
|
||||
Long userId = jwt.getClaim("id").asLong();
|
||||
if (userId == null) {
|
||||
throw new ServiceException("token中用户ID无效");
|
||||
}
|
||||
return userId;
|
||||
} catch (JWTVerificationException e) {
|
||||
throw new ServiceException("token验证失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从当前请求的token中获取访客用户名
|
||||
*/
|
||||
private String getCurrentVisitorUsername() {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if (attributes == null) {
|
||||
throw new ServiceException("无法获取当前请求信息");
|
||||
}
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
String token = request.getHeader("token");
|
||||
if (token == null || token.isEmpty()) {
|
||||
throw new ServiceException("token不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
DecodedJWT jwt = JWT.require(Algorithm.HMAC256(visitorJwtSecret)).build().verify(token);
|
||||
String realName = jwt.getClaim("realName").asString();
|
||||
return realName != null ? realName : "访客用户";
|
||||
} catch (JWTVerificationException e) {
|
||||
throw new ServiceException("token验证失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Object vistorsSave(VisitorsAppDTO visitors) {
|
||||
@@ -108,7 +168,7 @@ public class VisitorsServiceImpl extends ServiceImpl<VisitorsMapper, VisitorsEnt
|
||||
visitorInfo.setPhoneNo(visitors.getMobile());
|
||||
visitorInfoList.add(visitorInfo);
|
||||
|
||||
Long userId = SecurityUtils.getUser().getId();
|
||||
Long userId = getCurrentVisitorUserId();
|
||||
|
||||
VisitorsEntity bean = BeanUtils.toBean(visitors, VisitorsEntity.class);
|
||||
bean.setType(VisitorTypeEnum.FKSQ.getType());
|
||||
@@ -118,7 +178,9 @@ public class VisitorsServiceImpl extends ServiceImpl<VisitorsMapper, VisitorsEnt
|
||||
String uniqueToken = UUID.randomUUID().toString();
|
||||
String token = uniqueToken.replace("-", "");
|
||||
bean.setToken(token);
|
||||
// 设置数据归属信息
|
||||
bean.setUserId(userId);
|
||||
bean.setCreateBy(getCurrentVisitorUsername());
|
||||
|
||||
//如果yyIda不为空,就是被邀约了,需要把那条员工的邀约信息改为审核通过
|
||||
if (visitors.getYyId() != null){
|
||||
@@ -231,15 +293,15 @@ public class VisitorsServiceImpl extends ServiceImpl<VisitorsMapper, VisitorsEnt
|
||||
// throw new ServiceException("流程实例启动失败: " + stringR.getMsg());
|
||||
// }
|
||||
// String processInstanceId = stringR.getData();
|
||||
PjcloudUser user = SecurityUtils.getUser();
|
||||
Long deptId = user.getDeptId();
|
||||
Long userId = getCurrentVisitorUserId();
|
||||
String username = getCurrentVisitorUsername();
|
||||
|
||||
VisitorsEntity bean = BeanUtils.toBean(visitors, VisitorsEntity.class);
|
||||
bean.setType(VisitorTypeEnum.ZDYQ.getType());
|
||||
// bean.setProcessInstanceId(processInstanceId);
|
||||
bean.setUserId(user.getId());
|
||||
bean.setDeptId(deptId);
|
||||
R<UserInfo> byUserId = remoteUserService.getByUserId(user.getId());
|
||||
bean.setUserName(byUserId.getData().getSysUser().getUsername());
|
||||
// 设置数据归属信息
|
||||
bean.setUserId(userId);
|
||||
bean.setCreateBy(username);
|
||||
bean.setCheckStatus(TaskCheckStatusEnum.PADDING.getCode());
|
||||
|
||||
String uniqueToken = UUID.randomUUID().toString();
|
||||
@@ -308,9 +370,9 @@ public class VisitorsServiceImpl extends ServiceImpl<VisitorsMapper, VisitorsEnt
|
||||
|
||||
@Override
|
||||
public Object getPageSQ(Page page, VisitorsEntity visitors) {
|
||||
Long userId = SecurityUtils.getUser().getId();
|
||||
// Long userId = SecurityUtils.getUser().getId();
|
||||
LambdaQueryWrapper<VisitorsEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(VisitorsEntity::getUserId, userId);
|
||||
// wrapper.eq(VisitorsEntity::getUserId, userId);
|
||||
wrapper.eq(VisitorsEntity::getType, VisitorTypeEnum.FKSQ.getType());
|
||||
wrapper.eq(StrUtils.isNotBlank(visitors.getCheckStatus()),VisitorsEntity::getCheckStatus, visitors.getCheckStatus());
|
||||
wrapper.orderByDesc(VisitorsEntity::getCreateTime);
|
||||
|
||||
Reference in new issue
Block a user