diff --git a/apps/admin/src/main.ts b/apps/admin/src/main.ts index fafaf30..9dcd2e1 100644 --- a/apps/admin/src/main.ts +++ b/apps/admin/src/main.ts @@ -9,11 +9,13 @@ export class AdminDashboard { async load(): Promise { 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 = `

品牌后台

未读订单:${unread.count}

选择订单查看详情
`; + root.innerHTML = `

品牌后台

未读订单:${unread.count}

品牌配置

服务配置

选择订单查看详情
`; 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("[data-service-save]").forEach((item) => item.addEventListener("click", () => void this.saveService(item.dataset.serviceSave))); root.querySelectorAll("[data-id]").forEach((item) => item.addEventListener("click", () => this.select(item.dataset.id))); } @@ -21,6 +23,8 @@ export class AdminDashboard { private async filter(): Promise { 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 = `

${this.selected.bookingNo}

客户:${this.selected.customerName}

`; detail.querySelector("#save")?.addEventListener("click", () => void this.save()); } private async save(): Promise { 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 { 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 { 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"; diff --git a/apps/api/src/admin.dto.ts b/apps/api/src/admin.dto.ts index f16eee1..b306c7d 100644 --- a/apps/api/src/admin.dto.ts +++ b/apps/api/src/admin.dto.ts @@ -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"]) diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 8d2c455..370cc39 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -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 { this.requireAdmin(request); return this.prisma.brandProfile.findUnique({ where: { id: this.brandId() } }); } + @Patch("brand") + updateBrand(@Req() request: FastifyRequest, @Body() body: UpdateBrandDto): Promise { 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 { + 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], diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index 700bd56..dabd767 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -20,6 +20,8 @@ export interface BrandProfile { contactPhone: string | null; contactWechat: string | null; } +export type BrandUpdate = Partial>; +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 { return this.request("/admin/bookings/tags", { method: "POST", body: { name } }); } + getAdminBrand(): Promise { return this.request("/admin/config/brand"); } + updateAdminBrand(body: BrandUpdate): Promise { return this.request("/admin/config/brand", { method: "PATCH", body }); } + updateAdminService(id: string, body: ServiceUpdate): Promise { return this.request(`/admin/config/services/${encodeURIComponent(id)}`, { method: "PATCH", body }); } private async request(path: string, options: { method?: string; body?: unknown } = {}): Promise { const headers: Record = { accept: "application/json" }; if (options.body !== undefined) headers["content-type"] = "application/json";