|
|
@@ -21,9 +21,10 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* SaaS WebSocket 租户上下文工具:
|
|
|
@@ -139,6 +140,50 @@ public class TenantChannelContext {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * javax.websocket 握手阶段专用:当未传 tenantCode 时,根据 liveId 遍历所有租户库查找归属租户。
|
|
|
+ * 找到后将 tenantId/dsKey 存入 userProperties,兼容后台/销售端不带租户信息的场景。
|
|
|
+ */
|
|
|
+ public void resolveByLiveId(Long liveId, java.util.Map<String, Object> userProperties) {
|
|
|
+ if (!saasEnabled || liveId == null) return;
|
|
|
+ try {
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
+ TenantInfo query = new TenantInfo();
|
|
|
+ query.setStatus(1);
|
|
|
+ List<TenantInfo> tenants = tenantInfoService.selectTenantInfoList(query);
|
|
|
+ if (tenants == null || tenants.isEmpty()) return;
|
|
|
+
|
|
|
+ Date now = new Date();
|
|
|
+ for (TenantInfo tenant : tenants) {
|
|
|
+ if (tenant.getExpireTime() != null && tenant.getExpireTime().before(now)) continue;
|
|
|
+ try {
|
|
|
+ tenantDataSourceManager.switchTenant(tenant);
|
|
|
+ DataSource ds = tenantDataSourceManager.getTenantDataSource(tenant.getId());
|
|
|
+ try (Connection conn = ds.getConnection();
|
|
|
+ java.sql.PreparedStatement ps = conn.prepareStatement(
|
|
|
+ "SELECT 1 FROM live WHERE id = ? LIMIT 1")) {
|
|
|
+ ps.setLong(1, liveId);
|
|
|
+ try (ResultSet rs = ps.executeQuery()) {
|
|
|
+ if (rs.next()) {
|
|
|
+ String dsKey = "tenant:" + tenant.getId();
|
|
|
+ userProperties.put("_tenantId", tenant.getId());
|
|
|
+ userProperties.put("_dsKey", dsKey);
|
|
|
+ log.info("[SaaS WS] 根据liveId={}反查到租户 tenantId={}, tenantCode={}",
|
|
|
+ liveId, tenant.getId(), tenant.getTenantCode());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.debug("[SaaS WS] 查询租户 tenantCode={} 的live表失败: {}", tenant.getTenantCode(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.warn("[SaaS WS] 根据liveId={} 未找到归属租户", liveId);
|
|
|
+ } finally {
|
|
|
+ DynamicDataSourceContextHolder.clearDataSourceType();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* javax.websocket 业务处理前专用:从 userProperties 取 tenantId/dsKey 直接激活,无需查库。
|
|
|
* 在 onOpen/onMessage/onClose 开头调用。
|