feat: 增加后台品牌与服务配置能力

This commit is contained in:
que01 committed 2026-09-19 12:04:59 +08:00
1 parent 34ec893369
commit f485257e37
4 files changed
+61 -4

No files matched your search

+6 -2
View File
@@ -9,11 +9,13 @@ export class AdminDashboard {
async load(): Promise<void> {
const { root, client } = this.options;
const [bookings, unread] = await Promise.all([client.listAdminBookings(), client.getAdminUnreadCount()]);
const [bookings, unread, brand, services] = await Promise.all([client.listAdminBookings(), client.getAdminUnreadCount(), client.getAdminBrand(), client.listServices()]);
this.bookings = bookings;
root.innerHTML = `<h1>品牌后台</h1><p>未读订单:${unread.count}</p><label>状态 <select id="status"><option value="">全部</option><option>received</option><option>contacting</option><option>confirmed</option><option>completed</option><option>cancelled</option></select></label><button id="refresh">刷新</button><ul id="bookings">${this.listHtml()}</ul><section id="detail">选择订单查看详情</section>`;
root.innerHTML = `<h1>品牌后台</h1><p>未读订单:${unread.count}</p><section><h2>品牌配置</h2><label>品牌名 <input id="brand-name" value="${brand.name}"></label><label>品牌简介 <textarea id="brand-description">${brand.description ?? ""}</textarea></label><label>联系电话 <input id="brand-phone" value="${brand.contactPhone ?? ""}"></label><label>微信号 <input id="brand-wechat" value="${brand.contactWechat ?? ""}"></label><button id="save-brand">保存品牌配置</button></section><section><h2>服务配置</h2><ul id="services">${services.map((item) => `<li><input data-service-name="${item.id}" value="${item.name}"><input data-service-price="${item.id}" value="${item.priceText ?? ""}"><button data-service-save="${item.id}">保存</button></li>`).join("")}</ul></section><label>状态 <select id="status"><option value="">全部</option><option>received</option><option>contacting</option><option>confirmed</option><option>completed</option><option>cancelled</option></select></label><button id="refresh">刷新</button><ul id="bookings">${this.listHtml()}</ul><section id="detail">选择订单查看详情</section>`;
root.querySelector("#refresh")?.addEventListener("click", () => void this.load());
root.querySelector("#status")?.addEventListener("change", () => void this.filter());
root.querySelector("#save-brand")?.addEventListener("click", () => void this.saveBrand());
root.querySelectorAll<HTMLElement>("[data-service-save]").forEach((item) => item.addEventListener("click", () => void this.saveService(item.dataset.serviceSave)));
root.querySelectorAll<HTMLElement>("[data-id]").forEach((item) => item.addEventListener("click", () => this.select(item.dataset.id)));
}
@@ -21,6 +23,8 @@ export class AdminDashboard {
private async filter(): Promise<void> { const status = (this.options.root.querySelector("#status") as HTMLSelectElement).value as BookingStatus | ""; this.bookings = await this.options.client.listAdminBookings(status ? { status } : {}); const list = this.options.root.querySelector("#bookings"); if (list) list.innerHTML = this.listHtml(); }
private select(id: string | undefined): void { this.selected = this.bookings.find((item) => item.id === id); if (!this.selected) return; const detail = this.options.root.querySelector("#detail"); if (!detail) return; detail.innerHTML = `<h2>${this.selected.bookingNo}</h2><p>客户:${this.selected.customerName}</p><select id="detail-status"><option>received</option><option>contacting</option><option>confirmed</option><option>completed</option><option>cancelled</option></select><textarea id="note" placeholder="内部备注">${this.selected.adminNote ?? ""}</textarea><button id="save">保存</button>`; detail.querySelector("#save")?.addEventListener("click", () => void this.save()); }
private async save(): Promise<void> { if (!this.selected) return; const detail = this.options.root.querySelector("#detail")!; const status = (detail.querySelector("#detail-status") as HTMLSelectElement).value as BookingStatus; const note = (detail.querySelector("#note") as HTMLTextAreaElement).value; this.selected = await this.options.client.updateAdminBookingStatus(this.selected.id, status, note); await this.load(); }
private async saveBrand(): Promise<void> { const root = this.options.root; await this.options.client.updateAdminBrand({ name: (root.querySelector("#brand-name") as HTMLInputElement).value, description: (root.querySelector("#brand-description") as HTMLTextAreaElement).value, contactPhone: (root.querySelector("#brand-phone") as HTMLInputElement).value, contactWechat: (root.querySelector("#brand-wechat") as HTMLInputElement).value }); }
private async saveService(id: string | undefined): Promise<void> { if (!id) return; const root = this.options.root; await this.options.client.updateAdminService(id, { name: (root.querySelector(`[data-service-name="${id}"]`) as HTMLInputElement).value, priceText: (root.querySelector(`[data-service-price="${id}"]`) as HTMLInputElement).value }); }
}
export const adminAppName = "personal-brand-admin";
+25 -1
View File
@@ -1,4 +1,28 @@
import { IsArray, IsIn, IsOptional, IsString } from "class-validator";
import { IsArray, IsBoolean, IsIn, IsInt, IsOptional, IsString, Max, Min } from "class-validator";
export class UpdateBrandDto {
@IsOptional() @IsString() name?: string;
@IsOptional() @IsString() tagline?: string;
@IsOptional() @IsString() description?: string;
@IsOptional() @IsString() avatarUrl?: string;
@IsOptional() @IsString() coverUrl?: string;
@IsOptional() @IsString() hours?: string;
@IsOptional() @IsString() address?: string;
@IsOptional() @IsString() deliveryNote?: string;
@IsOptional() @IsString() contactPhone?: string;
@IsOptional() @IsString() contactWechat?: string;
}
export class UpdateServiceDto {
@IsOptional() @IsString() name?: string;
@IsOptional() @IsString() description?: string;
@IsOptional() @IsString() coverUrl?: string;
@IsOptional() @IsString() priceText?: string;
@IsOptional() @IsInt() @Min(0) priceAmount?: number;
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
@IsOptional() @IsBoolean() bookingEnabled?: boolean;
@IsOptional() @IsBoolean() isEnabled?: boolean;
}
export class UpdateBookingStatusDto {
@IsIn(["received", "contacting", "confirmed", "completed", "cancelled"])
+25 -1
View File
@@ -7,6 +7,7 @@ import {
Module,
Param,
Post,
Patch,
Query,
Req,
ValidationPipe,
@@ -19,7 +20,7 @@ import { PrismaService } from "./prisma.service.js";
import { WechatAuthService } from "./wechat-auth.js";
import { BookingService } from "./booking.service.js";
import { CreateBookingDto } from "./booking.dto.js";
import { CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto } from "./admin.dto.js";
import { CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto, UpdateBrandDto, UpdateServiceDto } from "./admin.dto.js";
import { AdminAuthService } from "./admin-auth.service.js";
import { AdminLoginDto } from "./admin-auth.dto.js";
import type { FastifyRequest } from "fastify";
@@ -279,6 +280,28 @@ export class AdminBookingController {
}
}
@Controller("admin/config")
export class AdminConfigController {
constructor(private readonly prisma: PrismaService) {}
private brandId(): string { return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001"; }
private requireAdmin(request: FastifyRequest): void {
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
if (!token) throw new BadRequestException("缺少管理员登录态");
if (readIdentity(token).role !== "admin") throw new BadRequestException("无管理员权限");
}
@Get("brand")
getBrand(@Req() request: FastifyRequest): Promise<unknown> { this.requireAdmin(request); return this.prisma.brandProfile.findUnique({ where: { id: this.brandId() } }); }
@Patch("brand")
updateBrand(@Req() request: FastifyRequest, @Body() body: UpdateBrandDto): Promise<unknown> { this.requireAdmin(request); return this.prisma.brandProfile.update({ where: { id: this.brandId() }, data: body }); }
@Patch("services/:id")
async updateService(@Req() request: FastifyRequest, @Param("id") id: string, @Body() body: UpdateServiceDto): Promise<unknown> {
this.requireAdmin(request);
const service = await this.prisma.service.findFirst({ where: { id, brandId: this.brandId() } });
if (!service) throw new BadRequestException("服务不存在");
return this.prisma.service.update({ where: { id }, data: body });
}
}
@Module({
controllers: [
HealthController,
@@ -287,6 +310,7 @@ export class AdminBookingController {
AdminSessionController,
CustomerBookingController,
AdminBookingController,
AdminConfigController,
],
providers: [PrismaService, WechatAuthService, BookingService, AdminAuthService],
exports: [PrismaService, WechatAuthService],
+5
View File
@@ -20,6 +20,8 @@ export interface BrandProfile {
contactPhone: string | null;
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 }
export interface BookingItemInput { serviceId: string; quantity: number }
export interface CreateBookingInput {
@@ -102,6 +104,9 @@ export class ApiClient {
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 }); }
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";