// 全局变量存储当前选中的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 = '
'; html += `

找到 ${profiles.length} 个Profile环境

`; profiles.forEach((profile, idx) => { const proxyConfig = profile.user_proxy_config || {}; const hasProxy = proxyConfig.proxy_host && proxyConfig.proxy_port; html += `
#${idx + 1} ${profile.name} No.${profile.profile_no}
Profile ID: ${profile.profile_id}
当前IP: ${profile.ip || 'N/A'} ${profile.ip_country ? `(${profile.ip_country.toUpperCase()})` : ''}
最后打开: ${profile.last_open_time ? new Date(parseInt(profile.last_open_time) * 1000).toLocaleString('zh-CN') : 'N/A'}
${hasProxy ? `
✓ 代理配置
类型: ${proxyConfig.proxy_type?.toUpperCase() || 'N/A'}
地址: ${proxyConfig.proxy_host}:${proxyConfig.proxy_port}
${proxyConfig.latest_ip ? `
最新IP: ${proxyConfig.latest_ip}
` : ''}
${proxyConfig.proxy_user ? `
认证: ${proxyConfig.proxy_user}
` : ''}
` : `
⚠ 未配置代理
`} ${profile.remark ? `
备注: ${profile.remark}
` : ''}
`; }); html += '
'; document.getElementById('profileResult').innerHTML = html; // 自动选择第一个Profile currentProfileId = profiles[0].profile_id; document.getElementById('profileId').value = currentProfileId; showToast(`找到 ${profiles.length} 个Profile环境`, 'success'); } else { document.getElementById('profileResult').innerHTML = '

未找到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 = '
'; html += `

找到 ${proxies.length} 个代理

`; proxies.forEach((proxy, idx) => { html += `
#${idx + 1} ID: ${proxy.proxy_id}
${proxy.type} - ${proxy.host}:${proxy.port}
${proxy.remark ? `
备注: ${proxy.remark}
` : ''}
关联环境数: ${proxy.profile_count || 0}
`; }); html += '
'; document.getElementById('proxyResult').innerHTML = html; showToast(`找到 ${proxies.length} 个代理`, 'success'); } else { document.getElementById('proxyResult').innerHTML = '

暂无代理,请先创建代理

'; 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 = '
'; resultHtml += '
✅ 代理更新成功 (API v1)
'; resultHtml += '
'; resultHtml += `
Profile ID: ${profileId}
`; resultHtml += `
代理地址: ${proxyConfig.proxy_host}:${proxyConfig.proxy_port}
`; resultHtml += `
认证用户: ${proxyConfig.proxy_user}
`; resultHtml += '
'; document.getElementById('updateResult').innerHTML = resultHtml; showToast('更新成功 (API v1)', 'success'); } else { showToast(data.message || '更新失败', 'error'); document.getElementById('updateResult').innerHTML = `

${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(`
[${time}] ${msg}
`); resultDiv.innerHTML = log.join(''); } try { addLog('开始完整测试流程...'); // 1. 查询Profile addLog('步骤1: 查询Profile列表'); const profileRes = await fetch(`${API_BASE}/api/adspower/profiles`); const profileData = await profileRes.json(); if (!profileData.success) { addLog('查询Profile失败: ' + profileData.message, 'error'); return; } const profiles = profileData.data?.data?.list || []; if (profiles.length === 0) { addLog('未找到Profile', 'error'); return; } currentProfileId = profiles[0].profile_id; document.getElementById('profileId').value = currentProfileId; addLog(`找到Profile: ${currentProfileId}`, 'success'); if (useProxy) { // 2. 获取大麦IP addLog('步骤2: 获取大麦IP代理'); const proxyRes = await fetch(`${API_BASE}/api/adspower/proxy/damai`); const proxyData = await proxyRes.json(); if (!proxyData.success) { addLog('获取代理失败: ' + proxyData.message, 'error'); return; } currentProxyInfo = proxyData.data; addLog(`获取代理: ${currentProxyInfo.host}:${currentProxyInfo.port}`, 'success'); // 3. 创建代理 addLog('步骤3: 创建AdsPower代理'); const proxyConfig = { type: 'http', host: currentProxyInfo.host, port: currentProxyInfo.port, user: '69538fdef04e1', password: '63v0kQBr2yJXnjf', ipchecker: 'ip2location', remark: 'Damai Auto Proxy' }; const createProxyRes = await fetch(`${API_BASE}/api/adspower/proxy/create`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ proxy_config: proxyConfig }) }); const createProxyData = await createProxyRes.json(); if (!createProxyData.success) { addLog('创建代理失败: ' + createProxyData.message, 'error'); return; } const proxyId = createProxyData.data.proxy_id; document.getElementById('proxyId').value = proxyId; addLog(`创建代理成功,ID: ${proxyId}`, 'success'); // 4. 更新Profile addLog('步骤4: 更新Profile代理配置'); const updateRes = await fetch(`${API_BASE}/api/adspower/profile/update`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ profile_id: currentProfileId, proxy_id: proxyId }) }); const updateData = await updateRes.json(); if (!updateData.success) { addLog('更新Profile失败: ' + updateData.message, 'error'); return; } addLog('更新Profile成功', 'success'); } // 5. 启动浏览器 addLog(`步骤${useProxy ? 5 : 2}: 启动浏览器`); const startRes = await fetch(`${API_BASE}/api/adspower/browser/start`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_id: currentProfileId }) }); const startData = await startRes.json(); if (!startData.success) { addLog('启动浏览器失败: ' + startData.message, 'error'); return; } addLog('浏览器启动成功', 'success'); addLog('测试流程完成!', 'success'); showToast('完整测试流程执行成功', 'success'); } catch (error) { addLog('测试异常: ' + error.message, 'error'); showToast('测试失败: ' + error.message, 'error'); } }