311 lines
11 KiB
JavaScript
311 lines
11 KiB
JavaScript
// ==========================================================
|
|
// Boss直聘 AI 投递 - 控制面板前端逻辑
|
|
// ==========================================================
|
|
|
|
const $ = (id) => document.getElementById(id);
|
|
let currentStep = 1;
|
|
let loginPollTimer = null;
|
|
let applyPollTimer = null;
|
|
|
|
// ---------- Toast ----------
|
|
function toast(msg, ms = 3000) {
|
|
const t = $('toast');
|
|
t.textContent = msg;
|
|
t.classList.add('show');
|
|
setTimeout(() => t.classList.remove('show'), ms);
|
|
}
|
|
|
|
// ---------- 步骤切换 ----------
|
|
function goToStep(step) {
|
|
currentStep = step;
|
|
document.querySelectorAll('.step').forEach(el => {
|
|
el.classList.toggle('active', parseInt(el.dataset.step) === step);
|
|
});
|
|
for (let i = 1; i <= 5; i++) {
|
|
$(`panel-${i}`).classList.toggle('hidden', i !== step);
|
|
}
|
|
if (step === 5) loadHistory();
|
|
if (step === 3) loadResumes();
|
|
}
|
|
document.querySelectorAll('.step').forEach(el => {
|
|
el.addEventListener('click', () => goToStep(parseInt(el.dataset.step)));
|
|
});
|
|
|
|
// ---------- 通用请求 ----------
|
|
async function api(path, options = {}) {
|
|
const res = await fetch(path, {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
...options,
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({}));
|
|
throw new Error(err.detail || `请求失败 ${res.status}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
// ---------- 徽章工具 ----------
|
|
function badge(text, cls) {
|
|
return `<span class="badge ${cls || ''}">${text}</span>`;
|
|
}
|
|
|
|
// ---------- 初始化状态 ----------
|
|
async function refreshStatus() {
|
|
try {
|
|
const s = await api('/api/status');
|
|
$('clientCode').textContent = s.client_code;
|
|
$('serverReachable').innerHTML = badge(
|
|
s.server_reachable ? '可连接' : '无法连接',
|
|
s.server_reachable ? 'ok' : 'err'
|
|
);
|
|
$('adspowerStatus').innerHTML = badge(
|
|
s.adspower_running ? '运行中' : '未运行',
|
|
s.adspower_running ? 'ok' : 'warn'
|
|
);
|
|
// 顶部徽章
|
|
$('statusBadges').innerHTML =
|
|
badge(s.server_connected ? '已连接' : '未连接', s.server_connected ? 'ok' : '') +
|
|
badge(s.ai_configured ? 'AI已配置' : 'AI未配置', s.ai_configured ? 'ok' : 'warn');
|
|
|
|
if (s.server_connected) {
|
|
updateLoginBadge(s.login_status, s.boss_account);
|
|
}
|
|
return s;
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
|
|
function updateLoginBadge(status, account) {
|
|
const el = $('loginStatus');
|
|
if (status === 'logged_in') {
|
|
el.innerHTML = badge(`已登录 ${escapeHtml(account || '')}`, 'ok');
|
|
} else if (status === 'logging_in') {
|
|
el.innerHTML = badge('登录中...', 'warn');
|
|
} else {
|
|
el.innerHTML = badge('未登录', '');
|
|
}
|
|
}
|
|
|
|
// ==========================================================
|
|
// Step 1: 连接服务端
|
|
// ==========================================================
|
|
$('btnConnect').addEventListener('click', async () => {
|
|
const btn = $('btnConnect');
|
|
btn.disabled = true;
|
|
try {
|
|
const data = await api('/api/connect', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ nickname: $('nickname').value || null }),
|
|
});
|
|
toast(data.is_new ? '注册成功!' : '登录成功!');
|
|
await refreshStatus();
|
|
goToStep(2);
|
|
} catch (e) {
|
|
toast('连接失败:' + e.message, 5000);
|
|
} finally {
|
|
btn.disabled = false;
|
|
}
|
|
});
|
|
|
|
// ==========================================================
|
|
// Step 2: 登录 Boss直聘
|
|
// ==========================================================
|
|
$('btnStartLogin').addEventListener('click', async () => {
|
|
const btn = $('btnStartLogin');
|
|
btn.disabled = true;
|
|
try {
|
|
await api('/api/start-login', { method: 'POST' });
|
|
toast('浏览器已打开,请在 AdsPower 浏览器中完成登录');
|
|
$('loginSpinner').classList.remove('hidden');
|
|
startLoginPoll();
|
|
} catch (e) {
|
|
toast('打开浏览器失败:' + e.message, 5000);
|
|
btn.disabled = false;
|
|
}
|
|
});
|
|
|
|
function startLoginPoll() {
|
|
if (loginPollTimer) clearInterval(loginPollTimer);
|
|
loginPollTimer = setInterval(async () => {
|
|
try {
|
|
const r = await api('/api/login-status');
|
|
updateLoginBadge(r.status, r.boss_account);
|
|
if (r.status === 'logged_in') {
|
|
clearInterval(loginPollTimer);
|
|
$('loginSpinner').classList.add('hidden');
|
|
$('btnStartLogin').disabled = false;
|
|
toast('登录成功!即将进入简历步骤');
|
|
setTimeout(() => goToStep(3), 1200);
|
|
}
|
|
} catch (e) { /* 忽略 */ }
|
|
}, 2500);
|
|
}
|
|
|
|
// ==========================================================
|
|
// Step 3: 抓取简历
|
|
// ==========================================================
|
|
$('btnScrape').addEventListener('click', async () => {
|
|
const btn = $('btnScrape');
|
|
btn.disabled = true;
|
|
btn.textContent = '正在抓取...';
|
|
try {
|
|
const data = await api('/api/scrape-resumes', { method: 'POST' });
|
|
toast(`抓取到 ${data.count} 份简历`);
|
|
loadResumes();
|
|
} catch (e) {
|
|
toast('抓取失败:' + e.message, 5000);
|
|
} finally {
|
|
btn.disabled = false;
|
|
btn.textContent = '开始抓取简历';
|
|
}
|
|
});
|
|
|
|
async function loadResumes() {
|
|
try {
|
|
const resumes = await api('/api/resumes');
|
|
const list = $('resumeList');
|
|
if (!resumes.length) {
|
|
list.innerHTML = '<p class="hint">暂无简历,请先抓取。</p>';
|
|
return;
|
|
}
|
|
list.innerHTML = resumes.map(r => `
|
|
<div class="resume-item ${r.is_primary ? 'primary' : ''}" data-id="${r.id}">
|
|
<div class="radio"></div>
|
|
<div>
|
|
<div class="r-title">${escapeHtml(r.title)}</div>
|
|
<div class="r-pos">${escapeHtml(r.position || '未设置求职意向')}</div>
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
list.querySelectorAll('.resume-item').forEach(el => {
|
|
el.addEventListener('click', () => setPrimary(parseInt(el.dataset.id)));
|
|
});
|
|
} catch (e) { console.error(e); }
|
|
}
|
|
|
|
async function setPrimary(id) {
|
|
try {
|
|
await api(`/api/resumes/${id}/primary`, { method: 'POST' });
|
|
toast('已设为主简历');
|
|
loadResumes();
|
|
// 允许进入下一步
|
|
setTimeout(() => { if (currentStep === 3) goToStep(4); }, 600);
|
|
} catch (e) {
|
|
toast('设置失败:' + e.message);
|
|
}
|
|
}
|
|
|
|
// ==========================================================
|
|
// Step 4: 开始 / 结束投递
|
|
// ==========================================================
|
|
$('btnStartApply').addEventListener('click', async () => {
|
|
const keywords = $('keywords').value.split(',').map(k => k.trim()).filter(Boolean);
|
|
if (!keywords.length) { toast('请填写搜索关键词'); return; }
|
|
|
|
const btn = $('btnStartApply');
|
|
btn.disabled = true;
|
|
try {
|
|
await api('/api/start-apply', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
keywords,
|
|
city_code: $('cityCode').value,
|
|
salary_min: parseInt($('salaryMin').value) || null,
|
|
salary_max: parseInt($('salaryMax').value) || null,
|
|
greeting: $('greeting').value,
|
|
min_score: parseInt($('minScore').value) || 65,
|
|
}),
|
|
});
|
|
toast('投递任务已启动');
|
|
toggleApplyUI(true);
|
|
startApplyPoll();
|
|
} catch (e) {
|
|
toast('启动失败:' + e.message, 5000);
|
|
btn.disabled = false;
|
|
}
|
|
});
|
|
|
|
$('btnStopApply').addEventListener('click', async () => {
|
|
try {
|
|
const r = await api('/api/stop-apply', { method: 'POST' });
|
|
toast(r.msg);
|
|
} catch (e) { toast('停止失败:' + e.message); }
|
|
});
|
|
|
|
function toggleApplyUI(running) {
|
|
$('btnStartApply').classList.toggle('hidden', running);
|
|
$('btnStopApply').classList.toggle('hidden', !running);
|
|
$('progressBox').classList.toggle('hidden', !running);
|
|
$('btnStartApply').disabled = false;
|
|
}
|
|
|
|
function startApplyPoll() {
|
|
if (applyPollTimer) clearInterval(applyPollTimer);
|
|
applyPollTimer = setInterval(async () => {
|
|
try {
|
|
const r = await api('/api/apply-status');
|
|
if (r.stats) {
|
|
$('progressCurrent').textContent = r.stats.current || '';
|
|
$('statApplied').textContent = r.stats.applied || 0;
|
|
$('statSkipped').textContent = r.stats.skipped || 0;
|
|
}
|
|
if (!r.running) {
|
|
clearInterval(applyPollTimer);
|
|
toggleApplyUI(false);
|
|
toast('投递任务已结束');
|
|
}
|
|
} catch (e) { /* 忽略 */ }
|
|
}, 2000);
|
|
}
|
|
|
|
// ==========================================================
|
|
// Step 5: 历史与统计
|
|
// ==========================================================
|
|
async function loadHistory() {
|
|
try {
|
|
const [stats, history] = await Promise.all([
|
|
api('/api/stats'),
|
|
api('/api/history?limit=50'),
|
|
]);
|
|
$('statsGrid').innerHTML = `
|
|
<div class="stat-card"><div class="num">${stats.total_applied || 0}</div><div class="label">累计投递</div></div>
|
|
<div class="stat-card"><div class="num">${stats.total_chat || 0}</div><div class="label">立即沟通</div></div>
|
|
<div class="stat-card"><div class="num">${stats.total_deliver || 0}</div><div class="label">投递简历</div></div>
|
|
<div class="stat-card"><div class="num">${stats.today_applied || 0}</div><div class="label">今日投递</div></div>
|
|
`;
|
|
$('historyBody').innerHTML = history.map(h => `
|
|
<tr>
|
|
<td>${escapeHtml(h.job_title || '-')}</td>
|
|
<td>${escapeHtml(h.company || '-')}</td>
|
|
<td>${escapeHtml(h.salary || '-')}</td>
|
|
<td><span class="tag ${safeType(h.apply_type)}">${typeLabel(h.apply_type)}</span></td>
|
|
<td>${h.ai_score >= 0 ? h.ai_score : '-'}</td>
|
|
<td>${formatTime(h.applied_at)}</td>
|
|
</tr>
|
|
`).join('') || '<tr><td colspan="6" style="text-align:center;color:#999">暂无记录</td></tr>';
|
|
} catch (e) { console.error(e); }
|
|
}
|
|
|
|
function typeLabel(t) {
|
|
return { chat: '沟通', deliver: '投递', skipped: '跳过' }[t] || t;
|
|
}
|
|
// apply_type 仅允许白名单值用作 CSS class,防止注入
|
|
function safeType(t) {
|
|
return ['chat', 'deliver', 'skipped'].includes(t) ? t : 'skipped';
|
|
}
|
|
function formatTime(s) {
|
|
if (!s) return '-';
|
|
return s.replace('T', ' ').substring(0, 16);
|
|
}
|
|
function escapeHtml(s) {
|
|
if (!s) return '';
|
|
return s.replace(/[&<>"']/g, c => ({
|
|
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
|
}[c]));
|
|
}
|
|
|
|
// ---------- 启动 ----------
|
|
refreshStatus();
|
|
setInterval(refreshStatus, 8000);
|