wip: 可能基本完成的开放模块

This commit is contained in:
小蟑螂
2026-05-26 23:52:17 +08:00
parent 2ef5b9a41a
commit 05babc0daa
41 changed files with 2502 additions and 4 deletions
@@ -5,6 +5,9 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import top.xyquan.ecomdmbbackend.modules.open.api.context.OpenApiContextHolder;
import top.xyquan.ecomdmbbackend.modules.open.api.security.OpenApiAuthentication;
import top.xyquan.ecomdmbbackend.modules.open.api.security.OpenApiPrincipal;
import top.xyquan.ecomdmbbackend.modules.org.company.member.model.po.CompanyMemberPO;
import top.xyquan.ecomdmbbackend.modules.org.company.member.service.CompanyMemberService;
@@ -30,11 +33,30 @@ public class CurrentUserContext {
* @throws BusinessException 如果无法获取用户信息
*/
public TokenPayload getCurrentUserPayload() {
OpenApiPrincipal openPrincipal = OpenApiContextHolder.get();
if (openPrincipal != null) {
return TokenPayload.builder()
.userId(String.valueOf(openPrincipal.operatorUserId()))
.companyId(openPrincipal.companyId())
.role("user")
.companyRole("owner")
.build();
}
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new BusinessException("用户未登录 - 认证信息为空");
}
if (authentication instanceof OpenApiAuthentication openApiAuthentication) {
return TokenPayload.builder()
.userId(String.valueOf(openApiAuthentication.getOperatorUserId()))
.companyId(openApiAuthentication.getCompanyId())
.role("user")
.companyRole("owner")
.build();
}
if (authentication.getCredentials() == null) {
throw new BusinessException("用户未登录 - Token信息为空");
}
@@ -51,6 +73,10 @@ public class CurrentUserContext {
* @throws BusinessException 如果无法获取公司信息
*/
public Long getCurrentCompanyId() {
OpenApiPrincipal openPrincipal = OpenApiContextHolder.get();
if (openPrincipal != null) {
return openPrincipal.companyId();
}
TokenPayload payload = getCurrentUserPayload();
if (payload.getCompanyId() == null) {
throw new BusinessException("无法获取公司信息");
@@ -65,6 +91,10 @@ public class CurrentUserContext {
* @throws BusinessException 如果无法获取用户信息
*/
public Long getCurrentUserId() {
OpenApiPrincipal openPrincipal = OpenApiContextHolder.get();
if (openPrincipal != null) {
return openPrincipal.operatorUserId();
}
TokenPayload payload = getCurrentUserPayload();
try {
return Long.parseLong(payload.getUserId());
@@ -73,6 +103,13 @@ public class CurrentUserContext {
}
}
/**
* 是否为开放 API 请求(API Key 认证)
*/
public boolean isOpenApiRequest() {
return OpenApiContextHolder.isOpenApiRequest();
}
/**
* 获取当前用户角色
*