feat(quota): implement unified quota management system and add community free plan

- Add `default_free_plan.py` to define the configuration for the Community Free Plan.
- Refactor `quota_stub.py` as a unified entry point, delegating checks to `core/quota_manager`.
- Implement core logic in `quota_manager.py` to support retrieving quotas from the premium module or configuration files.
- Update `tenant_subscription_controller` to return Community Free Plan information.
This commit is contained in:
wwq
2026-04-15 18:48:09 +08:00
parent 18be1a9f89
commit 1faa258e23
4 changed files with 567 additions and 43 deletions

View File

@@ -1,6 +1,7 @@
"""
租户套餐查询接口(普通用户可访问)
"""
import datetime
from typing import Callable
from fastapi import APIRouter, Depends
@@ -46,8 +47,36 @@ async def get_my_tenant_subscription(
return success(data=svc.build_response(sub))
except ModuleNotFoundError:
# 社区版无 premium 模块,返回空
return success(data=None, msg="套餐功能未启用")
# 社区版无 premium 模块,从配置文件读取免费套餐
if not current_user.tenant:
return JSONResponse(status_code=404, content=fail(code=404, msg="用户未关联租户"))
from app.config.default_free_plan import DEFAULT_FREE_PLAN
plan = DEFAULT_FREE_PLAN
response_data = {
"subscription_id": None,
"tenant_id": str(current_user.tenant.id),
"package_plan_id": None,
"package_version": plan["version"],
"package_plan": {
"id": None,
"name": plan["name"],
"version": plan["version"],
"category": plan["category"],
"tier_level": plan["tier_level"],
"price": float(plan["price"]),
"billing_cycle": plan["billing_cycle"],
},
"started_at": None,
"expired_at": None,
"status": "active",
"quota": plan["quotas"],
"created_at": int(datetime.datetime.utcnow().timestamp() * 1000),
"updated_at": int(datetime.datetime.utcnow().timestamp() * 1000),
}
return success(data=response_data, msg="社区版免费套餐")
except Exception as e:
logger.error(f"获取租户套餐信息失败: {e}", exc_info=True)
return JSONResponse(status_code=500, content=fail(code=500, msg="获取套餐信息失败"))