| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745 |
- const http = require('http');
- const fs = require('fs');
- const path = require('path');
- const { URL } = require('url');
- const { outputsRoot } = require('../core/output-paths');
- const {
- qiweiSyncExternalGroups,
- qiweiListExternalGroups,
- qiweiAnalyzeGroupMembers,
- qiweiConfirmExternalGroup,
- qiweiAddExternalGroup,
- qiweiConfigureGroupKeywords,
- qiweiRejectExternalGroup,
- qiweiSyncGroupMessages
- } = require('../tools/qiwei-group-management-run');
- const {
- qiweiLoginStatus,
- qiweiLoginStart,
- qiweiLoginCheck,
- qiweiLoginVerify
- } = require('../tools/qiwei-login-run');
- const { qiweiSubscriptionStatus } = require('../tools/qiwei-subscription-run');
- const { createAccountConnectionMonitor } = require('../core/account-connection-monitor');
- const {
- qiweiBatchAddFriends,
- qiweiCheckFriendStatus,
- qiweiGetCustomerProfile,
- qiweiAutoCreateGroup,
- qiweiUpdateCustomerInfo
- } = require('../tools/qiwei-customer-ops-run');
- const {
- qiweiUpdateCustomerPortrait,
- qiweiSaveCustomerPortrait,
- qiweiBatchUpdateCustomerPortrait,
- qiweiExportCustomerPortraits,
- qiweiListCustomerTags,
- qiweiListAllTags,
- qiweiAddCustomerTags,
- qiweiRemoveCustomerTags,
- qiweiSyncPersonalLabels,
- qiweiCreatePersonalLabel,
- qiweiUpdatePersonalLabel,
- qiweiDeletePersonalLabel,
- qiweiApplyPersonalLabels
- } = require('../tools/qiwei-portrait-tags-run');
- const {
- qiweiPreviewTransferPackage,
- qiweiExecuteTransfer
- } = require('../tools/qiwei-customer-transfer-run');
- const {
- getStateSection,
- rebuildStateFromOutputs
- } = require('../core/dashboard-state');
- const {
- switchActiveAccount,
- getAgentStatus,
- getConversations,
- syncConversations,
- changeGlobalMode,
- changeConversationMode,
- approveReply,
- approveDraft,
- rejectDraft,
- regenerateDraft,
- generateLatestDraft,
- manualSend,
- updateCustomerTask,
- syncCustomerTaskToOfficialTodo,
- updateCustomerAlert,
- getAudit,
- startListener,
- stopListener
- } = require('./agent-service');
- const {
- listKnowledgeTree,
- readKnowledgeFile,
- listProperties,
- getProperty,
- listSkillRegistry,
- getSkillDetail
- } = require('./workspace-library-service');
- const {
- getMeetingKnowledgeHub,
- syncMeetingKnowledge,
- analyzeMeetingKnowledge,
- getMeetingKnowledge
- } = require('./meeting-knowledge-service');
- const {
- getDocumentKnowledgeHub,
- importDocumentKnowledge,
- createDocumentKnowledge,
- analyzeDocumentKnowledge,
- refreshDocumentKnowledge,
- getDocumentKnowledge,
- getTodoKnowledgeHub,
- searchTodoUsers,
- syncTodoKnowledge,
- refreshTodoKnowledgeDetails,
- createTodoKnowledge,
- completeTodoKnowledge
- } = require('./official-office-knowledge-service');
- const {
- getUnifiedTaskHub,
- createUnifiedLocalTask,
- updateUnifiedTask
- } = require('./unified-task-service');
- const {
- getCustomerMasterHub,
- updateCustomerMaster,
- updateCustomerRecommendationFeedback
- } = require('./customer-master-service');
- const DASHBOARD_PORT = process.env.QIWEI_DASHBOARD_PORT || 4320;
- const STATIC_DIR = path.join(__dirname);
- const TMP_DIR = path.join(outputsRoot(), 'tmp');
- const jobs = new Map();
- const accountConnectionMonitor = createAccountConnectionMonitor({
- checkStatus: () => qiweiLoginStatus({}),
- recoverLogin: () => qiweiLoginCheck({ manual: true, persistConfig: false })
- });
- accountConnectionMonitor.start();
- function ensureTmpDir() {
- fs.mkdirSync(TMP_DIR, { recursive: true });
- }
- function readBody(req) {
- return new Promise((resolve, reject) => {
- const chunks = [];
- req.on('data', chunk => chunks.push(chunk));
- req.on('end', () => {
- try {
- resolve(JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}'));
- } catch {
- resolve({});
- }
- });
- req.on('error', reject);
- });
- }
- function json(res, status, body) {
- res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
- res.end(JSON.stringify(body));
- }
- function createJob(runFn) {
- const id = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
- const job = { id, status: 'running', progress: 0, result: null, error: null, startedAt: Date.now() };
- jobs.set(id, job);
- runFn()
- .then(r => { job.status = 'done'; job.progress = 100; job.result = r; })
- .catch(e => { job.status = 'error'; job.error = e.message || String(e); });
- return id;
- }
- function getJob(id) {
- const job = jobs.get(id);
- if (!job) return null;
- if (job.status === 'running') {
- const elapsed = Date.now() - job.startedAt;
- job.progress = Math.min(95, Math.floor((elapsed / 30000) * 100));
- }
- return job;
- }
- function serveStatic(req, res, filePath, contentType) {
- fs.readFile(filePath, (err, data) => {
- if (err) {
- res.writeHead(404);
- res.end('not found');
- return;
- }
- res.writeHead(200, { 'Content-Type': contentType });
- res.end(data);
- });
- }
- async function handleUpload(req) {
- const body = await readBody(req);
- const base64 = String(body.data || '');
- const name = String(body.name || `upload-${Date.now()}`).replace(/[\\/]/g, '_');
- if (!base64) throw new Error('缺少文件数据');
- ensureTmpDir();
- const buffer = Buffer.from(base64, 'base64');
- const filePath = path.join(TMP_DIR, name);
- fs.writeFileSync(filePath, buffer);
- return { path: filePath };
- }
- function handleOutputsDownload(req, res, query) {
- const requested = String(query.path || '');
- if (!requested) {
- json(res, 400, { status: 'error', message: '缺少 path 参数' });
- return;
- }
- const resolved = path.resolve(requested);
- const root = path.resolve(outputsRoot());
- if (!resolved.startsWith(root)) {
- json(res, 403, { status: 'error', message: '禁止访问 outputs 目录之外的文件' });
- return;
- }
- if (!fs.existsSync(resolved)) {
- json(res, 404, { status: 'error', message: '文件不存在' });
- return;
- }
- const data = fs.readFileSync(resolved);
- const ext = path.extname(resolved).toLowerCase();
- const contentType = {
- '.json': 'application/json',
- '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- '.xls': 'application/vnd.ms-excel',
- '.png': 'image/png',
- '.jpg': 'image/jpeg',
- '.jpeg': 'image/jpeg',
- '.mp3': 'audio/mpeg',
- '.mp4': 'video/mp4'
- }[ext] || 'application/octet-stream';
- res.writeHead(200, {
- 'Content-Type': contentType,
- 'Content-Disposition': `attachment; filename="${path.basename(resolved)}"`
- });
- res.end(data);
- }
- function getContentType(filePath) {
- const ext = path.extname(filePath).toLowerCase();
- const map = {
- '.html': 'text/html; charset=utf-8',
- '.css': 'text/css; charset=utf-8',
- '.js': 'application/javascript; charset=utf-8',
- '.png': 'image/png',
- '.jpg': 'image/jpeg',
- '.jpeg': 'image/jpeg',
- '.svg': 'image/svg+xml'
- };
- return map[ext] || 'application/octet-stream';
- }
- async function combinedStatus() {
- const [login, subscription] = await Promise.all([
- accountConnectionMonitor.getStatus(),
- qiweiSubscriptionStatus({})
- ]);
- return {
- status: 'ok',
- summary: {
- authConfigured: login.status !== 'needs_auth',
- online: !!login.summary?.online,
- subscribed: !!subscription.summary?.subscribed
- },
- data: { login, subscription }
- };
- }
- async function handleRequest(req, res) {
- const url = new URL(req.url, 'http://localhost');
- const pathname = url.pathname;
- try {
- if (pathname === '/' || pathname === '/index.html') {
- serveStatic(req, res, path.join(STATIC_DIR, 'index.html'), 'text/html; charset=utf-8');
- return;
- }
- if (pathname.startsWith('/dashboard/')) {
- const fileName = pathname.slice('/dashboard/'.length).replace(/\.{2,}/g, '');
- const filePath = path.join(STATIC_DIR, fileName);
- if (!filePath.startsWith(STATIC_DIR)) {
- json(res, 403, { status: 'error', message: '禁止访问' });
- return;
- }
- serveStatic(req, res, filePath, getContentType(filePath));
- return;
- }
- if (pathname === '/api/health') {
- json(res, 200, { status: 'ok' });
- return;
- }
- if (pathname === '/api/status' && req.method === 'GET') {
- json(res, 200, await combinedStatus());
- return;
- }
- if (pathname === '/api/skills' && req.method === 'GET') {
- json(res, 200, listSkillRegistry());
- return;
- }
- if (pathname === '/api/knowledge/tree' && req.method === 'GET') {
- json(res, 200, listKnowledgeTree());
- return;
- }
- if (pathname === '/api/knowledge/file' && req.method === 'GET') {
- json(res, 200, readKnowledgeFile(url.searchParams.get('id')));
- return;
- }
- if (pathname === '/api/knowledge/meetings' && req.method === 'GET') {
- json(res, 200, await getMeetingKnowledgeHub());
- return;
- }
- if (pathname === '/api/knowledge/meetings/sync' && req.method === 'POST') {
- json(res, 200, await syncMeetingKnowledge(await readBody(req)));
- return;
- }
- const meetingAnalyzeRoute = pathname.match(/^\/api\/knowledge\/meetings\/([^/]+)\/analyze$/);
- if (meetingAnalyzeRoute && req.method === 'POST') {
- json(res, 200, await analyzeMeetingKnowledge(decodeURIComponent(meetingAnalyzeRoute[1])));
- return;
- }
- const meetingDetailRoute = pathname.match(/^\/api\/knowledge\/meetings\/([^/]+)$/);
- if (meetingDetailRoute && req.method === 'GET') {
- json(res, 200, getMeetingKnowledge(decodeURIComponent(meetingDetailRoute[1])));
- return;
- }
- if (pathname === '/api/knowledge/docs' && req.method === 'GET') {
- json(res, 200, await getDocumentKnowledgeHub());
- return;
- }
- if (pathname === '/api/knowledge/docs/import' && req.method === 'POST') {
- json(res, 200, await importDocumentKnowledge(await readBody(req)));
- return;
- }
- if (pathname === '/api/knowledge/docs/create' && req.method === 'POST') {
- json(res, 200, await createDocumentKnowledge(await readBody(req)));
- return;
- }
- const documentAnalyzeRoute = pathname.match(/^\/api\/knowledge\/docs\/([^/]+)\/analyze$/);
- if (documentAnalyzeRoute && req.method === 'POST') {
- json(res, 200, await analyzeDocumentKnowledge(decodeURIComponent(documentAnalyzeRoute[1])));
- return;
- }
- const documentRefreshRoute = pathname.match(/^\/api\/knowledge\/docs\/([^/]+)\/refresh$/);
- if (documentRefreshRoute && req.method === 'POST') {
- json(res, 200, await refreshDocumentKnowledge(decodeURIComponent(documentRefreshRoute[1])));
- return;
- }
- const documentDetailRoute = pathname.match(/^\/api\/knowledge\/docs\/([^/]+)$/);
- if (documentDetailRoute && req.method === 'GET') {
- json(res, 200, getDocumentKnowledge(decodeURIComponent(documentDetailRoute[1])));
- return;
- }
- if (pathname === '/api/knowledge/todos' && req.method === 'GET') {
- json(res, 200, await getTodoKnowledgeHub());
- return;
- }
- if (pathname === '/api/knowledge/tasks' && req.method === 'GET') {
- json(res, 200, getUnifiedTaskHub());
- return;
- }
- if (pathname === '/api/knowledge/tasks/create' && req.method === 'POST') {
- json(res, 200, createUnifiedLocalTask(await readBody(req)));
- return;
- }
- if (pathname === '/api/knowledge/tasks/update' && req.method === 'POST') {
- json(res, 200, await updateUnifiedTask(await readBody(req)));
- return;
- }
- if (pathname === '/api/knowledge/todos/search-users' && req.method === 'POST') {
- json(res, 200, await searchTodoUsers(await readBody(req)));
- return;
- }
- if (pathname === '/api/knowledge/todos/sync' && req.method === 'POST') {
- json(res, 200, await syncTodoKnowledge(await readBody(req)));
- return;
- }
- if (pathname === '/api/knowledge/todos/details' && req.method === 'POST') {
- json(res, 200, await refreshTodoKnowledgeDetails(await readBody(req)));
- return;
- }
- if (pathname === '/api/knowledge/todos/create' && req.method === 'POST') {
- json(res, 200, await createTodoKnowledge(await readBody(req)));
- return;
- }
- const todoCompleteRoute = pathname.match(/^\/api\/knowledge\/todos\/([^/]+)\/complete$/);
- if (todoCompleteRoute && req.method === 'POST') {
- json(res, 200, await completeTodoKnowledge(decodeURIComponent(todoCompleteRoute[1])));
- return;
- }
- if (pathname === '/api/knowledge/properties' && req.method === 'GET') {
- json(res, 200, listProperties(Object.fromEntries(url.searchParams.entries())));
- return;
- }
- const propertyRoute = pathname.match(/^\/api\/knowledge\/properties\/([^/]+)$/);
- if (propertyRoute && req.method === 'GET') {
- json(res, 200, getProperty(decodeURIComponent(propertyRoute[1])));
- return;
- }
- const skillRoute = pathname.match(/^\/api\/skills\/(.+)$/);
- if (skillRoute && req.method === 'GET') {
- json(res, 200, getSkillDetail(decodeURIComponent(skillRoute[1])));
- return;
- }
- if (pathname === '/api/agent/status' && req.method === 'GET') {
- json(res, 200, await getAgentStatus());
- return;
- }
- if (pathname === '/api/accounts/switch' && req.method === 'POST') {
- json(res, 200, await switchActiveAccount(await readBody(req)));
- return;
- }
- if (pathname === '/api/agent/conversations' && req.method === 'GET') {
- json(res, 200, getConversations());
- return;
- }
- if (pathname === '/api/agent/conversations/sync' && req.method === 'POST') {
- json(res, 200, await syncConversations());
- return;
- }
- if (pathname === '/api/agent/audit' && req.method === 'GET') {
- json(res, 200, getAudit(url.searchParams.get('limit')));
- return;
- }
- if (pathname === '/api/agent/mode' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, changeGlobalMode(body.mode));
- return;
- }
- if (pathname === '/api/agent/listener/start' && req.method === 'POST') {
- json(res, 200, await startListener());
- return;
- }
- if (pathname === '/api/agent/listener/stop' && req.method === 'POST') {
- json(res, 200, stopListener());
- return;
- }
- const customerTaskSyncRoute = pathname.match(/^\/api\/agent\/tasks\/([^/]+)\/sync-official$/);
- if (customerTaskSyncRoute && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await syncCustomerTaskToOfficialTodo(customerTaskSyncRoute[1], body));
- return;
- }
- const customerTaskRoute = pathname.match(/^\/api\/agent\/tasks\/([^/]+)$/);
- if (customerTaskRoute && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await updateCustomerTask(customerTaskRoute[1], body));
- return;
- }
- const customerAlertRoute = pathname.match(/^\/api\/agent\/alerts\/([^/]+)$/);
- if (customerAlertRoute && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, updateCustomerAlert(customerAlertRoute[1], body));
- return;
- }
- const agentConversationRoute = pathname.match(/^\/api\/agent\/conversations\/([^/]+)\/(takeover|resume|pause|auto|approve-reply|generate|manual-send)$/);
- if (agentConversationRoute && req.method === 'POST') {
- const [, conversationId, action] = agentConversationRoute;
- const body = await readBody(req);
- if (action === 'takeover') json(res, 200, changeConversationMode(conversationId, 'human'));
- else if (action === 'resume') json(res, 200, changeConversationMode(conversationId, 'review'));
- else if (action === 'pause') json(res, 200, changeConversationMode(conversationId, 'paused'));
- else if (action === 'auto') json(res, 200, changeConversationMode(conversationId, 'auto'));
- else if (action === 'generate') json(res, 200, await generateLatestDraft(conversationId));
- else if (action === 'manual-send') json(res, 200, await manualSend(conversationId, body.content));
- else json(res, 200, await approveReply(conversationId, body.content));
- return;
- }
- const agentDraftRoute = pathname.match(/^\/api\/agent\/drafts\/([^/]+)\/(approve|reject|regenerate)$/);
- if (agentDraftRoute && req.method === 'POST') {
- const [, draftId, action] = agentDraftRoute;
- const body = await readBody(req);
- if (action === 'approve') json(res, 200, await approveDraft(draftId, body.content));
- else if (action === 'reject') json(res, 200, rejectDraft(draftId, body.reason));
- else json(res, 200, await regenerateDraft(draftId));
- return;
- }
- if (pathname === '/api/dashboard/summary' && req.method === 'GET') {
- json(res, 200, { status: 'ok', data: getStateSection('summary') });
- return;
- }
- if (pathname === '/api/dashboard/customers' && req.method === 'GET') {
- const filter = {
- keyword: url.searchParams.get('keyword') || '',
- hasPortrait: url.searchParams.has('hasPortrait') ? url.searchParams.get('hasPortrait') === 'true' : undefined,
- friendRequestStatus: url.searchParams.get('friendRequestStatus') || '',
- tag: url.searchParams.get('tag') || ''
- };
- json(res, 200, { status: 'ok', data: getStateSection('customers', { filter }) });
- return;
- }
- if (pathname === '/api/dashboard/portraits' && req.method === 'GET') {
- json(res, 200, { status: 'ok', data: getStateSection('portraits') });
- return;
- }
- if (pathname === '/api/dashboard/tags' && req.method === 'GET') {
- json(res, 200, { status: 'ok', data: getStateSection('tags') });
- return;
- }
- if (pathname === '/api/dashboard/transfers' && req.method === 'GET') {
- json(res, 200, { status: 'ok', data: getStateSection('transfers', { limit: url.searchParams.get('limit') }) });
- return;
- }
- if (pathname === '/api/dashboard/groups' && req.method === 'GET') {
- json(res, 200, { status: 'ok', data: getStateSection('groups') });
- return;
- }
- if (pathname === '/api/dashboard/operations' && req.method === 'GET') {
- json(res, 200, { status: 'ok', data: getStateSection('operations', { limit: url.searchParams.get('limit') }) });
- return;
- }
- if (pathname === '/api/dashboard/rebuild' && req.method === 'POST') {
- const state = rebuildStateFromOutputs();
- json(res, 200, { status: 'ok', data: { message: '索引已重建', summary: getStateSection('summary'), updatedAt: state.updatedAt } });
- return;
- }
- if (pathname === '/api/login/start' && req.method === 'POST') {
- const body = await readBody(req);
- const result = await qiweiLoginStart({
- flowUi: false,
- openBrowser: false,
- ...body
- });
- json(res, 200, result);
- return;
- }
- if (pathname === '/api/login/check' && req.method === 'POST') {
- const body = await readBody(req);
- const result = await qiweiLoginCheck(body);
- json(res, 200, result);
- return;
- }
- if (pathname === '/api/login/verify' && req.method === 'POST') {
- const body = await readBody(req);
- const result = await qiweiLoginVerify(body);
- json(res, 200, result);
- return;
- }
- if (pathname === '/api/groups/sync' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiSyncExternalGroups(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/groups/list' && req.method === 'GET') {
- const params = {};
- if (url.searchParams.has('keyword')) params.keyword = url.searchParams.get('keyword');
- if (url.searchParams.has('status')) params.status = url.searchParams.get('status');
- if (url.searchParams.has('source')) params.source = url.searchParams.get('source');
- if (url.searchParams.has('includeRejected')) params.includeRejected = url.searchParams.get('includeRejected') === 'true';
- json(res, 200, await qiweiListExternalGroups(params));
- return;
- }
- if (pathname === '/api/groups/analyze' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiAnalyzeGroupMembers(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/groups/confirm' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiConfirmExternalGroup(body));
- return;
- }
- if (pathname === '/api/groups/add' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiAddExternalGroup(body));
- return;
- }
- if (pathname === '/api/groups/reject' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiRejectExternalGroup(body));
- return;
- }
- if (pathname === '/api/groups/keywords' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiConfigureGroupKeywords(body));
- return;
- }
- if (pathname === '/api/groups/messages/sync' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiSyncGroupMessages(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- // Customer Operations
- if (pathname === '/api/customers' && req.method === 'GET') {
- json(res, 200, getCustomerMasterHub());
- return;
- }
- const customerMasterRoute = pathname.match(/^\/api\/customers\/([^/]+)\/profile$/);
- if (customerMasterRoute && req.method === 'POST') {
- json(res, 200, updateCustomerMaster(decodeURIComponent(customerMasterRoute[1]), await readBody(req)));
- return;
- }
- const customerRecommendationRoute = pathname.match(/^\/api\/customers\/([^/]+)\/recommendations\/([^/]+)$/);
- if (customerRecommendationRoute && req.method === 'POST') {
- json(res, 200, updateCustomerRecommendationFeedback(decodeURIComponent(customerRecommendationRoute[1]), decodeURIComponent(customerRecommendationRoute[2]), await readBody(req)));
- return;
- }
- if (pathname === '/api/customer-ops/batch-add-friends' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiBatchAddFriends(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/customer-ops/check-friend-status' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiCheckFriendStatus(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/customer-ops/customer-profile' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiGetCustomerProfile(body));
- return;
- }
- if (pathname === '/api/customer-ops/update' && req.method === 'POST') {
- json(res, 200, await qiweiUpdateCustomerInfo(await readBody(req)));
- return;
- }
- if (pathname === '/api/customer-ops/auto-create-group' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiAutoCreateGroup(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- // Portraits & Tags
- if (pathname === '/api/portraits/update' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiUpdateCustomerPortrait(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/portraits/save' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiSaveCustomerPortrait(body));
- return;
- }
- if (pathname === '/api/portraits/batch' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiBatchUpdateCustomerPortrait(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/portraits/export' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiExportCustomerPortraits(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/tags/list' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiListCustomerTags(body));
- return;
- }
- if (pathname === '/api/tags/all' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiListAllTags(body));
- return;
- }
- if (pathname === '/api/tags/add' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiAddCustomerTags(body));
- return;
- }
- if (pathname === '/api/tags/remove' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiRemoveCustomerTags(body));
- return;
- }
- if (pathname === '/api/personal-labels/sync' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiSyncPersonalLabels(body));
- return;
- }
- if (pathname === '/api/personal-labels/create' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiCreatePersonalLabel(body));
- return;
- }
- if (pathname === '/api/personal-labels/update' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiUpdatePersonalLabel(body));
- return;
- }
- if (pathname === '/api/personal-labels/delete' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiDeletePersonalLabel(body));
- return;
- }
- if (pathname === '/api/personal-labels/apply' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiApplyPersonalLabels(body));
- return;
- }
- // Customer Transfers
- if (pathname === '/api/transfers/preview' && req.method === 'POST') {
- const body = await readBody(req);
- json(res, 200, await qiweiPreviewTransferPackage(body));
- return;
- }
- if (pathname === '/api/transfers/execute' && req.method === 'POST') {
- const body = await readBody(req);
- const id = createJob(() => qiweiExecuteTransfer(body));
- json(res, 200, { status: 'ok', data: { jobId: id } });
- return;
- }
- if (pathname === '/api/upload' && req.method === 'POST') {
- const result = await handleUpload(req);
- json(res, 200, { status: 'ok', data: result });
- return;
- }
- if (pathname === '/api/outputs' && req.method === 'GET') {
- handleOutputsDownload(req, res, url.searchParams);
- return;
- }
- if (pathname.startsWith('/api/jobs/') && req.method === 'GET') {
- const id = pathname.slice('/api/jobs/'.length);
- const job = getJob(id);
- if (!job) {
- json(res, 404, { status: 'error', message: '任务不存在' });
- return;
- }
- json(res, 200, { status: 'ok', data: job });
- return;
- }
- json(res, 404, { status: 'error', message: 'not found' });
- } catch (error) {
- json(res, 500, { status: 'error', message: error.message || String(error) });
- }
- }
- function startServer(port = DASHBOARD_PORT) {
- const server = http.createServer(handleRequest);
- return new Promise((resolve, reject) => {
- server.once('error', reject);
- server.listen(port, '127.0.0.1', () => {
- console.log(`Qiwei Dashboard 已启动:http://127.0.0.1:${port}/`);
- resolve({ server, url: `http://127.0.0.1:${port}/`, port });
- });
- });
- }
- if (require.main === module) {
- startServer().catch(error => {
- console.error('Dashboard 启动失败:', error);
- process.exit(1);
- });
- }
- module.exports = { startServer };
|