From 893a547967231b110f4fc083b9579d802d35629f Mon Sep 17 00:00:00 2001 From: que01 Date: Sat, 19 Sep 2026 19:02:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B8=85=E7=90=86=E6=97=A7?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=85=A5=E5=8F=A3=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E5=88=86=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/admin/src/entry.ts | 23 --------- apps/admin/src/main.ts | 110 ---------------------------------------- apps/admin/src/main.tsx | 40 ++++++++++----- 3 files changed, 28 insertions(+), 145 deletions(-) delete mode 100644 apps/admin/src/entry.ts delete mode 100644 apps/admin/src/main.ts diff --git a/apps/admin/src/entry.ts b/apps/admin/src/entry.ts deleted file mode 100644 index 020ea3a..0000000 --- a/apps/admin/src/entry.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { bootstrapAdmin } from "./main.js"; -import { createApiClient } from "./lib/api.js"; - -const root = document.querySelector("#app"); -if (root) { - const token = localStorage.getItem("adminToken"); - if (!token) { - root.innerHTML = `

品牌后台

`; - root.querySelector("#login")?.addEventListener("submit", async (event) => { - event.preventDefault(); - const username = (root.querySelector("#username") as HTMLInputElement).value; - const password = (root.querySelector("#password") as HTMLInputElement).value; - try { - const client = createApiClient(); - const result = await client.loginAdmin(username, password); - localStorage.setItem("adminToken", result.token); - location.reload(); - } catch (error) { - alert(error instanceof Error ? error.message : "登录失败"); - } - }); - } else bootstrapAdmin(root); -} diff --git a/apps/admin/src/main.ts b/apps/admin/src/main.ts deleted file mode 100644 index 78c7671..0000000 --- a/apps/admin/src/main.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { ApiClient, type AdminBooking, type BookingStatus } from "@personal-brand/api-client"; - -export interface AdminDashboardOptions { - client: ApiClient; - root: HTMLElement; -} - -export class AdminDashboard { - private bookings: AdminBooking[] = []; - private selected: AdminBooking | undefined; - constructor(private readonly options: AdminDashboardOptions) {} - - async load(): Promise { - const { root, client } = this.options; - const [bookings, unread, brand, services] = await Promise.all([ - client.listAdminBookings(), - client.getAdminUnreadCount(), - client.getAdminBrand(), - client.listServices(), - ]); - this.bookings = bookings; - root.innerHTML = `

品牌后台

未读订单:${unread.count}

品牌配置

服务配置

    ${services.map((item) => `
  • `).join("")}
    ${this.listHtml()}
选择订单查看详情
`; - 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))); - } - - private listHtml(): string { - return ( - this.bookings - .map( - (item) => - `
  • `, - ) - .join("") || "
  • 暂无订单
  • " - ); - } - 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({ - templateKey: (root.querySelector("#brand-template") as HTMLSelectElement).value as - "food" | "personal", - 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"; - -export function bootstrapAdmin( - root: HTMLElement, - baseUrl = "http://localhost:3001", -): AdminDashboard { - const token = localStorage.getItem("adminToken"); - const client = new ApiClient({ - baseUrl, - fetcher: (input, init) => fetch(input, init), - ...(token ? { token } : {}), - }); - const dashboard = new AdminDashboard({ client, root }); - void dashboard.load(); - return dashboard; -} diff --git a/apps/admin/src/main.tsx b/apps/admin/src/main.tsx index 9d6ba64..6698039 100644 --- a/apps/admin/src/main.tsx +++ b/apps/admin/src/main.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { Suspense, useState } from "react"; import { createRoot } from "react-dom/client"; import { QueryClient, @@ -26,10 +26,24 @@ import { Badge, } from "antd"; import { createApiClient } from "./lib/api.js"; -import { InquiriesPage as FeatureInquiriesPage } from "./features/inquiries/InquiriesPage.js"; -import { TrustContentPage as FeatureTrustContentPage } from "./features/trust-content/TrustContentPage.js"; -import { ServicesPage as FeatureServicesPage } from "./features/services/ServicesPage.js"; -import { BrandPage as FeatureBrandPage } from "./features/brand/BrandPage.js"; +const FeatureInquiriesPage = React.lazy(() => + import("./features/inquiries/InquiriesPage.js").then((module) => ({ + default: module.InquiriesPage, + })), +); +const FeatureTrustContentPage = React.lazy(() => + import("./features/trust-content/TrustContentPage.js").then((module) => ({ + default: module.TrustContentPage, + })), +); +const FeatureServicesPage = React.lazy(() => + import("./features/services/ServicesPage.js").then((module) => ({ + default: module.ServicesPage, + })), +); +const FeatureBrandPage = React.lazy(() => + import("./features/brand/BrandPage.js").then((module) => ({ default: module.BrandPage })), +); import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom"; import type { BrandProfile, @@ -112,13 +126,15 @@ function Dashboard({ onLogout }: { onLogout: () => void }) { - - } /> - } /> - } /> - } /> - } /> - + }> + + } /> + } /> + } /> + } /> + } /> + +