style: 统一项目代码格式
This commit is contained in:
1 parent
f0836ec056
commit
718d357d88
8 files changed
+1012
-117
No files matched your search
@@ -23,11 +23,44 @@ export interface BrandProfile {
|
||||
contactWechat: string | null;
|
||||
}
|
||||
export type BrandUpdate = Partial<Omit<BrandProfile, "id">>;
|
||||
export interface ServiceUpdate { name?: string; description?: string; coverUrl?: string; priceText?: string; priceAmount?: number; sortOrder?: number; bookingEnabled?: boolean; isEnabled?: boolean; categoryKey?: string; config?: Record<string, unknown> }
|
||||
export interface TrustContent { id: string; type: string; title: string; summary?: string | null; content: string; coverUrl?: string | null; sortOrder: number; isPublished: boolean; config?: Record<string, unknown> | null }
|
||||
export interface TrustContentInput { type: string; title: string; summary?: string; content: string; coverUrl?: string; sortOrder?: number; isPublished?: boolean; config?: Record<string, unknown> }
|
||||
export interface ServiceUpdate {
|
||||
name?: string;
|
||||
description?: string;
|
||||
coverUrl?: string;
|
||||
priceText?: string;
|
||||
priceAmount?: number;
|
||||
sortOrder?: number;
|
||||
bookingEnabled?: boolean;
|
||||
isEnabled?: boolean;
|
||||
categoryKey?: string;
|
||||
config?: Record<string, unknown>;
|
||||
}
|
||||
export interface TrustContent {
|
||||
id: string;
|
||||
type: string;
|
||||
title: string;
|
||||
summary?: string | null;
|
||||
content: string;
|
||||
coverUrl?: string | null;
|
||||
sortOrder: number;
|
||||
isPublished: boolean;
|
||||
config?: Record<string, unknown> | null;
|
||||
}
|
||||
export interface TrustContentInput {
|
||||
type: string;
|
||||
title: string;
|
||||
summary?: string;
|
||||
content: string;
|
||||
coverUrl?: string;
|
||||
sortOrder?: number;
|
||||
isPublished?: boolean;
|
||||
config?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface BookingItemInput { serviceId: string; quantity: number }
|
||||
export interface BookingItemInput {
|
||||
serviceId: string;
|
||||
quantity: number;
|
||||
}
|
||||
export interface CreateBookingInput {
|
||||
requestType: BookingRequestType;
|
||||
customerName: string;
|
||||
@@ -43,24 +76,62 @@ export interface CreateBookingInput {
|
||||
extraData?: Record<string, unknown>;
|
||||
items: BookingItemInput[];
|
||||
}
|
||||
export interface BookingSummary { bookingNo: string; status: BookingStatus; estimatedAmount: number | null; createdAt: string }
|
||||
export interface Booking extends BookingSummary { id: string; customerName: string; adminNote?: string | null }
|
||||
export interface BookingTag { id: string; name: string; isPreset: boolean; isEnabled: boolean }
|
||||
export interface AdminBooking extends Booking { tags?: Array<{ tag: BookingTag }> }
|
||||
export interface ApiErrorBody { data: null; error: { code: string; message: string }; requestId: string | null }
|
||||
export interface BookingSummary {
|
||||
bookingNo: string;
|
||||
status: BookingStatus;
|
||||
estimatedAmount: number | null;
|
||||
createdAt: string;
|
||||
}
|
||||
export interface Booking extends BookingSummary {
|
||||
id: string;
|
||||
customerName: string;
|
||||
adminNote?: string | null;
|
||||
}
|
||||
export interface BookingTag {
|
||||
id: string;
|
||||
name: string;
|
||||
isPreset: boolean;
|
||||
isEnabled: boolean;
|
||||
}
|
||||
export interface AdminBooking extends Booking {
|
||||
tags?: Array<{ tag: BookingTag }>;
|
||||
}
|
||||
export interface ApiErrorBody {
|
||||
data: null;
|
||||
error: { code: string; message: string };
|
||||
requestId: string | null;
|
||||
}
|
||||
|
||||
export class ApiClientError extends Error {
|
||||
constructor(readonly status: number, readonly body: ApiErrorBody) {
|
||||
constructor(
|
||||
readonly status: number,
|
||||
readonly body: ApiErrorBody,
|
||||
) {
|
||||
super(body.error.message);
|
||||
this.name = "ApiClientError";
|
||||
}
|
||||
get code(): string { return this.body.error.code; }
|
||||
get requestId(): string | null { return this.body.requestId; }
|
||||
get code(): string {
|
||||
return this.body.error.code;
|
||||
}
|
||||
get requestId(): string | null {
|
||||
return this.body.requestId;
|
||||
}
|
||||
}
|
||||
|
||||
export interface FetchResponse { readonly status: number; readonly ok: boolean; json(): Promise<unknown> }
|
||||
export type FetchLike = (input: string, init?: { method?: string; headers?: Record<string, string>; body?: string }) => Promise<FetchResponse>;
|
||||
export interface ApiClientOptions { baseUrl: string; fetcher: FetchLike; token?: string }
|
||||
export interface FetchResponse {
|
||||
readonly status: number;
|
||||
readonly ok: boolean;
|
||||
json(): Promise<unknown>;
|
||||
}
|
||||
export type FetchLike = (
|
||||
input: string,
|
||||
init?: { method?: string; headers?: Record<string, string>; body?: string },
|
||||
) => Promise<FetchResponse>;
|
||||
export interface ApiClientOptions {
|
||||
baseUrl: string;
|
||||
fetcher: FetchLike;
|
||||
token?: string;
|
||||
}
|
||||
|
||||
export class ApiClient {
|
||||
private readonly baseUrl: string;
|
||||
@@ -72,13 +143,21 @@ export class ApiClient {
|
||||
this.fetcher = options.fetcher;
|
||||
this.token = options.token;
|
||||
}
|
||||
setToken(token: string | undefined): void { this.token = token; }
|
||||
getBrand(): Promise<BrandProfile> { return this.request("/public/brand"); }
|
||||
listServices(): Promise<PublicService[]> { return this.request("/public/services"); }
|
||||
setToken(token: string | undefined): void {
|
||||
this.token = token;
|
||||
}
|
||||
getBrand(): Promise<BrandProfile> {
|
||||
return this.request("/public/brand");
|
||||
}
|
||||
listServices(): Promise<PublicService[]> {
|
||||
return this.request("/public/services");
|
||||
}
|
||||
createCustomerSession(code: string): Promise<{ token: string }> {
|
||||
return this.request("/customer/session/wechat", { method: "POST", body: { code } });
|
||||
}
|
||||
listCustomerBookings(): Promise<Booking[]> { return this.request("/customer/bookings"); }
|
||||
listCustomerBookings(): Promise<Booking[]> {
|
||||
return this.request("/customer/bookings");
|
||||
}
|
||||
createCustomerBooking(input: CreateBookingInput): Promise<BookingSummary> {
|
||||
return this.request("/customer/bookings", { method: "POST", body: input });
|
||||
}
|
||||
@@ -88,7 +167,9 @@ export class ApiClient {
|
||||
loginAdmin(username: string, password: string): Promise<{ token: string; displayName: string }> {
|
||||
return this.request("/admin/session/login", { method: "POST", body: { username, password } });
|
||||
}
|
||||
listAdminBookings(filters: { status?: BookingStatus; unreadOnly?: boolean; tagId?: string } = {}): Promise<AdminBooking[]> {
|
||||
listAdminBookings(
|
||||
filters: { status?: BookingStatus; unreadOnly?: boolean; tagId?: string } = {},
|
||||
): Promise<AdminBooking[]> {
|
||||
const params: string[] = [];
|
||||
if (filters.status) params.push(`status=${encodeURIComponent(filters.status)}`);
|
||||
if (filters.unreadOnly !== undefined) params.push(`unreadOnly=${filters.unreadOnly}`);
|
||||
@@ -96,28 +177,72 @@ export class ApiClient {
|
||||
const query = params.join("&");
|
||||
return this.request(`/admin/bookings${query ? `?${query}` : ""}`);
|
||||
}
|
||||
getAdminBooking(id: string): Promise<AdminBooking> { return this.request(`/admin/bookings/${encodeURIComponent(id)}`); }
|
||||
getAdminUnreadCount(): Promise<{ count: number }> { return this.request("/admin/bookings/unread-count"); }
|
||||
updateAdminBookingStatus(id: string, status: BookingStatus, adminNote?: string): Promise<Booking> {
|
||||
return this.request(`/admin/bookings/${encodeURIComponent(id)}/status`, { method: "POST", body: { status, ...(adminNote === undefined ? {} : { adminNote }) } });
|
||||
getAdminBooking(id: string): Promise<AdminBooking> {
|
||||
return this.request(`/admin/bookings/${encodeURIComponent(id)}`);
|
||||
}
|
||||
getAdminUnreadCount(): Promise<{ count: number }> {
|
||||
return this.request("/admin/bookings/unread-count");
|
||||
}
|
||||
updateAdminBookingStatus(
|
||||
id: string,
|
||||
status: BookingStatus,
|
||||
adminNote?: string,
|
||||
): Promise<Booking> {
|
||||
return this.request(`/admin/bookings/${encodeURIComponent(id)}/status`, {
|
||||
method: "POST",
|
||||
body: { status, ...(adminNote === undefined ? {} : { adminNote }) },
|
||||
});
|
||||
}
|
||||
markAdminBookingRead(id: string): Promise<Booking> {
|
||||
return this.request(`/admin/bookings/${encodeURIComponent(id)}/read`, { method: "POST" });
|
||||
}
|
||||
listAdminTags(): Promise<BookingTag[]> { return this.request("/admin/bookings/tags"); }
|
||||
listAdminTags(): Promise<BookingTag[]> {
|
||||
return this.request("/admin/bookings/tags");
|
||||
}
|
||||
createAdminTag(name: string): Promise<BookingTag> {
|
||||
return this.request("/admin/bookings/tags", { method: "POST", body: { name } });
|
||||
}
|
||||
getAdminBrand(): Promise<BrandProfile> { return this.request("/admin/config/brand"); }
|
||||
updateAdminBrand(body: BrandUpdate): Promise<BrandProfile> { return this.request("/admin/config/brand", { method: "PATCH", body }); }
|
||||
updateAdminService(id: string, body: ServiceUpdate): Promise<PublicService> { return this.request(`/admin/config/services/${encodeURIComponent(id)}`, { method: "PATCH", body }); }
|
||||
listAdminServices(): Promise<PublicService[]> { return this.request("/admin/config/services"); }
|
||||
createAdminService(body: ServiceUpdate & { name: string; description: string }): Promise<PublicService> { return this.request("/admin/config/services", { method: "POST", body }); }
|
||||
listAdminTrustContent(): Promise<TrustContent[]> { return this.request("/admin/config/trust-content"); }
|
||||
createAdminTrustContent(body: TrustContentInput): Promise<TrustContent> { return this.request("/admin/config/trust-content", { method: "POST", body }); }
|
||||
updateAdminTrustContent(id: string, body: Partial<TrustContentInput>): Promise<TrustContent> { return this.request(`/admin/config/trust-content/${encodeURIComponent(id)}`, { method: "PATCH", body }); }
|
||||
deleteAdminTrustContent(id: string): Promise<{ deleted: boolean }> { return this.request(`/admin/config/trust-content/${encodeURIComponent(id)}`, { method: "DELETE" }); }
|
||||
private async request<T>(path: string, options: { method?: string; body?: unknown } = {}): Promise<T> {
|
||||
getAdminBrand(): Promise<BrandProfile> {
|
||||
return this.request("/admin/config/brand");
|
||||
}
|
||||
updateAdminBrand(body: BrandUpdate): Promise<BrandProfile> {
|
||||
return this.request("/admin/config/brand", { method: "PATCH", body });
|
||||
}
|
||||
updateAdminService(id: string, body: ServiceUpdate): Promise<PublicService> {
|
||||
return this.request(`/admin/config/services/${encodeURIComponent(id)}`, {
|
||||
method: "PATCH",
|
||||
body,
|
||||
});
|
||||
}
|
||||
listAdminServices(): Promise<PublicService[]> {
|
||||
return this.request("/admin/config/services");
|
||||
}
|
||||
createAdminService(
|
||||
body: ServiceUpdate & { name: string; description: string },
|
||||
): Promise<PublicService> {
|
||||
return this.request("/admin/config/services", { method: "POST", body });
|
||||
}
|
||||
listAdminTrustContent(): Promise<TrustContent[]> {
|
||||
return this.request("/admin/config/trust-content");
|
||||
}
|
||||
createAdminTrustContent(body: TrustContentInput): Promise<TrustContent> {
|
||||
return this.request("/admin/config/trust-content", { method: "POST", body });
|
||||
}
|
||||
updateAdminTrustContent(id: string, body: Partial<TrustContentInput>): Promise<TrustContent> {
|
||||
return this.request(`/admin/config/trust-content/${encodeURIComponent(id)}`, {
|
||||
method: "PATCH",
|
||||
body,
|
||||
});
|
||||
}
|
||||
deleteAdminTrustContent(id: string): Promise<{ deleted: boolean }> {
|
||||
return this.request(`/admin/config/trust-content/${encodeURIComponent(id)}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
}
|
||||
private async request<T>(
|
||||
path: string,
|
||||
options: { method?: string; body?: unknown } = {},
|
||||
): Promise<T> {
|
||||
const headers: Record<string, string> = { accept: "application/json" };
|
||||
if (options.body !== undefined) headers["content-type"] = "application/json";
|
||||
if (this.token) headers.authorization = `Bearer ${this.token}`;
|
||||
|
||||
Reference in new issue
Block a user