wendajia/backend/app/config.py

86 lines
5.2 KiB
Python

import os
from pathlib import Path
from dotenv import load_dotenv
PROJECT_DIR = Path(__file__).resolve().parents[2]
BASE_DIR = Path(__file__).resolve().parents[1]
load_dotenv(BASE_DIR / ".env")
def _resolve_backend_path(value: str) -> str:
if not value:
return ""
path = Path(value.strip())
return str(path if path.is_absolute() else BASE_DIR / path)
class Settings:
app_name = "问大家运营管理后台"
database_url = os.getenv("DATABASE_URL", f"sqlite:///{BASE_DIR / 'data' / 'app.db'}")
_screenshot_storage_value = Path(os.getenv("SCREENSHOT_STORAGE_DIR", "data/uploads/screenshots"))
screenshot_storage_dir = (
_screenshot_storage_value
if _screenshot_storage_value.is_absolute()
else BASE_DIR / _screenshot_storage_value
)
secret_key = os.getenv("SECRET_KEY", "change-this-secret-key-in-production")
cookie_name = "admin_session"
session_hours = int(os.getenv("SESSION_HOURS", "12"))
cookie_secure = os.getenv("COOKIE_SECURE", "false").lower() == "true"
admin_username = os.getenv("ADMIN_USERNAME", "admin")
admin_password = os.getenv("ADMIN_PASSWORD", "admin123456")
operator_username = os.getenv("OPERATOR_USERNAME", "operator")
operator_password = os.getenv("OPERATOR_PASSWORD", "operator123456")
llm_api_key = os.getenv("LLM_API_KEY", "")
llm_base_url = os.getenv("LLM_BASE_URL", "https://api.openai.com/v1")
llm_model = os.getenv("LLM_MODEL", "gpt-4o-mini")
# OCR/识图可单独使用视觉模型,未配置时沿用普通问答模型。
llm_vision_model = os.getenv("LLM_VISION_MODEL", llm_model)
llm_timeout_seconds = int(os.getenv("LLM_TIMEOUT_SECONDS", "90"))
# 开发环境返回验证码,接入短信服务后请关闭并在发送函数中对接供应商。
sms_dev_mode = os.getenv("SMS_DEV_MODE", "true").lower() == "true"
# DingTalk workbench SSO/OAuth settings.
dingtalk_corp_id = os.getenv("DINGTALK_CORP_ID", "")
dingtalk_agent_id = os.getenv("DINGTALK_AGENT_ID", "")
dingtalk_client_id = os.getenv("DINGTALK_CLIENT_ID", "")
dingtalk_client_secret = os.getenv("DINGTALK_CLIENT_SECRET", "")
dingtalk_redirect_uri = os.getenv("DINGTALK_REDIRECT_URI", "http://localhost:8000/api/admin/auth/dingtalk/callback")
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:5173")
dingtalk_allowed_union_ids = {item.strip() for item in os.getenv("DINGTALK_ALLOWED_UNION_IDS", "").split(",") if item.strip()}
# New DingTalk users may enter only when they belong to one of these
# business divisions (including child departments). Existing active
# admin_users are grandfathered and continue to work.
dingtalk_allowed_dept_ids = {item.strip() for item in os.getenv("DINGTALK_ALLOWED_DEPT_IDS", "").split(",") if item.strip()}
dingtalk_allowed_dept_names = {item.strip() for item in os.getenv("DINGTALK_ALLOWED_DEPT_NAMES", "战狼事业部,雪狼事业部").split(",") if item.strip()}
dingtalk_super_admin_union_ids = {item.strip() for item in os.getenv("DINGTALK_SUPER_ADMIN_UNION_IDS", "").split(",") if item.strip()}
# Members of these departments receive the same access as super admins.
# IDs are preferred; names are supported as a readable fallback.
dingtalk_super_admin_dept_ids = {item.strip() for item in os.getenv("DINGTALK_SUPER_ADMIN_DEPT_IDS", "").split(",") if item.strip()}
dingtalk_super_admin_dept_names = {item.strip() for item in os.getenv("DINGTALK_SUPER_ADMIN_DEPT_NAMES", "AI指挥部").split(",") if item.strip()}
# Alipay transfer settings. Keep live transfer disabled until credentials and
# the application's transfer permission have been verified in the sandbox.
alipay_enabled = os.getenv("ALIPAY_ENABLED", "false").lower() == "true"
alipay_app_id = os.getenv("ALIPAY_APP_ID", "")
alipay_payer_account = os.getenv("ALIPAY_PAYER_ACCOUNT", "")
alipay_app_private_key_path = _resolve_backend_path(os.getenv("ALIPAY_APP_PRIVATE_KEY_PATH", ""))
alipay_public_key_path = _resolve_backend_path(os.getenv("ALIPAY_PUBLIC_KEY_PATH", ""))
# Certificate-mode signing files from Alipay Open Platform.
alipay_app_cert_path = _resolve_backend_path(os.getenv("ALIPAY_APP_CERT_PATH", ""))
alipay_alipay_public_cert_path = _resolve_backend_path(os.getenv("ALIPAY_ALIPAY_PUBLIC_CERT_PATH", ""))
alipay_root_cert_path = _resolve_backend_path(os.getenv("ALIPAY_ROOT_CERT_PATH", ""))
alipay_gateway = os.getenv("ALIPAY_GATEWAY", "https://openapi.alipay.com/gateway.do")
alipay_sign_type = os.getenv("ALIPAY_SIGN_TYPE", "RSA2")
alipay_charset = os.getenv("ALIPAY_CHARSET", "utf-8")
alipay_format = os.getenv("ALIPAY_FORMAT", "json")
alipay_notify_url = os.getenv("ALIPAY_NOTIFY_URL", "")
alipay_transfer_api = os.getenv("ALIPAY_TRANSFER_API", "alipay.fund.trans.uni.transfer")
alipay_transfer_query_api = os.getenv("ALIPAY_TRANSFER_QUERY_API", "alipay.fund.trans.common.query")
alipay_transfer_scene_name = os.getenv("ALIPAY_TRANSFER_SCENE_NAME", "").strip()
alipay_http_timeout_seconds = int(os.getenv("ALIPAY_HTTP_TIMEOUT_SECONDS", "30"))
alipay_reconcile_interval_seconds = max(10, int(os.getenv("ALIPAY_RECONCILE_INTERVAL_SECONDS", "30")))
settings = Settings()