/** * 全局 API 包装器 * 提供 editor-core.js 所需的所有 API 方法 */ (function() { 'use strict'; const BASE = window.location.origin; // e.g. http://localhost:3000 function getHeaders() { const token = localStorage.getItem('ycat_token'); return { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + (token || '') }; } async function apiFetch(path, options = {}) { // 统一处理:path 可能是 /api/xxx 或 http://xxx let url; if (path.startsWith('http')) { url = path; } else { url = BASE + path; // /api/xxx -> http://localhost:3000/api/xxx } const res = await fetch(url, { ...options, headers: { ...getHeaders(), ...(options.headers || {}) } }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'API error ' + res.status); return data; } function get(path) { return apiFetch(path, { method: 'GET' }); } function post(path, body) { return apiFetch(path, { method: 'POST', body: JSON.stringify(body) }); } // === OAuth 授权 === // platform: wechat|zhihu|xiaohongshu|bilibili|douyin function oauthUrl(platform) { return get('/api/publish/platforms/oauth-url?platform=' + encodeURIComponent(platform)); } function oauthPoll(state) { return get('/api/publish/platforms/oauth-poll?state=' + encodeURIComponent(state)); } function authorizeOAuth(platform, code) { return post('/api/publish/platforms/authorize', { platform: platform, code: code }); } // === 文章 === function getArticles(params) { return get('/articles?' + new URLSearchParams(params||{})); } function getArticle(id) { return get('/articles/' + id); } function createArticle(data) { return post('/articles', data); } function updateArticle(id, data) { return post('/articles/' + id, data); } function deleteArticle(id) { return post('/articles/' + id + '/delete', {}); } function getDraft(id) { return get('/articles/' + id + '/draft'); } function saveDraft(id, data) { return post('/articles/' + id + '/draft', data); } function exportMarkdown(id) { return get('/articles/' + id + '/export'); } function listCategories() { return get('/categories'); } function listTags() { return get('/tags'); } function getStats() { return get('/stats'); } // === 平台账号 === function getPlatforms() { return get('/publish/platforms'); } function addPlatformAccount(platform, accountName) { return post('/publish/accounts/add', { platform: platform, accountName: accountName, authorize: 1 }); } function removePlatformAccount(accountId) { return post('/publish/accounts/remove', { accountId: accountId }); } // === 发布 === function publish(articleId, platforms) { return post('/api/publish', { articleId: articleId, platforms: platforms }); } function schedulePublish(articleId, platforms, publishAt) { return post('/api/scheduled/publish', { articleId: articleId, platforms: platforms, publishAt: publishAt }); } // === AI 工具 === function aiSuggestTitle(content) { return post('/api/ai/suggest-title', { content }); } function aiSuggestTags(content) { return post('/api/ai/suggest-tags', { content }); } function aiAnalyzeContent(content) { return post('/api/ai/analyze', { content }); } function aiRewrite(content, style) { return post('/api/ai/rewrite', { content, style }); } function aiPolish(content) { return post('/api/ai/polish', { content }); } function aiExpand(content) { return post('/api/ai/expand', { content }); } function aiSummarize(content) { return post('/api/ai/summarize', { content }); } function aiSuggestCategories(content) { return post('/api/ai/suggest-categories', { content }); } function seoScore(content) { return post('/api/ai/seo-score', { content }); } function healthCheck(content) { return post('/api/ai/health-check', { content }); } function batchSuggest(content, platforms) { return post('/api/ai/batch-suggest', { content, platforms }); } // === 合规 === function complianceCheck(content) { return post('/api/compliance/check', { content }); } window.API = { get, post, publish, schedulePublish, oauthUrl, oauthPoll, authorizeOAuth, getArticles, getArticle, createArticle, updateArticle, deleteArticle, getDraft, saveDraft, exportMarkdown, listCategories, listTags, getStats, getPlatforms, addPlatformAccount, removePlatformAccount, aiSuggestTitle, aiSuggestTags, aiAnalyzeContent, aiRewrite, aiPolish, aiExpand, aiSummarize, aiSuggestCategories, seoScore, healthCheck, batchSuggest, complianceCheck }; console.log('[API] initialized, endpoints:', Object.keys(window.API).length); })();