Skip to main content

ProductBackend (區域 CMS 管理系統)

📋 基本資訊

📖 專案描述

ProductBackend區域站點 (TW/US/JP) 的產品後台管理系統,提供產品內容管理 (CMS)、首頁編輯、AI 聊天輔助等功能。

架構定位

中央層 (Central)
├─ SERP API → GlobalHub API → GlobalHub

▼ 同步產品
區域層 (Regions: TW, US, JP)
├─ ProductsAPI (接收同步的產品)
├─ ProductBackend (本專案) ← 區域 CMS 管理
└─ 區域 B2C 前台

職責:

  • 管理來自 GlobalHub API 同步的產品
  • 區域化產品內容編輯(語言、幣別、定價)
  • 區域特色促銷與活動設定
  • 首頁與 CMS 內容編輯

🏗️ 專案架構

目錄結構

productbackend/
├── app/ # Next.js App Router
│ ├── layout.tsx # 根佈局
│ ├── page.tsx # 首頁 (Dashboard)
│ ├── globals.css # 全域樣式
│ ├── favicon.ico # 網站圖示
│ ├── homepage/ # 首頁編輯模組
│ │ ├── page.tsx # CMS 首頁編輯器
│ │ └── actions.ts # Server Actions
│ ├── products/ # 產品管理模組
│ │ ├── page.tsx # 產品列表
│ │ ├── new/ # 新增產品
│ │ └── [id]/ # 產品詳情/編輯
│ ├── globals/ # 全域設定
│ │ └── page.tsx # 全域設定頁
│ ├── chat/ # AI 聊天模組
│ │ └── page.tsx # 聊天介面
│ └── api/ # API Routes
│ └── cms/ # CMS API 端點

├── components/ # React 元件
│ ├── ui/ # shadcn/ui 元件庫
│ │ ├── button.tsx
│ │ ├── card.tsx
│ │ ├── input.tsx
│ │ ├── table.tsx
│ │ ├── tabs.tsx
│ │ ├── dialog.tsx
│ │ ├── dropdown-menu.tsx
│ │ ├── select.tsx
│ │ ├── label.tsx
│ │ ├── textarea.tsx
│ │ ├── switch.tsx
│ │ ├── checkbox.tsx
│ │ ├── badge.tsx
│ │ ├── avatar.tsx
│ │ ├── tooltip.tsx
│ │ ├── chart.tsx
│ │ ├── separator.tsx
│ │ ├── collapsible.tsx
│ │ ├── skeleton.tsx
│ │ ├── sheet.tsx
│ │ ├── sidebar.tsx
│ │ ├── logo.tsx
│ │ └── grid-pattern.tsx
│ ├── products/ # 產品管理元件
│ │ ├── products-table.tsx # 產品列表表格
│ │ ├── product-form.tsx # 產品表單
│ │ ├── product-status-badge.tsx # 狀態標籤
│ │ ├── media-uploader.tsx # 媒體上傳器
│ │ ├── itinerary-editor.tsx # 行程編輯器
│ │ ├── package-editor.tsx # 套餐編輯器
│ │ ├── pricing-editor.tsx # 費用編輯器
│ │ ├── traveler-fields-editor.tsx # 旅客欄位編輯器
│ │ ├── localized-input.tsx # 多語言輸入
│ │ └── locale-tabs.tsx # 語言切換標籤
│ ├── homepage/ # 首頁編輯元件
│ │ ├── editable-field.tsx # 可編輯欄位
│ │ └── widget-editor.tsx # Widget 編輯器
│ ├── globals/ # 全域設定元件
│ │ └── global-settings-editor.tsx
│ ├── chat/ # 聊天元件
│ │ ├── chat-main.tsx
│ │ ├── chat-sidebar.tsx
│ │ ├── chat-message.tsx
│ │ └── chat-input-box.tsx
│ └── theme-toggle.tsx # 主題切換

├── lib/ # 函式庫
│ ├── utils.ts # 工具函式 (cn, clsx, tailwind-merge)
│ ├── cms.ts # CMS API 客戶端
│ ├── products-api.ts # Products API 客戶端
│ └── sections.ts # CMS Section 定義

├── store/ # Zustand 狀態管理
│ ├── dashboard-store.ts # Dashboard 狀態
│ └── chat-store.ts # 聊天狀態

├── hooks/ # Custom Hooks
│ └── use-mobile.ts # 偵測行動裝置

├── mock-data/ # 模擬資料
│ ├── products.ts # 產品資料
│ ├── chats.ts # 聊天資料
│ ├── deals.ts # 交易資料
│ └── stats.ts # 統計資料

├── public/ # 靜態資源
│ └── ln.png # Logo

├── next.config.ts # Next.js 設定
├── package.json # 專案依賴
├── tsconfig.json # TypeScript 設定
├── components.json # shadcn/ui 設定
├── eslint.config.mjs # ESLint 設定
├── postcss.config.mjs # PostCSS 設定
├── Dockerfile # Docker 容器化
├── .gitlab-ci.yml # GitLab CI/CD
├── .env.example # 環境變數範例
├── .env.staging # Staging 環境變數
├── .env.production # Production 環境變數
└── README.md # 專案說明

技術棧詳細

核心框架

  • Next.js 16.0.10 - React 框架 (App Router)
  • React 19.2.3 - UI 函式庫
  • TypeScript 5 - 型別安全

UI 元件庫

  • shadcn/ui - 可自訂的高品質元件
  • Radix UI - 無障礙的 Headless UI 元件
  • Lucide React - 圖示庫

樣式

  • Tailwind CSS 4 - 實用優先的 CSS 框架
  • next-themes - 深色/淺色模式切換
  • class-variance-authority - 變體樣式管理
  • clsx + tailwind-merge - 條件式類別名稱合併

表格與圖表

  • @tanstack/react-table - 強大的表格元件
  • recharts - 圖表繪製

狀態管理

  • Zustand - 輕量級狀態管理

其他工具

  • date-fns - 日期處理
  • vaul - Drawer 元件

🎯 核心功能

1. 產品管理模組

頁面路由

路由說明
/products產品列表 - 搜尋、篩選、分頁
/products/new新增產品表單
/products/[id]產品詳情頁
/products/[id]/edit編輯產品表單

核心元件

元件檔案功能
ProductsTablecomponents/products/products-table.tsx產品列表表格,含搜尋、篩選、分頁
ProductFormcomponents/products/product-form.tsx新增/編輯產品主表單(6個分頁)
ProductStatusBadgecomponents/products/product-status-badge.tsx產品狀態標籤(草稿/已上架/已下架)
MediaUploadercomponents/products/media-uploader.tsx照片/影片上傳管理
ItineraryEditorcomponents/products/itinerary-editor.tsx行程表編輯器(多日行程+活動)
PackageEditorcomponents/products/package-editor.tsx套餐編輯器(支援多幣別)
PricingEditorcomponents/products/pricing-editor.tsx費用項目編輯器(支援多幣別)
TravelerFieldsEditorcomponents/products/traveler-fields-editor.tsx旅客資料欄位設定
LocalizedInputcomponents/products/localized-input.tsx多語言輸入欄位
LocaleTabscomponents/products/locale-tabs.tsx語言切換標籤

資料模型 (mock-data/products.ts)

幣別金額 (支援多幣別)

interface CurrencyAmount {
currency: string; // TWD, USD, JPY, EUR...
amount: number;
}

費用項目

interface PriceItem {
id: string;
name: string;
amounts: CurrencyAmount[]; // 多幣別金額
isIncluded: boolean;
description?: string;
}

套餐

interface Package {
id: string;
name: LocalizedString;
description?: LocalizedString;
basePrice: number;
currency: string;
maxCapacity?: number;
currentBookings: number;
isActive: boolean;
highlights: LocalizedString[];
}

產品

interface Product {
id: string;
name: LocalizedString;
summary: LocalizedString;
dateDescription: string;
media: MediaItem[];
description: DescriptionBlock[];
htmlDescription: string;
itinerary: ItineraryDay[];
purchaseNotes: LocalizedString;
pricing: PriceItem[];
notices: LocalizedString[];
redemption: LocalizedString;
validity: ValidityPeriod;
packages: Package[];
travelerFields: TravelerField[];
status: ProductStatus; // draft | published | archived
createdAt: string;
updatedAt: string;
}

支援幣別

費用項目和套餐編輯器支援以下幣別:

代碼名稱
TWD新台幣
USD美元
JPY日圓
EUR歐元
CNY人民幣
HKD港幣
SGD新加坡幣
KRW韓元
AUD澳幣
GBP英鎊
THB泰銖
MYR馬幣

2. 首頁編輯模組

整合 ApostropheCMS 的首頁內容編輯功能。

頁面路由

路由說明
/homepageCMS 首頁內容編輯器

核心元件

元件檔案功能
EditableFieldcomponents/homepage/editable-field.tsx可編輯欄位元件
WidgetEditorcomponents/homepage/widget-editor.tsxWidget 編輯器

CMS API (lib/cms.ts)

函式說明
getCmsToken()取得 CMS 登入 Token
getCmsPages()取得頁面資料
updateCmsPage()更新頁面欄位

可編輯欄位

支援編輯的欄位類型:

  • label, placeholder, title, cardTitle
  • subtitle, slideTitle, url, link
  • content, cardContent, explainLabel, pretitle
  • price, currency, description
  • buttonText, buttonLabel, alt, caption

3. AI 聊天模組

提供 AI 輔助的產品管理與諮詢功能。

頁面路由

路由說明
/chatAI 聊天介面

核心元件

元件檔案功能
ChatMaincomponents/chat/chat-main.tsx主聊天區域
ChatSidebarcomponents/chat/chat-sidebar.tsx聊天列表側邊欄
ChatMessagecomponents/chat/chat-message.tsx訊息顯示
ChatInputBoxcomponents/chat/chat-input-box.tsx輸入框

4. 全域設定

統一管理系統全域設定。

頁面路由

路由說明
/globals全域設定頁

🔌 API 整合

環境配置

環境API URL說明
開發http://localhost:5050/api/v1本地開發預設
測試https://productapi.107.liondev.work/api/v1Staging 環境
正式https://productapi.liontravel.com/api/v1Production 環境

設定檔

本地開發 - 建立 .env.local

PRODUCTS_API_URL=http://localhost:5050/api/v1

Staging - .env.staging Production - .env.production

API 客戶端

Products API (lib/products-api.ts)

  • 產品 CRUD 操作
  • 產品搜尋
  • 訂單管理

CMS API (lib/cms.ts)

  • ApostropheCMS 整合
  • 頁面內容管理

🚀 開發指南

環境需求

  • Node.js 20+
  • npmpnpm

本地開發

  1. 安裝依賴

    npm install
  2. 設定環境變數

    cp .env.example .env.local
    # 編輯 .env.local 設定 PRODUCTS_API_URL
  3. 啟動開發伺服器

    npm run dev

    開啟 http://localhost:3000

  4. 程式碼檢查

    npm run lint

建置生產版本

npm run build
npm start

Docker 部署

本地建置

docker build -t productbackend .

指定環境建置

docker build --build-arg PRODUCTS_API_URL=https://productapi.107.liondev.work/api/v1 -t productbackend:staging .

執行容器

docker run -p 3000:3000 productbackend:staging

📊 CI/CD 流程

GitLab CI 會根據分支自動選擇環境:

分支環境Image Tag
mainproduction:latest, :sha
其他staging:branch-name, :sha

🎨 UI/UX 設計

shadcn/ui 元件

使用 shadcn/ui 元件庫提供:

  • 一致性 - 統一的設計語言
  • 可訪問性 - 符合 WCAG 標準
  • 可自訂 - 完全控制樣式
  • 深色模式 - 內建主題切換

主要元件

  • Button - 按鈕(多種變體)
  • Input - 輸入框
  • Table - 表格(整合 @tanstack/react-table)
  • Dialog - 對話框
  • Dropdown - 下拉選單
  • Tabs - 標籤頁
  • Badge - 標籤
  • Card - 卡片容器
  • Chart - 圖表(整合 recharts)

響應式設計

  • Mobile-first - 優先考慮行動裝置
  • Breakpoints - Tailwind 標準斷點
  • Sidebar - 可收合的側邊欄

🔗 相關專案

專案關係說明
productsapi後端 API提供產品資料與訂單管理
globalhub前台展示B2C 客戶端
globalhubapi匯入服務產品資料匯入

資料流

ProductBackend (CMS)
↓ (管理產品內容)
ProductsAPI
↓ (提供資料)
GlobalHub (前台)

🧪 開發工具

VS Code 擴充

推薦安裝:

  • Tailwind CSS IntelliSense - Tailwind 自動完成
  • ESLint - 程式碼檢查
  • Prettier - 程式碼格式化

狀態管理

使用 Zustand 管理:

  • Dashboard 狀態
  • 聊天狀態
  • 使用者偏好設定

工具函式

lib/utils.ts 提供:

  • cn() - Tailwind 類別合併工具

📝 重要文件

檔案說明
next.config.tsNext.js 配置
tsconfig.jsonTypeScript 編譯設定
components.jsonshadcn/ui 元件設定
eslint.config.mjsESLint 規則
.env.example環境變數範例
DockerfileDocker 容器化設定
.gitlab-ci.ymlCI/CD 流程

🌐 多語言支援

透過 LocalizedInputLocaleTabs 元件支援:

  • en-US - 英文
  • zh-TW - 繁體中文
  • ja-JP - 日文

🔐 安全性

  • 環境變數 - 敏感資料不進版控
  • API 驗證 - 整合後端 API 驗證機制
  • 輸入驗證 - 表單驗證

📈 效能優化

  • Next.js Image - 自動圖片最佳化
  • Code Splitting - 自動程式碼分割
  • Server Components - 減少客戶端 JavaScript
  • Static Generation - 靜態頁面生成

🎯 未來規劃

  • 整合更多 CMS 功能
  • 增強 AI 聊天能力
  • 多站點管理
  • 更豐富的數據視覺化