291 lines
11 KiB
Python
291 lines
11 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import yaml
|
|
|
|
|
|
class Config:
|
|
"""配置管理类"""
|
|
|
|
_instance = None
|
|
_config_data = None
|
|
|
|
def __new__(cls):
|
|
if cls._instance is None:
|
|
cls._instance = super().__new__(cls)
|
|
return cls._instance
|
|
|
|
def __init__(self):
|
|
if self._config_data is None:
|
|
self._load_config()
|
|
|
|
def _load_config(self) -> None:
|
|
"""加载配置文件"""
|
|
config_path = Path(__file__).parent / "config.yaml"
|
|
|
|
# 默认配置
|
|
default_config = {
|
|
"wechat": {
|
|
"appid": "",
|
|
"appsecret": ""
|
|
},
|
|
"chrome": {
|
|
"path": r"C:\Program Files\Google\Chrome\Application\chrome.exe"
|
|
},
|
|
"step1": {
|
|
"url": "https://feast.yidaima.cn/login",
|
|
"username": "wangpeng",
|
|
"password": "Feastcoding@123",
|
|
"project_name": "【A167】基于Springboot + vue3实现的宿舍报修系统",
|
|
"feast_button": "FeastCoding"
|
|
},
|
|
"step2": {
|
|
"url": "http://yidaima.cn:6005/"
|
|
},
|
|
"database": {
|
|
"host": "localhost",
|
|
"port": 3306,
|
|
"database": "test",
|
|
"user": "root",
|
|
"password": "123456"
|
|
},
|
|
"quark": {
|
|
"cookies_dir": os.path.join(os.getcwd(), "data", "quark_cookies"),
|
|
"root_path": "精品项目整理"
|
|
},
|
|
"baidu": {
|
|
"cookies_dir": os.path.join(os.getcwd(), "data", "baidu_cookies"),
|
|
"root_path": "精品项目整理"
|
|
},
|
|
"project_screenshot": {
|
|
"project_path": "",
|
|
"desktop_path": "C:\\Users\\南音\\Desktop",
|
|
"has_front": True,
|
|
"bat_folder": "C:\\Users\\南音\\Desktop\\yidaima\\bat",
|
|
"show_cmd_window": True,
|
|
"code_source_folder": "code",
|
|
"code_target_path": "D:\\code",
|
|
"scripts": {
|
|
"install_server": "run_install_server.bat",
|
|
"install_front": "run_install_front.bat",
|
|
"install_admin": "run_install_admin.bat",
|
|
"run_server": "run_server.bat",
|
|
"run_front": "run_front.bat",
|
|
"run_admin": "run_admin.bat",
|
|
"run_sql": "run_sql.bat"
|
|
},
|
|
"sql": {
|
|
"script_path": "server/db/init.sql"
|
|
},
|
|
"services": {
|
|
"backend": {
|
|
"url": "http://localhost:8080",
|
|
"health_endpoint": "/actuator/health",
|
|
"startup_timeout": 120
|
|
},
|
|
"front": {
|
|
"url": "http://localhost:8082",
|
|
"login_url": "http://localhost:8082/#/login",
|
|
"startup_timeout": 60
|
|
},
|
|
"admin": {
|
|
"url": "http://localhost:8081",
|
|
"login_url": "http://localhost:8081/#/login",
|
|
"startup_timeout": 60
|
|
}
|
|
},
|
|
"login": {
|
|
"admin": {
|
|
"username": "admin",
|
|
"password": "admin"
|
|
},
|
|
"front": {
|
|
"username": "2",
|
|
"password": "123456"
|
|
}
|
|
},
|
|
"screenshot": {
|
|
"output_dir": "./screenshots",
|
|
"full_page": True,
|
|
"menu_selectors": {
|
|
"admin": ".el-menu-item, .el-sub-menu__title",
|
|
"front": ".nav-item, .menu-item"
|
|
},
|
|
"delay": 2000
|
|
},
|
|
"swiper_images": {
|
|
"source_folder": "bg_pic",
|
|
"target_subpath": "server/src/main/resources/static/file",
|
|
"target_names": ["swiperPicture1.jpg", "swiperPicture2.jpg", "swiperPicture3.jpg"],
|
|
"count": 3
|
|
},
|
|
"login_background": {
|
|
"vue_file_subpath": "front/manage_code/src/views/login.vue",
|
|
"images": [
|
|
"http://img.yidaima.cn/7-dcd4fc1e8d0144aaa064bc282d1af289",
|
|
"http://img.yidaima.cn/6-3db67e2e6ff64e018d48282638900a67",
|
|
"http://img.yidaima.cn/5-74ee6fe2f0954a8db2574e8f7fbb8ef2",
|
|
"http://img.yidaima.cn/4-a926593ba8c444a3984100e99ae322bb",
|
|
"http://img.yidaima.cn/3-f91e9a0ed55d45c9aff80a08f554c625",
|
|
"http://img.yidaima.cn/2-2bd41cdd57b54b529dc3a139e66233e0",
|
|
"http://img.yidaima.cn/1-5a0b7ee2344a46bd987c1cafff19b3ba",
|
|
"http://img.yidaima.cn/8-495b9767a2f34197b963a13226d3e1d2",
|
|
"http://img.yidaima.cn/10-991483f941914f3d85bf87a4f8748fee",
|
|
"http://img.yidaima.cn/11-d274f1d695f7472a8fcd2c8c6238a79e",
|
|
"http://img.yidaima.cn/14-0e30653336d849379060ad8c436fe512",
|
|
"http://img.yidaima.cn/16-7a58235a2a4040f99e3431c3cac9ea7a",
|
|
"http://img.yidaima.cn/17-3406a546b81c412d9e3501cdb0f88b12",
|
|
"http://img.yidaima.cn/18-0a2fae1d6634480dabf5471fa86b3623",
|
|
"http://img.yidaima.cn/19-107196938e074c92a40b62c60aed18a4"
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
if config_path.exists():
|
|
try:
|
|
with open(config_path, 'r', encoding='utf-8') as f:
|
|
loaded_config = yaml.safe_load(f) or {}
|
|
# 合并配置
|
|
self._config_data = self._merge_config(default_config, loaded_config)
|
|
except Exception as e:
|
|
print(f"[Config] 加载配置文件失败: {e},使用默认配置")
|
|
self._config_data = default_config
|
|
else:
|
|
print("[Config] 配置文件不存在,使用默认配置")
|
|
self._config_data = default_config
|
|
|
|
# 环境变量覆盖
|
|
self._apply_env_overrides()
|
|
|
|
def _merge_config(self, default: dict, loaded: dict) -> dict:
|
|
"""递归合并配置"""
|
|
result = default.copy()
|
|
for key, value in loaded.items():
|
|
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
|
|
result[key] = self._merge_config(result[key], value)
|
|
else:
|
|
result[key] = value
|
|
return result
|
|
|
|
def _apply_env_overrides(self) -> None:
|
|
"""应用环境变量覆盖"""
|
|
# 微信配置
|
|
if os.environ.get("WECHAT_APPID"):
|
|
self._config_data["wechat"]["appid"] = os.environ.get("WECHAT_APPID")
|
|
if os.environ.get("WECHAT_APPSECRET"):
|
|
self._config_data["wechat"]["appsecret"] = os.environ.get("WECHAT_APPSECRET")
|
|
|
|
# Chrome 路径
|
|
if os.environ.get("CHROME_PATH"):
|
|
self._config_data["chrome"]["path"] = os.environ.get("CHROME_PATH")
|
|
|
|
# Step1 配置
|
|
if os.environ.get("FEAST_USERNAME"):
|
|
self._config_data["step1"]["username"] = os.environ.get("FEAST_USERNAME")
|
|
if os.environ.get("FEAST_PASSWORD"):
|
|
self._config_data["step1"]["password"] = os.environ.get("FEAST_PASSWORD")
|
|
|
|
# 数据库配置
|
|
if os.environ.get("DB_HOST"):
|
|
self._config_data["database"]["host"] = os.environ.get("DB_HOST")
|
|
if os.environ.get("DB_PORT"):
|
|
self._config_data["database"]["port"] = int(os.environ.get("DB_PORT"))
|
|
if os.environ.get("DB_NAME"):
|
|
self._config_data["database"]["database"] = os.environ.get("DB_NAME")
|
|
if os.environ.get("DB_USER"):
|
|
self._config_data["database"]["user"] = os.environ.get("DB_USER")
|
|
if os.environ.get("DB_PASSWORD"):
|
|
self._config_data["database"]["password"] = os.environ.get("DB_PASSWORD")
|
|
|
|
# 夸克网盘配置
|
|
if os.environ.get("QUARK_COOKIES_DIR"):
|
|
self._config_data["quark"]["cookies_dir"] = os.environ.get("QUARK_COOKIES_DIR")
|
|
if os.environ.get("QUARK_ROOT_PATH"):
|
|
self._config_data["quark"]["root_path"] = os.environ.get("QUARK_ROOT_PATH")
|
|
|
|
# 百度网盘配置
|
|
if os.environ.get("BAIDU_COOKIES_DIR"):
|
|
self._config_data["baidu"]["cookies_dir"] = os.environ.get("BAIDU_COOKIES_DIR")
|
|
if os.environ.get("BAIDU_ROOT_PATH"):
|
|
self._config_data["baidu"]["root_path"] = os.environ.get("BAIDU_ROOT_PATH")
|
|
|
|
def get(self, key: str, default: Any = None) -> Any:
|
|
"""
|
|
获取配置值,支持点号分隔的路径
|
|
例如: get("wechat.appid") 或 get("step1.url")
|
|
"""
|
|
keys = key.split('.')
|
|
value = self._config_data
|
|
for k in keys:
|
|
if isinstance(value, dict) and k in value:
|
|
value = value[k]
|
|
else:
|
|
return default
|
|
return value
|
|
|
|
@property
|
|
def wechat_appid(self) -> str:
|
|
return self.get("wechat.appid", "")
|
|
|
|
@property
|
|
def wechat_appsecret(self) -> str:
|
|
return self.get("wechat.appsecret", "")
|
|
|
|
@property
|
|
def chrome_path(self) -> str:
|
|
return self.get("chrome.path", r"C:\Program Files\Google\Chrome\Application\chrome.exe")
|
|
|
|
@property
|
|
def step1_config(self) -> dict:
|
|
return self.get("step1", {})
|
|
|
|
@property
|
|
def step2_config(self) -> dict:
|
|
return self.get("step2", {})
|
|
|
|
@property
|
|
def database_config(self) -> dict:
|
|
return self.get("database", {})
|
|
|
|
@property
|
|
def quark_config(self) -> dict:
|
|
return self.get("quark", {})
|
|
|
|
@property
|
|
def project_screenshot_config(self) -> dict:
|
|
return self.get("project_screenshot", {})
|
|
|
|
|
|
# 全局配置实例
|
|
_config_instance = None
|
|
|
|
|
|
def get_config() -> Config:
|
|
"""获取配置实例(单例模式)"""
|
|
global _config_instance
|
|
if _config_instance is None:
|
|
_config_instance = Config()
|
|
return _config_instance
|
|
|
|
|
|
def reset_config():
|
|
"""重置配置实例(用于重新加载)"""
|
|
global _config_instance
|
|
_config_instance = None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 测试配置加载
|
|
config = get_config()
|
|
print(f"WeChat AppID: {config.wechat_appid}")
|
|
print(f"WeChat AppSecret: {'*' * len(config.wechat_appsecret) if config.wechat_appsecret else '(empty)'}")
|
|
print(f"Chrome Path: {config.chrome_path}")
|
|
print(f"Step1 URL: {config.get('step1.url')}")
|
|
print(f"Step2 URL: {config.get('step2.url')}")
|
|
print(f"Database Host: {config.get('database.host')}")
|
|
print(f"Database Port: {config.get('database.port')}")
|