import axios from 'axios' const TOKEN_KEY = 'token' const http = axios.create({ baseURL: '/api/v1', timeout: 30000 }) http.interceptors.request.use(config => { const token = localStorage.getItem(TOKEN_KEY) if (token) config.headers.Authorization = `Bearer ${token}` return config }) http.interceptors.response.use( res => res.data, err => { if (err.response?.status === 401) { localStorage.removeItem(TOKEN_KEY) const path = window.location.pathname.replace('/workspace', '') || '/' window.location.href = '/workspace/login?redirect=' + encodeURIComponent(path) } return Promise.reject(err.response?.data || err) } ) export function login(data) { return http.post('/auth/login', data) } export function register(data) { return http.post('/auth/register', data) } export function getUserInfo() { return http.get('/auth/me') } export function updateProfile(data) { return http.put('/auth/me', data) } export function changePassword(data) { return http.put('/auth/password', data) } export function translate(data) { return http.post('/translate', data) } export function translateReply(data) { return http.post('/translate/reply', data) } export function extractInfo(data) { return http.post('/translate/extract', data) } export function translateFeedback(data) { return http.post('/translate/feedback', data) } export function sendSuggestion(data) { return http.post('/interaction/select', data) } export function listCustomers(params) { return http.get('/customers', { params }) } export function getCustomer(id) { return http.get(`/customers/${id}`) } export function createCustomer(data) { return http.post('/customers', data) } export function updateCustomer(id, data) { return http.patch(`/customers/${id}`, data) } export function deleteCustomer(id) { return http.delete(`/customers/${id}`) } export function getSilentCustomers(days = 3) { return http.get('/customers/silent', { params: { days } }) } export function getCustomerConversation(id) { return http.get(`/customers/${id}/conversation`) } export function exportCustomersCsv() { return http.get('/customers/export/csv') } export function exportCustomersXlsx() { return http.get('/customers/export/xlsx') } export function importCustomers(data) { return http.post('/customers/import', data) } export function getHealthOverview() { return http.get('/customers/health-overview') } export function getHealthScores() { return http.get('/customers/health-scores') } export function getCustomerHealth(id) { return http.get(`/customers/${id}/health`) } export function listProducts(params) { return http.get('/products', { params }) } export function createProduct(data) { return http.post('/products', data) } export function updateProduct(id, data) { return http.patch(`/products/${id}`, data) } export function deleteProduct(id) { return http.delete(`/products/${id}`) } export function exportProductsCsv() { return http.get('/products/export/csv') } export function exportProductsXlsx() { return http.get('/products/export/xlsx') } export function importProducts(data) { return http.post('/products/import', data) } export function listQuotations(params) { return http.get('/quotations', { params }) } export function getQuotation(id) { return http.get(`/quotations/${id}`) } export function createQuotation(data) { return http.post('/quotations', data) } export function updateQuotationStatus(id, status) { return http.patch(`/quotations/${id}/status`, { status }) } export function exportQuotationPdf(id) { return http.get(`/quotations/${id}/pdf`) } export function exportQuotationsCsv() { return http.get('/quotations/export/csv') } export function exportQuotationsXlsx() { return http.get('/quotations/export/xlsx') } export function generateQuoteFromInquiry(data) { return http.post('/quotations/generate-from-inquiry', data) } export function importQuotations(data) { return http.post('/quotations/import', data) } export function generateMarketing(data) { return http.post('/marketing/generate', data) } export function getKeywords(data) { return http.post('/marketing/keywords', data) } export function competitorAnalysis(data) { return http.post('/marketing/competitor-analysis', data) } export function discoverySearch(data) { return http.post('/discovery/search', data, { timeout: 120000 }) } export function discoveryAnalyze(data) { return http.post('/discovery/analyze', data, { timeout: 60000 }) } export function discoveryOutreach(data) { return http.post('/discovery/outreach', data, { timeout: 60000 }) } export function saveDiscoveryRecord(data) { return http.post('/discovery/records', data) } export function listDiscoveryRecords(params) { return http.get('/discovery/records', { params }) } export function getDiscoveryRecord(id) { return http.get(`/discovery/records/${id}`) } export function deleteDiscoveryRecord(id) { return http.delete(`/discovery/records/${id}`) } export function getFollowupStats() { return http.get('/followup/stats') } export function getFollowupPending() { return http.get('/followup/pending') } export function getFollowupLogs(params) { return http.get('/followup/logs', { params }) } export function markFollowupSent(id) { return http.post(`/followup/${id}/send`) } export function editFollowup(id, data) { return http.post(`/followup/${id}/edit`, data) } export function scanFollowups() { return http.post('/followup/scan') } export function getAnalyticsOverview() { return http.get('/analytics/overview') } export function listTeams() { return http.get('/teams') } export function createTeam(data) { return http.post('/teams', data) } export function inviteTeamMember(id, userId) { return http.post(`/teams/${id}/invite`, { user_id: userId }) } export function deleteTeamMember(id, memberId) { return http.delete(`/teams/${id}/members/${memberId}`) } export function leaveTeam(id) { return http.post(`/teams/${id}/leave`) } export function updateTeamMemberRole(id, memberId, role) { return http.patch(`/teams/${id}/members/${memberId}/role`, { role }) } export function listNotifications(params) { return http.get('/notifications', { params }) } export function getUnreadCount() { return http.get('/notifications/unread-count') } export function markNotificationRead(id) { return http.patch(`/notifications/${id}/read`) } export function markAllRead() { return http.post('/notifications/read-all') } export function getPlans() { return http.get('/payment/plans') } export function getSubscription() { return http.get('/payment/subscription') } export function createOrder(plan, payType = 'alipay') { return http.post('/payment/create-order', { plan, pay_type: payType }) } export function submitCertification(data) { return http.post('/certification/submit', data) } export function getCertificationStatus() { return http.get('/certification/status') } export function applyInvoice(data) { return http.post('/invoices/apply', data) } export function listInvoices(params) { return http.get('/invoices/list', { params }) } export function submitFeedback(data) { return http.post('/feedback', data) } export function sendWhatsApp(data) { return http.post('/whatsapp/send', data) } export function getUsageStats() { return http.get('/usage/stats') } export function aiChat(message, history = []) { return http.post('/ai/chat', { message, history }) } export function aiQuickQuestions() { return http.get('/ai/quick-questions') } export function getCreditBalance() { return http.get('/credits/balance') } export function getCreditHistory(params) { return http.get('/credits/history', { params }) } export function getCreditPackages() { return http.get('/credits/packages') } export function getSubscriptionPlans() { return http.get('/credits/subscription-plans') } export function purchaseCreditPackage(packageId, payType = 'alipay') { return http.post('/credits/purchase', { package_id: packageId, pay_type: payType }) } export function subscribeCreditPlan(planId, payType = 'alipay') { return http.post('/credits/subscribe', { plan_id: planId, pay_type: payType }) } export function cancelCreditSubscription() { return http.post('/credits/cancel-subscription') } export function startAgentPipeline(data) { return http.post('/agent/start', data, { timeout: 300000 }) } export function listAgentPipelines(params) { return http.get('/agent/pipelines', { params }) } export function getAgentPipeline(id) { return http.get(`/agent/${id}`) } export default http