mirror of
https://github.com/ddCat-main/cursor-auto-register.git
synced 2025-12-24 13:38:01 +08:00
增加系统配置可视化
This commit is contained in:
104
api.py
104
api.py
@@ -23,6 +23,7 @@ import concurrent.futures
|
||||
from functools import lru_cache
|
||||
from config import MAX_ACCOUNTS, REGISTRATION_INTERVAL, API_HOST, API_PORT, API_DEBUG, API_WORKERS
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 全局状态追踪
|
||||
registration_status = {
|
||||
@@ -971,6 +972,109 @@ async def use_account_token(id: int):
|
||||
error(traceback.format_exc())
|
||||
return {"success": False, "message": f"使用Token失败: {str(e)}"}
|
||||
|
||||
# 添加配置相关模型
|
||||
class ConfigModel(BaseModel):
|
||||
BROWSER_HEADLESS: bool
|
||||
BROWSER_USER_AGENT: str
|
||||
MAX_ACCOUNTS: int
|
||||
EMAIL_DOMAINS: str
|
||||
EMAIL_USERNAME: str
|
||||
EMAIL_PIN: str
|
||||
BROWSER_PATH: Optional[str] = None
|
||||
CURSOR_PATH: Optional[str] = None
|
||||
|
||||
# 获取配置端点
|
||||
@app.get("/config", tags=["Config"])
|
||||
async def get_config():
|
||||
"""获取当前系统配置"""
|
||||
try:
|
||||
# 重新加载配置以确保获取最新值
|
||||
load_dotenv()
|
||||
|
||||
config = {
|
||||
"BROWSER_HEADLESS": os.getenv("BROWSER_HEADLESS", "True").lower() == "true",
|
||||
"BROWSER_USER_AGENT": os.getenv("BROWSER_USER_AGENT", ""),
|
||||
"MAX_ACCOUNTS": int(os.getenv("MAX_ACCOUNTS", "10")),
|
||||
"EMAIL_DOMAINS": os.getenv("EMAIL_DOMAINS", ""),
|
||||
"EMAIL_USERNAME": os.getenv("EMAIL_USERNAME", ""),
|
||||
"EMAIL_PIN": os.getenv("EMAIL_PIN", ""),
|
||||
"BROWSER_PATH": os.getenv("BROWSER_PATH", ""),
|
||||
"CURSOR_PATH": os.getenv("CURSOR_PATH", "")
|
||||
}
|
||||
|
||||
return {"success": True, "data": config}
|
||||
except Exception as e:
|
||||
error(f"获取配置失败: {str(e)}")
|
||||
error(traceback.format_exc())
|
||||
return {"success": False, "message": f"获取配置失败: {str(e)}"}
|
||||
|
||||
# 更新配置端点
|
||||
@app.put("/config", tags=["Config"])
|
||||
async def update_config(config: ConfigModel):
|
||||
"""更新系统配置"""
|
||||
try:
|
||||
# 获取.env文件路径
|
||||
env_path = Path(__file__).parent / ".env"
|
||||
|
||||
# 读取当前.env文件内容
|
||||
current_lines = []
|
||||
if env_path.exists():
|
||||
with open(env_path, "r", encoding="utf-8") as f:
|
||||
current_lines = f.readlines()
|
||||
|
||||
# 构建配置字典
|
||||
config_dict = {
|
||||
"BROWSER_HEADLESS": str(config.BROWSER_HEADLESS),
|
||||
"BROWSER_USER_AGENT": config.BROWSER_USER_AGENT,
|
||||
"MAX_ACCOUNTS": str(config.MAX_ACCOUNTS),
|
||||
"EMAIL_DOMAINS": config.EMAIL_DOMAINS,
|
||||
"EMAIL_USERNAME": config.EMAIL_USERNAME,
|
||||
"EMAIL_PIN": config.EMAIL_PIN
|
||||
}
|
||||
|
||||
# 添加可选配置(如果提供)
|
||||
if config.BROWSER_PATH:
|
||||
config_dict["BROWSER_PATH"] = config.BROWSER_PATH
|
||||
if config.CURSOR_PATH:
|
||||
config_dict["CURSOR_PATH"] = config.CURSOR_PATH
|
||||
|
||||
# 处理现有行或创建新行
|
||||
updated_lines = []
|
||||
updated_keys = set()
|
||||
|
||||
for line in current_lines:
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
updated_lines.append(line)
|
||||
continue
|
||||
|
||||
key, value = line.split("=", 1) if "=" in line else (line, "")
|
||||
key = key.strip()
|
||||
|
||||
if key in config_dict:
|
||||
updated_lines.append(f"{key}={config_dict[key]}")
|
||||
updated_keys.add(key)
|
||||
else:
|
||||
updated_lines.append(line)
|
||||
|
||||
# 添加未更新的配置项
|
||||
for key, value in config_dict.items():
|
||||
if key not in updated_keys and value:
|
||||
updated_lines.append(f"{key}={value}")
|
||||
|
||||
# 写入更新后的配置
|
||||
with open(env_path, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(updated_lines))
|
||||
|
||||
# 重新加载环境变量
|
||||
load_dotenv(override=True)
|
||||
|
||||
return {"success": True, "message": "配置已更新"}
|
||||
except Exception as e:
|
||||
error(f"更新配置失败: {str(e)}")
|
||||
error(traceback.format_exc())
|
||||
return {"success": False, "message": f"更新配置失败: {str(e)}"}
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
"api:app",
|
||||
|
||||
170
index.html
170
index.html
@@ -600,6 +600,78 @@
|
||||
<div id="alert-container"></div>
|
||||
|
||||
<div class="container py-4">
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
<div class="card">
|
||||
<div class="card-header bg-white">
|
||||
<div class="d-flex justify-content-between align-items-center">
|
||||
<h5 class="mb-0">系统配置</h5>
|
||||
<button id="edit-config-btn" class="btn btn-sm btn-primary">
|
||||
<i class="fas fa-edit me-1"></i> 编辑配置
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form id="config-form">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="mb-3">
|
||||
<label for="browser-headless" class="form-label">浏览器无头模式</label>
|
||||
<select id="browser-headless" class="form-control" disabled>
|
||||
<option value="true">是 (无界面)</option>
|
||||
<option value="false">否 (有界面)</option>
|
||||
</select>
|
||||
<small class="text-muted">True=无界面运行浏览器,False=显示浏览器界面</small>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="email-domains" class="form-label">邮箱域名</label>
|
||||
<input type="text" id="email-domains" class="form-control" disabled>
|
||||
<small class="text-muted">用于注册的邮箱域名,多个域名用逗号分隔</small>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="browser-useragent" class="form-label">浏览器User-Agent</label>
|
||||
<input type="text" id="browser-useragent" class="form-control" disabled>
|
||||
<small class="text-muted">浏览器模拟的用户代理字符串</small>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="max-accounts" class="form-label">最大激活账号数量</label>
|
||||
<input type="number" id="max-accounts" class="form-control" min="1" disabled>
|
||||
<small class="text-muted">系统允许创建的最大账号数量</small>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="mb-3">
|
||||
<label for="email-username" class="form-label">临时邮箱用户名(只用用户名,示例:ddcat28,访问:https://tempmail.plus/ 获取)</label>
|
||||
<input type="text" id="email-username" class="form-control" disabled>
|
||||
<small class="text-muted">用于接收验证码的临时邮箱用户名</small>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="email-pin" class="form-label">临时邮箱PIN</label>
|
||||
<input type="text" id="email-pin" class="form-control" disabled>
|
||||
<small class="text-muted">临时邮箱PIN码(如果需要)</small>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="browser-path" class="form-label">浏览器路径 (可选,windows下自定义浏览器安装目录时需修改该配置)</label>
|
||||
<input type="text" id="browser-path" class="form-control" disabled>
|
||||
<small class="text-muted">Windows下浏览器可执行文件的完整路径</small>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cursor-path" class="form-label">Cursor路径 (可选,windows下自定义cursor安装目录时需修改该配置)</label>
|
||||
<input type="text" id="cursor-path" class="form-control" disabled>
|
||||
<small class="text-muted">Cursor安装目录的完整路径</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-end">
|
||||
<button type="button" id="cancel-config-btn" class="btn btn-secondary me-2" style="display:none">取消</button>
|
||||
<button type="submit" id="save-config-btn" class="btn btn-success" style="display:none">保存配置</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mb-4">
|
||||
<!-- 将左侧任务控制卡片调整为全宽 -->
|
||||
<div class="col-12">
|
||||
@@ -752,6 +824,9 @@
|
||||
|
||||
// 应用初始化函数 - 提高代码组织性
|
||||
function initializeApplication() {
|
||||
// 加载配置
|
||||
loadConfig();
|
||||
|
||||
// 初始加载数据
|
||||
loadAllData();
|
||||
|
||||
@@ -789,6 +864,21 @@
|
||||
});
|
||||
|
||||
// 可以添加更多事件绑定...
|
||||
|
||||
// 在bindEventHandlers函数中添加配置相关事件
|
||||
$("#edit-config-btn").click(function() {
|
||||
enableConfigForm(true);
|
||||
});
|
||||
|
||||
$("#cancel-config-btn").click(function() {
|
||||
enableConfigForm(false);
|
||||
loadConfig(); // 重新加载配置
|
||||
});
|
||||
|
||||
$("#config-form").submit(function(e) {
|
||||
e.preventDefault();
|
||||
saveConfig();
|
||||
});
|
||||
}
|
||||
|
||||
// 全局变量
|
||||
@@ -1727,6 +1817,86 @@
|
||||
</td>
|
||||
`;
|
||||
}
|
||||
|
||||
// 加载配置函数
|
||||
function loadConfig() {
|
||||
showLoading();
|
||||
fetch('/config')
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
hideLoading();
|
||||
if (data.success) {
|
||||
const config = data.data;
|
||||
$("#browser-headless").val(config.BROWSER_HEADLESS.toString());
|
||||
$("#browser-useragent").val(config.BROWSER_USER_AGENT);
|
||||
$("#max-accounts").val(config.MAX_ACCOUNTS);
|
||||
$("#email-domains").val(config.EMAIL_DOMAINS);
|
||||
$("#email-username").val(config.EMAIL_USERNAME);
|
||||
$("#email-pin").val(config.EMAIL_PIN);
|
||||
$("#browser-path").val(config.BROWSER_PATH || '');
|
||||
$("#cursor-path").val(config.CURSOR_PATH || '');
|
||||
} else {
|
||||
showAlert(`加载配置失败: ${data.message || '未知错误'}`, 'danger');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('加载配置时发生错误:', error);
|
||||
hideLoading();
|
||||
showAlert('加载配置失败,请稍后重试', 'danger');
|
||||
});
|
||||
}
|
||||
|
||||
// 保存配置函数
|
||||
function saveConfig() {
|
||||
showLoading();
|
||||
const config = {
|
||||
BROWSER_HEADLESS: $("#browser-headless").val() === 'true',
|
||||
BROWSER_USER_AGENT: $("#browser-useragent").val(),
|
||||
MAX_ACCOUNTS: parseInt($("#max-accounts").val()),
|
||||
EMAIL_DOMAINS: $("#email-domains").val(),
|
||||
EMAIL_USERNAME: $("#email-username").val(),
|
||||
EMAIL_PIN: $("#email-pin").val(),
|
||||
BROWSER_PATH: $("#browser-path").val(),
|
||||
CURSOR_PATH: $("#cursor-path").val()
|
||||
};
|
||||
|
||||
fetch('/config', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(config)
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
hideLoading();
|
||||
if (data.success) {
|
||||
showAlert('配置已成功保存', 'success');
|
||||
enableConfigForm(false); // 禁用编辑状态
|
||||
} else {
|
||||
showAlert(`保存配置失败: ${data.message || '未知错误'}`, 'danger');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('保存配置时发生错误:', error);
|
||||
hideLoading();
|
||||
showAlert('保存配置失败,请稍后重试', 'danger');
|
||||
});
|
||||
}
|
||||
|
||||
// 启用/禁用配置表单
|
||||
function enableConfigForm(enable) {
|
||||
const inputs = $('#config-form select, #config-form input');
|
||||
if (enable) {
|
||||
inputs.prop('disabled', false);
|
||||
$('#save-config-btn, #cancel-config-btn').show();
|
||||
$('#edit-config-btn').hide();
|
||||
} else {
|
||||
inputs.prop('disabled', true);
|
||||
$('#save-config-btn, #cancel-config-btn').hide();
|
||||
$('#edit-config-btn').show();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- 添加删除确认对话框 -->
|
||||
|
||||
Reference in New Issue
Block a user