import axios from 'axios'; import { i18n } from './locales'; const instance = axios.create({ baseURL: '/api', timeout: 20000 }); instance.interceptors.request.use((config) => { config.headers = config.headers ?? {}; config.headers['Accept-Language'] = String(i18n.global.locale.value || 'zh-CN'); return config; }); instance.interceptors.response.use( (res) => { const body = res.data; if (body && typeof body === 'object' && 'code' in body) { return body.code === 0 ? body.data : Promise.reject(body); } return body; }, (err) => Promise.reject(err), ); export function fetchTrace(shortCode: string): Promise { return instance.get(`/public/trace/${shortCode}`) as unknown as Promise; } export function fetchTraceOutboundImages( shortCode: string, page: number, pageSize = 20, preview?: { token?: string; state?: 'inactive' | 'active' }, ): Promise { return instance.get(`/public/trace/${shortCode}/outbound-images`, { params: { page, pageSize, ...preview }, }) as unknown as Promise; } // 后台预览:凭时间令牌访问,零副作用(不激活/不计数/不写日志);state 控制未激活/已激活形态 export function fetchTracePreview(shortCode: string, token: string, state: 'inactive' | 'active'): Promise { return instance.get(`/public/trace/${shortCode}/preview`, { params: { token, state } }) as unknown as Promise; } export function fetchAppInfo(): Promise { return instance.get('/settings/public') as unknown as Promise; } export interface TraceCertificateRecord { text: string; digest: string; txHash: string; author: string; createdAt: string | null; environment?: 'test' | 'production'; } export interface ChainDetailLog { address: string; name: string; data: string; topics: string[]; } export interface ChainDetail { gasUsed: string; fromAddress: string; toAddress: string; timeStamp: string | null; blockHeight: string | null; method: string; methodId: string; inputs: Array<{ name: string; type: string; data: string }>; logs: ChainDetailLog[]; action: { fromAddress: string; toAddress: string; contractAddress: string; tokenId: string } | null; } export function fetchChainDetail(shortCode: string): Promise<{ complete: boolean; detail: ChainDetail | null }> { return instance.post(`/public/trace/${encodeURIComponent(shortCode)}/chain-detail`, {}, { timeout: 30_000 }) as unknown as Promise<{ complete: boolean; detail: ChainDetail | null }>; } export interface TraceCertificateResult { status: 'NONE' | 'PENDING' | 'SUBMITTING' | 'CHECKING' | 'WAITING' | 'READY' | 'UNKNOWN' | 'FAILED'; generatedAt: string | null; certificate: TraceCertificateRecord | null; } export function requestTraceCertificate(shortCode: string): Promise { return instance.post(`/public/trace/${encodeURIComponent(shortCode)}/certificate`, {}, { timeout: 90_000 }) as unknown as Promise; } export function fetchTraceCertificate(shortCode: string): Promise { return instance.get(`/public/trace/${encodeURIComponent(shortCode)}/certificate`) as unknown as Promise; }