Files
cwd/cwd-admin/src/api/admin.ts
2026-01-21 10:53:11 +08:00

163 lines
3.9 KiB
TypeScript

import { get, post, put, del } from './http';
export type AdminLoginResponse = {
data: {
key: string;
};
};
export type CommentItem = {
id: number;
created: number;
name: string;
email: string;
avatar: string;
postSlug: string;
url: string | null;
ipAddress: string | null;
contentText: string;
contentHtml: string;
status: string;
ua?: string | null;
};
export type CommentListResponse = {
data: CommentItem[];
pagination: {
page: number;
limit: number;
total: number;
};
};
export type AdminEmailResponse = {
email: string | null;
};
export type CommentSettingsResponse = {
adminEmail: string | null;
adminBadge: string | null;
avatarPrefix: string | null;
adminEnabled: boolean;
allowedDomains?: string[];
adminKey?: string | null;
adminKeySet?: boolean;
requireReview?: boolean;
blockedIps?: string[];
blockedEmails?: string[];
};
export type EmailNotifySettingsResponse = {
globalEnabled: boolean;
smtp?: {
host: string;
port: number;
user: string;
pass: string;
secure: boolean;
};
templates?: {
reply?: string;
admin?: string;
};
};
export async function loginAdmin(name: string, password: string): Promise<string> {
const res = await post<AdminLoginResponse>('/admin/login', { name, password });
const key = res.data.key;
localStorage.setItem('cwd_admin_token', key);
return key;
}
export function logoutAdmin(): void {
localStorage.removeItem('cwd_admin_token');
}
export function fetchComments(page: number): Promise<CommentListResponse> {
return get<CommentListResponse>(`/admin/comments/list?page=${page}`);
}
export function deleteComment(id: number): Promise<{ message: string }> {
return del<{ message: string }>(`/admin/comments/delete?id=${id}`);
}
export function updateCommentStatus(id: number, status: string): Promise<{ message: string }> {
return put<{ message: string }>(`/admin/comments/status?id=${id}&status=${encodeURIComponent(status)}`);
}
export function fetchAdminEmail(): Promise<AdminEmailResponse> {
return get<AdminEmailResponse>('/admin/settings/email');
}
export function saveAdminEmail(email: string): Promise<{ message: string }> {
return put<{ message: string }>('/admin/settings/email', { email });
}
export function fetchEmailNotifySettings(): Promise<EmailNotifySettingsResponse> {
return get<EmailNotifySettingsResponse>('/admin/settings/email-notify');
}
export function saveEmailNotifySettings(data: {
globalEnabled?: boolean;
smtp?: {
host?: string;
port?: number;
user?: string;
pass?: string;
secure?: boolean;
};
templates?: {
reply?: string;
admin?: string;
};
}): Promise<{ message: string }> {
return put<{ message: string }>('/admin/settings/email-notify', data);
}
export function sendTestEmail(data: {
toEmail: string;
smtp?: {
host?: string;
port?: number;
user?: string;
pass?: string;
secure?: boolean;
};
}): Promise<{ message: string }> {
return post<{ message: string }>('/admin/settings/email-test', data);
}
export function fetchCommentSettings(): Promise<CommentSettingsResponse> {
return get<CommentSettingsResponse>('/admin/settings/comments');
}
export function saveCommentSettings(data: {
adminEmail?: string;
adminBadge?: string;
avatarPrefix?: string;
adminEnabled?: boolean;
allowedDomains?: string[];
adminKey?: string;
requireReview?: boolean;
blockedIps?: string[];
blockedEmails?: string[];
}): Promise<{ message: string }> {
return put<{ message: string }>('/admin/settings/comments', data);
}
export function blockIp(ip: string): Promise<{ message: string }> {
return post<{ message: string }>('/admin/comments/block-ip', { ip });
}
export function blockEmail(email: string): Promise<{ message: string }> {
return post<{ message: string }>('/admin/comments/block-email', { email });
}
export function exportComments(): Promise<any[]> {
return get<any[]>('/admin/comments/export');
}
export function importComments(data: any[]): Promise<{ message: string }> {
return post<{ message: string }>('/admin/comments/import', data);
}