('all');
// ...
}
```
#### 组件结构顺序
```typescript
// 1. "use client" 指令(如需要)
"use client"
// 2. 导入
import { useState } from 'react';
// 3. 类型定义
interface ExampleProps {
title: string;
children: React.ReactNode;
}
// 4. 组件定义
export function Example({ title, children }: ExampleProps) {
// 4a. hooks
const t = useTranslations('common');
// 4b. 派生状态
const formattedTitle = title.toUpperCase();
// 4c. 事件处理
const handleClick = () => { /* ... */ };
// 4d. 渲染
return (
{formattedTitle}
{children}
);
}
```
#### 组件设计原则
- 单一职责:每个组件只做一件事
- 组合优于继承:使用 children 和 props 组合
- Props 解构:始终解构 props,不要使用 `props.xxx`
- Forward Ref:可复用组件需要支持 ref 转发
- 有意义的默认值:为可选 props 提供合理的默认值
### 样式规范
#### Tailwind CSS 使用约定
- 使用 Tailwind 工具类,禁止手写 CSS(除非 CSS 变量或动画关键帧)
- 类名顺序遵循一致性(建议:布局 -> 尺寸 -> 间距 -> 排版 -> 颜色 -> 其他)
- 超过 5 个类名时考虑提取为组件或使用 `@apply`(谨慎使用)
- 响应式设计使用 Mobile-first 方式:`md:` -> `lg:`
- 条件样式使用模板字符串或 `clsx`/`cn` 工具函数
#### 设计令牌(Design Tokens)
| 令牌 | 值 | 用途 |
|------|-----|------|
| 主色 | `#3a6b35` | 品牌绿,CTA 按钮、强调元素 |
| 辅色 | 大地色系 | 深绿、岩石灰、矿物绿 |
| 字体 | Inter | 英文主字体 |
| 中文回退 | Source Han Sans SC, PingFang SC | 中文排版 |
| 断点 | `md: 769px`, `lg: 1025px` | 响应式分界 |
| 触摸目标 | 最小 44px | 移动端可访问性 |
| 对比度 | >= 4.5:1 | WCAG 2.1 AA |
#### 响应式断点
```css
/* Mobile-first */
/* 默认:< 769px (Mobile) */
/* md: >= 769px (Tablet) */
/* lg: >= 1025px (Desktop) */
```
### TypeScript 规范
- 启用 strict 模式
- 禁止使用 `any`,如必须使用需添加注释说明原因
- 函数参数和返回值必须有明确的类型注解
- 优先使用 `interface` 定义对象类型,`type` 用于联合类型、工具类型
- 使用 `as const` 断言定义常量对象
- 异步函数返回类型明确标注 `Promise`
- 使用 TypeScript 枚举或联合字面量类型替代魔法字符串
```typescript
// 推荐
interface NavigationItem {
id: string;
title: Record;
path: string;
}
type Locale = 'zh-CN' | 'en';
type ButtonVariant = 'primary' | 'secondary' | 'outline';
// 避免
const config: any = {}; // 禁止
function handleClick(e) { /* ... */ } // 缺少类型
```
### 国际化规范(next-intl,apps/web)
#### 翻译文件
- 翻译文件位于 `apps/web/messages/` 目录
- 文件格式:JSON(`zh-CN.json`、`en.json`)
- 使用命名空间分隔:`home`、`products`、`common`、`nav` 等
#### 使用方式
```typescript
// Server Component
import { useTranslations } from 'next-intl';
export default function Page() {
const t = useTranslations('home');
return {t('title')}
;
}
// Client Component
"use client"
import { useTranslations } from 'next-intl';
export function ProductCard() {
const t = useTranslations('products');
return {t('addToCart')};
}
```
#### 内容回退机制
- 回退链:请求语言 -> 基准语言 -> `en`
- 多语言内容使用 `Record` 类型存储
- 导航项通过 `visible_locales` 控制语言可见性
#### URL 策略
- 中文站:无语言前缀(如 `/products`)
- 国际站:`/en` 前缀(如 `/en/products`)
- SEO 自动生成 hreflang 替代链接
### API 调用规范
#### 数据获取(apps/web)
- Server Components 中直接使用 `fetch` 或 API 客户端
- Client Components 中使用 TanStack React Query
- API 调用超时设置:5 秒
- API 不可用时使用本地回退内容
```typescript
// Server Component 数据获取
async function getProduct(slug: string) {
try {
const res = await fetch(`${API_URL}/api/products/${slug}`, { next: { revalidate: 3600 } });
if (!res.ok) throw new Error('Failed to fetch');
return await res.json();
} catch {
return getFallbackProduct(slug); // 回退机制
}
}
```
#### API 客户端(apps/admin)
- 使用 `src/lib/api-client.ts` 统一管理 API 请求
- JWT Token 存储在 localStorage
- 自动刷新过期 Token(Access Token + Refresh Token 双令牌)
- API 请求前缀:`/admin/*`(需认证)
#### React Query 约定
- 使用 Query Key 工厂模式
- 避免在组件中直接调用 `fetch`,通过 hooks 封装
- 乐观更新用于即时反馈操作
### 路由规范
#### Next.js App Router(apps/web)
- 使用文件系统路由:`app/` 目录
- 动态路由参数为 `Promise` 类型:`params: Promise<{ slug: string }>`
- 布局使用 `layout.tsx`,页面使用 `page.tsx`
- 加载状态使用 `loading.tsx`
- 错误处理使用 `error.tsx`
- ISR 策略:首页 30 分钟,产品页 1 小时
#### React Router(apps/admin)
- 使用 `createBrowserRouter` 配置路由
- 所有页面使用 `lazy()` 懒加载实现代码分割
- `AuthGuard` 组件保护需要认证的路由
- 路由守卫在 `src/router/AuthGuard.tsx`
- 布局结构:`DashboardLayout`(侧边栏 + 内容区)和 `AuthLayout`(登录页)
### 表单规范
- 使用 React Hook Form 管理表单状态
- 使用 Zod 定义验证 schema
- 通过 `zodResolver` 连接 React Hook Form 和 Zod
```typescript
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
const schema = z.object({
email: z.string().email(),
password: z.string().min(8),
});
type LoginForm = z.infer;
export function LoginForm() {
const { register, handleSubmit, formState: { errors } } = useForm({
resolver: zodResolver(schema),
});
// ...
}
```
### 注释规范
- 公共组件和 Hook 必须有 JSDoc 注释
- 复杂业务逻辑处需有行内注释说明意图
- 禁止保留无意义的注释和注释掉的废弃代码
- `// TODO:` 标注待办事项,需包含上下文说明
- `// NOTE:` 标注重要设计决策或注意事项
### 错误处理规范
- 页面级错误使用 `error.tsx` 捕获
- API 调用失败必须提供回退方案
- 禁止捕获错误后不做任何处理(空 catch)
- 用户操作失败时展示友好的错误提示
- 使用 TypeScript 的 discriminated union 处理不同状态
### 性能规范
- Server Components:默认使用,减少客户端 JS 体积
- 代码分割:路由级自动分割,大型组件使用 `dynamic()` 懒加载
- 图片优化:使用 `OptimizedImage` 组件或 Next.js ``
- ISR:合理设置页面重新验证时间
- Bundle 分析:定期检查包体积
- Standalone 输出:生产构建使用 `output: 'standalone'`
### 无障碍规范(A11y)
- 语义化 HTML:使用正确的标签(`