// 全局变量存储当前选中的Profile和代理 let currentProfileId = null; let currentProxyInfo = null; // 格式化JSON显示 function formatResult(data, elementId) { const el = document.getElementById(elementId); el.innerHTML = `
${JSON.stringify(data, null, 2)}`;
}
// 查询Profile列表
async function listProfiles() {
try {
showToast('正在查询Profile列表...', 'info');
const response = await fetch(`${API_BASE}/api/adspower/profiles`);
const data = await response.json();
if (data.success) {
const profiles = data.data?.data?.list || [];
if (profiles.length > 0) {
// 美化显示Profile信息
let html = '找到 ${profiles.length} 个Profile环境
`; profiles.forEach((profile, idx) => { const proxyConfig = profile.user_proxy_config || {}; const hasProxy = proxyConfig.proxy_host && proxyConfig.proxy_port; html += `未找到Profile环境
'; showToast('未找到Profile', 'info'); } } else { showToast(data.message || '查询失败', 'error'); } } catch (error) { console.error('查询Profile列表错误:', error); showToast('查询失败: ' + error.message, 'error'); } } // 选择Profile function selectProfile(profileId, profileName) { currentProfileId = profileId; document.getElementById('profileId').value = profileId; showToast(`已选择环境: ${profileName}`, 'success'); } // 启动浏览器 async function startBrowser() { const profileId = document.getElementById('profileId').value || currentProfileId; if (!profileId) { showToast('请先查询Profile列表或输入Profile ID', 'error'); return; } try { showToast('正在启动浏览器...', 'info'); const response = await fetch(`${API_BASE}/api/adspower/browser/start`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_id: profileId }) }); const data = await response.json(); if (data.success) { formatResult(data.data, 'profileResult'); showToast('浏览器启动成功', 'success'); } else { showToast(data.message || '启动失败', 'error'); } } catch (error) { showToast('启动失败: ' + error.message, 'error'); } } // 停止浏览器 async function stopBrowser() { const profileId = document.getElementById('profileId').value || currentProfileId; if (!profileId) { showToast('请先输入Profile ID', 'error'); return; } try { showToast('正在停止浏览器...', 'info'); const response = await fetch(`${API_BASE}/api/adspower/browser/stop`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_id: profileId }) }); const data = await response.json(); if (data.success) { formatResult(data.data, 'profileResult'); showToast('浏览器已停止', 'success'); } else { showToast(data.message || '停止失败', 'error'); } } catch (error) { showToast('停止失败: ' + error.message, 'error'); } } // 获取大麦IP async function getDamaiProxy() { try { showToast('正在获取大麦IP...', 'info'); const response = await fetch(`${API_BASE}/api/adspower/proxy/damai`); const data = await response.json(); if (data.success) { currentProxyInfo = data.data; formatResult(data.data, 'proxyResult'); showToast(`获取成功: ${data.data.host}:${data.data.port}`, 'success'); } else { showToast(data.message || '获取失败', 'error'); } } catch (error) { showToast('获取失败: ' + error.message, 'error'); } } // 创建代理 async function createProxy() { if (!currentProxyInfo) { showToast('请先获取大麦IP', 'error'); return; } try { showToast('正在创建代理...', 'info'); const proxyConfig = { type: 'http', host: currentProxyInfo.host, port: currentProxyInfo.port, user: '69538fdef04e1', password: '63v0kQBr2yJXnjf', ipchecker: 'ip2location', remark: 'Damai Auto Proxy' }; const response = await fetch(`${API_BASE}/api/adspower/proxy/create`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ proxy_config: proxyConfig }) }); const data = await response.json(); if (data.success) { document.getElementById('proxyId').value = data.data.proxy_id; formatResult(data.data, 'proxyResult'); showToast(`代理创建成功,ID: ${data.data.proxy_id}`, 'success'); } else { showToast(data.message || '创建失败', 'error'); } } catch (error) { showToast('创建失败: ' + error.message, 'error'); } } // 查询代理列表 async function listProxies() { try { showToast('正在查询代理列表...', 'info'); const response = await fetch(`${API_BASE}/api/adspower/proxy/list?page=1&limit=50`); // 检查响应类型 const contentType = response.headers.get('content-type'); if (!contentType || !contentType.includes('application/json')) { console.error('服务器返回非JSON响应:', contentType); const text = await response.text(); console.error('响应内容:', text.substring(0, 200)); showToast('服务器响应错误,请检查后端服务', 'error'); return; } const data = await response.json(); if (data.success && data.data) { const proxies = data.data?.data?.list || []; if (proxies.length > 0) { // 格式化显示代理信息 let html = '找到 ${proxies.length} 个代理
`; proxies.forEach((proxy, idx) => { html += `暂无代理,请先创建代理
'; showToast('暂无代理', 'info'); } } else { showToast(data.message || '查询失败', 'error'); document.getElementById('proxyResult').innerHTML = `${data.message || '查询失败'}
`; } } catch (error) { console.error('查询代理列表错误:', error); showToast('查询失败: ' + error.message, 'error'); document.getElementById('proxyResult').innerHTML = `错误: ${error.message}
`; } } // 更新Profile代理(API v2方式) async function updateProfileProxy() { const profileId = document.getElementById('profileId').value; const proxyId = document.getElementById('proxyId').value; if (!profileId || !proxyId) { showToast('请输入Profile ID和代理ID', 'error'); return; } try { showToast('正在更新Profile代理 (API v2)...', 'info'); const response = await fetch(`${API_BASE}/api/adspower/profile/update`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ profile_id: profileId, proxy_id: proxyId }) }); const data = await response.json(); if (data.success) { formatResult(data.data, 'updateResult'); showToast('更新成功 (API v2)', 'success'); } else { showToast(data.message || '更新失败', 'error'); } } catch (error) { showToast('更新失败: ' + error.message, 'error'); } } // 更新Profile代理(API v1方式) async function updateProfileProxyV1() { const profileId = document.getElementById('profileId').value; if (!profileId) { showToast('请输入Profile ID', 'error'); return; } // 获取当前代理信息 if (!currentProxyInfo) { showToast('请先获取大麦IP', 'error'); return; } try { showToast('正在更新Profile代理 (API v1)...', 'info'); // 构建 proxy_config const proxyConfig = { proxy_type: 'http', proxy_host: currentProxyInfo.host, proxy_port: currentProxyInfo.port, proxy_user: '69538fdef04e1', proxy_password: '63v0kQBr2yJXnjf', proxy_soft: 'other' }; const response = await fetch(`${API_BASE}/api/adspower/profile/update-v1`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ profile_id: profileId, proxy_config: proxyConfig }) }); const data = await response.json(); if (data.success) { let resultHtml = '${data.message || '更新失败'}
`; } } catch (error) { console.error('更新Profile代理错误 (v1):', error); showToast('更新失败: ' + error.message, 'error'); document.getElementById('updateResult').innerHTML = `错误: ${error.message}
`; } } // 完整测试流程 async function fullTest(useProxy) { const resultDiv = document.getElementById('fullTestResult'); let log = []; function addLog(msg, type = 'info') { const time = new Date().toLocaleTimeString(); const color = type === 'error' ? '#ff4d4f' : type === 'success' ? '#52c41a' : '#666'; log.push(`