feat: 多租户实时切换 — 前端拦截器统一entity_id + 后端get_entity_id依赖 + dashboard 6端点支持租户过滤
- 前端: api/index.ts拦截器自动附加X-Entity-Id header + entity_id query参数 - 后端: 新增app/deps.py的get_entity_id公共依赖(query→header→默认1) - dashboard.py: summary/kpis/finance-analysis/predict/my-kpis/my-dashboard全部支持entity_id - 解决: 42个页面仅13个传entity_id导致切换企业后数据混乱
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
"""多租户公共依赖 — 从请求头/参数读取当前企业ID"""
|
||||
from fastapi import Request, Header, Query, Depends
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def get_entity_id(
|
||||
request: Request,
|
||||
x_entity_id: Optional[str] = Header(None, alias="X-Entity-Id"),
|
||||
entity_id: Optional[int] = Query(None, ge=1),
|
||||
) -> int:
|
||||
"""解析当前企业ID:优先query参数 → header → 默认1(酣客)
|
||||
前端拦截器已统一附加 X-Entity-Id header 和 entity_id query参数
|
||||
"""
|
||||
if entity_id is not None:
|
||||
return entity_id
|
||||
if x_entity_id and x_entity_id.isdigit():
|
||||
return int(x_entity_id)
|
||||
# 兼容 body 中的 entity_id(POST场景)
|
||||
if request.method in ("POST", "PUT", "PATCH"):
|
||||
try:
|
||||
body = request.state.body_json or {}
|
||||
if body.get("entity_id"):
|
||||
return int(body["entity_id"])
|
||||
except Exception:
|
||||
pass
|
||||
return 1 # 默认酣客
|
||||
Reference in New Issue
Block a user