@coze-arch/cli 0.0.1-beta.6 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/lib/__templates__/expo/.coze +7 -2
- package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +46 -0
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +229 -0
- package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +47 -0
- package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +34 -0
- package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +46 -0
- package/lib/__templates__/expo/README.md +68 -7
- package/lib/__templates__/expo/_gitignore +2 -1
- package/lib/__templates__/expo/_npmrc +4 -5
- package/lib/__templates__/expo/client/app/+not-found.tsx +15 -64
- package/lib/__templates__/expo/client/app/_layout.tsx +15 -12
- package/lib/__templates__/expo/client/app/index.tsx +1 -0
- package/lib/__templates__/expo/client/app.config.ts +76 -0
- package/lib/__templates__/expo/client/components/Screen.tsx +3 -19
- package/lib/__templates__/expo/client/components/ThemedText.tsx +33 -0
- package/lib/__templates__/expo/client/components/ThemedView.tsx +37 -0
- package/lib/__templates__/expo/client/constants/theme.ts +117 -58
- package/lib/__templates__/expo/client/contexts/AuthContext.tsx +14 -107
- package/lib/__templates__/expo/client/declarations.d.ts +5 -0
- package/lib/__templates__/expo/{eslint.config.mjs → client/eslint.config.mjs} +40 -10
- package/lib/__templates__/expo/client/hooks/useColorScheme.tsx +48 -0
- package/lib/__templates__/expo/client/hooks/useSafeRouter.ts +152 -0
- package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
- package/lib/__templates__/expo/client/metro.config.js +124 -0
- package/lib/__templates__/expo/client/package.json +95 -0
- package/lib/__templates__/expo/client/screens/demo/index.tsx +25 -0
- package/lib/__templates__/expo/client/screens/demo/styles.ts +28 -0
- package/lib/__templates__/expo/client/scripts/install-missing-deps.js +11 -10
- package/lib/__templates__/expo/client/tsconfig.json +24 -0
- package/lib/__templates__/expo/client/utils/index.ts +23 -2
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1889 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +174 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/rule.js +112 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/tech.md +94 -0
- package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
- package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/rule.js +120 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/tech.md +58 -0
- package/lib/__templates__/expo/package.json +16 -101
- package/lib/__templates__/expo/patches/expo@54.0.33.patch +45 -0
- package/lib/__templates__/expo/pnpm-lock.yaml +1622 -3274
- package/lib/__templates__/expo/pnpm-workspace.yaml +3 -0
- package/lib/__templates__/expo/server/build.js +21 -0
- package/lib/__templates__/expo/server/package.json +34 -0
- package/lib/__templates__/expo/server/src/index.ts +20 -0
- package/lib/__templates__/expo/server/tsconfig.json +24 -0
- package/lib/__templates__/expo/template.config.js +58 -1
- package/lib/__templates__/expo/tsconfig.json +1 -24
- package/lib/__templates__/native-static/.coze +11 -0
- package/lib/__templates__/native-static/index.html +33 -0
- package/lib/__templates__/native-static/styles/main.css +136 -0
- package/lib/__templates__/native-static/template.config.js +22 -0
- package/lib/__templates__/nextjs/.coze +4 -3
- package/lib/__templates__/nextjs/README.md +5 -0
- package/lib/__templates__/nextjs/_gitignore +1 -0
- package/lib/__templates__/nextjs/_npmrc +2 -1
- package/lib/__templates__/nextjs/eslint.config.mjs +5 -0
- package/lib/__templates__/nextjs/next.config.ts +11 -0
- package/lib/__templates__/nextjs/package.json +15 -1
- package/lib/__templates__/nextjs/pnpm-lock.yaml +7694 -4394
- package/lib/__templates__/nextjs/scripts/build.sh +4 -1
- package/lib/__templates__/nextjs/scripts/dev.sh +15 -28
- package/lib/__templates__/nextjs/scripts/prepare.sh +9 -0
- package/lib/__templates__/nextjs/scripts/start.sh +7 -1
- package/lib/__templates__/nextjs/src/app/globals.css +109 -89
- package/lib/__templates__/nextjs/src/app/layout.tsx +20 -33
- package/lib/__templates__/nextjs/src/app/page.tsx +18 -49
- package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +29 -22
- package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +228 -230
- package/lib/__templates__/nextjs/src/server.ts +35 -0
- package/lib/__templates__/nextjs/template.config.js +68 -3
- package/lib/__templates__/nextjs/tsconfig.json +1 -1
- package/lib/__templates__/nuxt-vue/.coze +12 -0
- package/lib/__templates__/nuxt-vue/README.md +73 -0
- package/lib/__templates__/nuxt-vue/_gitignore +25 -0
- package/lib/__templates__/nuxt-vue/_npmrc +23 -0
- package/lib/__templates__/nuxt-vue/app/app.vue +6 -0
- package/lib/__templates__/nuxt-vue/app/pages/index.vue +23 -0
- package/lib/__templates__/nuxt-vue/assets/css/main.css +24 -0
- package/lib/__templates__/nuxt-vue/nuxt.config.ts +126 -0
- package/lib/__templates__/nuxt-vue/package.json +35 -0
- package/lib/__templates__/nuxt-vue/pnpm-lock.yaml +8759 -0
- package/lib/__templates__/nuxt-vue/postcss.config.mjs +8 -0
- package/lib/__templates__/nuxt-vue/public/favicon.ico +0 -0
- package/lib/__templates__/nuxt-vue/public/robots.txt +2 -0
- package/lib/__templates__/nuxt-vue/scripts/build.sh +14 -0
- package/lib/__templates__/nuxt-vue/scripts/dev.sh +39 -0
- package/lib/__templates__/nuxt-vue/scripts/prepare.sh +14 -0
- package/lib/__templates__/nuxt-vue/scripts/start.sh +21 -0
- package/lib/__templates__/nuxt-vue/server/api/hello.ts +10 -0
- package/lib/__templates__/nuxt-vue/server/middleware/logger.ts +10 -0
- package/lib/__templates__/nuxt-vue/server/routes/health.ts +10 -0
- package/lib/__templates__/nuxt-vue/tailwind.config.js +13 -0
- package/lib/__templates__/nuxt-vue/template.config.js +87 -0
- package/lib/__templates__/nuxt-vue/tsconfig.json +18 -0
- package/lib/__templates__/taro/.coze +14 -0
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +14 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +2 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +151 -0
- package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
- package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -0
- package/lib/__templates__/taro/README.md +763 -0
- package/lib/__templates__/taro/_gitignore +41 -0
- package/lib/__templates__/taro/_npmrc +18 -0
- package/lib/__templates__/taro/babel.config.js +12 -0
- package/lib/__templates__/taro/config/dev.ts +9 -0
- package/lib/__templates__/taro/config/index.ts +238 -0
- package/lib/__templates__/taro/config/prod.ts +34 -0
- package/lib/__templates__/taro/eslint.config.mjs +135 -0
- package/lib/__templates__/taro/key/private.appid.key +0 -0
- package/lib/__templates__/taro/package.json +112 -0
- package/lib/__templates__/taro/patches/@tarojs__plugin-mini-ci@4.1.9.patch +30 -0
- package/lib/__templates__/taro/pnpm-lock.yaml +23412 -0
- package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
- package/lib/__templates__/taro/project.config.json +15 -0
- package/lib/__templates__/taro/server/nest-cli.json +10 -0
- package/lib/__templates__/taro/server/package.json +40 -0
- package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
- package/lib/__templates__/taro/server/src/app.module.ts +10 -0
- package/lib/__templates__/taro/server/src/app.service.ts +8 -0
- package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
- package/lib/__templates__/taro/server/src/main.ts +49 -0
- package/lib/__templates__/taro/server/tsconfig.json +24 -0
- package/lib/__templates__/taro/src/app.config.ts +11 -0
- package/lib/__templates__/taro/src/app.css +156 -0
- package/lib/__templates__/taro/src/app.tsx +9 -0
- package/lib/__templates__/taro/src/components/ui/accordion.tsx +159 -0
- package/lib/__templates__/taro/src/components/ui/alert-dialog.tsx +260 -0
- package/lib/__templates__/taro/src/components/ui/alert.tsx +60 -0
- package/lib/__templates__/taro/src/components/ui/aspect-ratio.tsx +36 -0
- package/lib/__templates__/taro/src/components/ui/avatar.tsx +84 -0
- package/lib/__templates__/taro/src/components/ui/badge.tsx +37 -0
- package/lib/__templates__/taro/src/components/ui/breadcrumb.tsx +117 -0
- package/lib/__templates__/taro/src/components/ui/button-group.tsx +83 -0
- package/lib/__templates__/taro/src/components/ui/button.tsx +67 -0
- package/lib/__templates__/taro/src/components/ui/calendar.tsx +394 -0
- package/lib/__templates__/taro/src/components/ui/card.tsx +108 -0
- package/lib/__templates__/taro/src/components/ui/carousel.tsx +228 -0
- package/lib/__templates__/taro/src/components/ui/checkbox.tsx +58 -0
- package/lib/__templates__/taro/src/components/ui/code-block.tsx +169 -0
- package/lib/__templates__/taro/src/components/ui/collapsible.tsx +71 -0
- package/lib/__templates__/taro/src/components/ui/command.tsx +385 -0
- package/lib/__templates__/taro/src/components/ui/context-menu.tsx +614 -0
- package/lib/__templates__/taro/src/components/ui/dialog.tsx +256 -0
- package/lib/__templates__/taro/src/components/ui/drawer.tsx +192 -0
- package/lib/__templates__/taro/src/components/ui/dropdown-menu.tsx +561 -0
- package/lib/__templates__/taro/src/components/ui/field.tsx +228 -0
- package/lib/__templates__/taro/src/components/ui/hover-card.tsx +282 -0
- package/lib/__templates__/taro/src/components/ui/input-group.tsx +197 -0
- package/lib/__templates__/taro/src/components/ui/input-otp.tsx +136 -0
- package/lib/__templates__/taro/src/components/ui/input.tsx +56 -0
- package/lib/__templates__/taro/src/components/ui/label.tsx +24 -0
- package/lib/__templates__/taro/src/components/ui/menubar.tsx +595 -0
- package/lib/__templates__/taro/src/components/ui/navigation-menu.tsx +264 -0
- package/lib/__templates__/taro/src/components/ui/pagination.tsx +118 -0
- package/lib/__templates__/taro/src/components/ui/popover.tsx +291 -0
- package/lib/__templates__/taro/src/components/ui/portal.tsx +19 -0
- package/lib/__templates__/taro/src/components/ui/progress.tsx +28 -0
- package/lib/__templates__/taro/src/components/ui/radio-group.tsx +64 -0
- package/lib/__templates__/taro/src/components/ui/resizable.tsx +346 -0
- package/lib/__templates__/taro/src/components/ui/scroll-area.tsx +34 -0
- package/lib/__templates__/taro/src/components/ui/select.tsx +438 -0
- package/lib/__templates__/taro/src/components/ui/separator.tsx +30 -0
- package/lib/__templates__/taro/src/components/ui/sheet.tsx +262 -0
- package/lib/__templates__/taro/src/components/ui/skeleton.tsx +17 -0
- package/lib/__templates__/taro/src/components/ui/slider.tsx +203 -0
- package/lib/__templates__/taro/src/components/ui/sonner.tsx +1 -0
- package/lib/__templates__/taro/src/components/ui/switch.tsx +55 -0
- package/lib/__templates__/taro/src/components/ui/table.tsx +142 -0
- package/lib/__templates__/taro/src/components/ui/tabs.tsx +114 -0
- package/lib/__templates__/taro/src/components/ui/textarea.tsx +54 -0
- package/lib/__templates__/taro/src/components/ui/toast.tsx +517 -0
- package/lib/__templates__/taro/src/components/ui/toggle-group.tsx +120 -0
- package/lib/__templates__/taro/src/components/ui/toggle.tsx +77 -0
- package/lib/__templates__/taro/src/components/ui/tooltip.tsx +455 -0
- package/lib/__templates__/taro/src/index.html +39 -0
- package/lib/__templates__/taro/src/lib/hooks/use-keyboard-offset.ts +37 -0
- package/lib/__templates__/taro/src/lib/measure.ts +115 -0
- package/lib/__templates__/taro/src/lib/platform.ts +12 -0
- package/lib/__templates__/taro/src/lib/utils.ts +6 -0
- package/lib/__templates__/taro/src/network.ts +39 -0
- package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
- package/lib/__templates__/taro/src/pages/index/index.css +1 -0
- package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
- package/lib/__templates__/taro/src/presets/dev-debug.ts +23 -0
- package/lib/__templates__/taro/src/presets/h5-container.tsx +15 -0
- package/lib/__templates__/taro/src/presets/h5-navbar.tsx +238 -0
- package/lib/__templates__/taro/src/presets/h5-styles.ts +220 -0
- package/lib/__templates__/taro/src/presets/index.tsx +18 -0
- package/lib/__templates__/taro/stylelint.config.mjs +4 -0
- package/lib/__templates__/taro/template.config.js +68 -0
- package/lib/__templates__/taro/tsconfig.json +29 -0
- package/lib/__templates__/taro/types/global.d.ts +32 -0
- package/lib/__templates__/templates.json +173 -36
- package/lib/__templates__/vite/.coze +4 -3
- package/lib/__templates__/vite/README.md +383 -26
- package/lib/__templates__/vite/_gitignore +2 -0
- package/lib/__templates__/vite/_npmrc +2 -1
- package/lib/__templates__/vite/eslint.config.mjs +14 -0
- package/lib/__templates__/vite/package.json +23 -3
- package/lib/__templates__/vite/pnpm-lock.yaml +2509 -293
- package/lib/__templates__/vite/scripts/build.sh +4 -1
- package/lib/__templates__/vite/scripts/dev.sh +16 -28
- package/lib/__templates__/vite/scripts/prepare.sh +9 -0
- package/lib/__templates__/vite/scripts/start.sh +9 -3
- package/lib/__templates__/vite/server/routes/index.ts +31 -0
- package/lib/__templates__/vite/server/server.ts +65 -0
- package/lib/__templates__/vite/server/vite.ts +67 -0
- package/lib/__templates__/vite/src/main.ts +17 -48
- package/lib/__templates__/vite/template.config.js +77 -7
- package/lib/__templates__/vite/tsconfig.json +4 -3
- package/lib/__templates__/vite/vite.config.ts +8 -3
- package/lib/cli.js +1545 -526
- package/package.json +20 -7
- package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +0 -109
- package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +0 -257
- package/lib/__templates__/expo/app.json +0 -63
- package/lib/__templates__/expo/babel.config.js +0 -9
- package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +0 -43
- package/lib/__templates__/expo/client/app/(tabs)/home.tsx +0 -1
- package/lib/__templates__/expo/client/app/(tabs)/index.tsx +0 -7
- package/lib/__templates__/expo/client/hooks/useColorScheme.ts +0 -1
- package/lib/__templates__/expo/client/index.js +0 -12
- package/lib/__templates__/expo/client/screens/home/index.tsx +0 -54
- package/lib/__templates__/expo/client/screens/home/styles.ts +0 -332
- package/lib/__templates__/expo/metro.config.js +0 -53
- package/lib/__templates__/expo/src/index.ts +0 -12
- package/lib/__templates__/nextjs/.vscode/settings.json +0 -121
- package/lib/__templates__/nextjs/server.mjs +0 -50
- package/lib/__templates__/vite/.vscode/settings.json +0 -7
- /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
# Coze Mini Program
|
|
2
|
+
|
|
3
|
+
这是一个基于 [Taro 4](https://docs.taro.zone/docs/) + [Nest.js](https://nestjs.com/) 的前后端分离项目,由扣子编程 CLI 创建。
|
|
4
|
+
|
|
5
|
+
## 技术栈
|
|
6
|
+
|
|
7
|
+
- **整体框架**: Taro 4.1.9
|
|
8
|
+
- **语言**: TypeScript 5.4.5
|
|
9
|
+
- **渲染**: React 18.0.0
|
|
10
|
+
- **样式**: TailwindCSS 4.1.18
|
|
11
|
+
- **Tailwind 适配层**: weapp-tailwindcss 4.9.2
|
|
12
|
+
- **状态管理**: Zustand 5.0.9
|
|
13
|
+
- **图标库**: lucide-react-taro latest
|
|
14
|
+
- **工程化**: Vite 4.2.0
|
|
15
|
+
- **包管理**: pnpm
|
|
16
|
+
- **运行时**: Node.js >= 18
|
|
17
|
+
- **服务端**: NestJS 10.4.15
|
|
18
|
+
- **数据库 ORM**: Drizzle ORM 0.45.1
|
|
19
|
+
- **类型校验**: Zod 4.3.5
|
|
20
|
+
|
|
21
|
+
## 项目结构
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
├── .cozeproj/ # Coze 平台配置
|
|
25
|
+
│ └── scripts/ # 构建和运行脚本
|
|
26
|
+
├── config/ # Taro 构建配置
|
|
27
|
+
│ ├── index.ts # 主配置文件
|
|
28
|
+
│ ├── dev.ts # 开发环境配置
|
|
29
|
+
│ └── prod.ts # 生产环境配置
|
|
30
|
+
├── server/ # NestJS 后端服务
|
|
31
|
+
│ └── src/
|
|
32
|
+
│ ├── main.ts # 服务入口
|
|
33
|
+
│ ├── app.module.ts # 根模块
|
|
34
|
+
│ ├── app.controller.ts # 应用控制器
|
|
35
|
+
│ └── app.service.ts # 应用服务
|
|
36
|
+
├── src/ # 前端源码
|
|
37
|
+
│ ├── pages/ # 页面组件
|
|
38
|
+
│ ├── presets/ # 框架预置逻辑(无需读取,如无必要不改动)
|
|
39
|
+
│ ├── utils/ # 工具函数
|
|
40
|
+
│ ├── network.ts # 封装好的网络请求工具
|
|
41
|
+
│ ├── app.ts # 应用入口
|
|
42
|
+
│ ├── app.config.ts # 应用配置
|
|
43
|
+
│ └── app.css # 全局样式
|
|
44
|
+
├── types/ # TypeScript 类型定义
|
|
45
|
+
├── key/ # 小程序密钥(CI 上传用)
|
|
46
|
+
├── .env.local # 环境变量
|
|
47
|
+
└── project.config.json # 微信小程序项目配置
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 快速开始
|
|
51
|
+
|
|
52
|
+
### 安装依赖
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pnpm install
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 本地开发
|
|
59
|
+
|
|
60
|
+
同时启动 H5 前端和 NestJS 后端:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pnpm dev
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- 前端地址:http://localhost:5000
|
|
67
|
+
- 后端地址:http://localhost:3000
|
|
68
|
+
|
|
69
|
+
单独启动:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pnpm dev:web # 仅 H5 前端
|
|
73
|
+
pnpm dev:weapp # 仅微信小程序
|
|
74
|
+
pnpm dev:server # 仅后端服务
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 构建
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pnpm build # 构建所有(H5 + 小程序 + 后端)
|
|
81
|
+
pnpm build:web # 仅构建 H5,输出到 dist-web
|
|
82
|
+
pnpm build:weapp # 仅构建微信小程序,输出到 dist
|
|
83
|
+
pnpm build:server # 仅构建后端
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 预览小程序
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pnpm preview:weapp # 构建并生成预览小程序二维码
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## 前端核心开发规范
|
|
93
|
+
|
|
94
|
+
### 新建页面流程
|
|
95
|
+
|
|
96
|
+
1. 在 \`src/pages/\` 下创建页面目录
|
|
97
|
+
2. 创建 \`index.tsx\`(页面组件)
|
|
98
|
+
3. 创建 \`index.config.ts\`(页面配置)
|
|
99
|
+
4. 创建 \`index.css\`(页面样式,可选)
|
|
100
|
+
5. 在 \`src/app.config.ts\` 的 \`pages\` 数组中注册页面路径
|
|
101
|
+
|
|
102
|
+
或使用 Taro 脚手架命令:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
pnpm new # 交互式创建页面/组件
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 组件库
|
|
109
|
+
|
|
110
|
+
#### UI 组件
|
|
111
|
+
|
|
112
|
+
UI 组件位于 `@/components/ui`,推荐按需引入:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { Button } from '@/components/ui/button'
|
|
116
|
+
import { Card, CardContent } from '@/components/ui/card'
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
UI 组件列表:
|
|
120
|
+
|
|
121
|
+
Accordion,Alert,AlertDialog,AspectRatio,Avatar,Badge,Breadcrumb,Button,ButtonGroup,Calendar,Card,Carousel,Checkbox,CodeBlock,Collapsible,Command,ContextMenu,Dialog,Drawer,DropdownMenu,Field,HoverCard,Input,InputGroup,InputOTP,Label,Menubar,NavigationMenu,Pagination,Popover,Portal,Progress,RadioGroup,Resizable,ScrollArea,Select,Separator,Sheet,Skeleton,Slider,Sonner,Switch,Table,Tabs,Textarea,Toast,Toggle,ToggleGroup,Tooltip
|
|
122
|
+
|
|
123
|
+
#### Taro 原生组件
|
|
124
|
+
|
|
125
|
+
可以使用的 Taro 组件(UI 未覆盖)
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { View, Text, Icon, Image } from '@tarojs/components'
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Taro 原生组件列表:
|
|
132
|
+
|
|
133
|
+
Text,Icon,RichText,CheckboxGroup,Editor,Form,Picker,PickerView,PickerViewColumn,Radio,FunctionalPageNavigator,NavigationBar,Navigator,TabItem,Camera,Image,Video,ScrollView,Swiper,SwiperItem,View
|
|
134
|
+
|
|
135
|
+
### 路径别名
|
|
136
|
+
|
|
137
|
+
项目配置了 `@/*` 路径别名指向 `src/*`:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { SomeComponent } from '@/components/some-component'
|
|
141
|
+
import { useUserStore } from '@/stores/user'
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 代码模板
|
|
145
|
+
|
|
146
|
+
#### 页面组件 (TypeScript + React)
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
// src/pages/example/index.tsx
|
|
150
|
+
import { View } from '@tarojs/components'
|
|
151
|
+
import { useLoad, useDidShow } from '@tarojs/taro'
|
|
152
|
+
import type { FC } from 'react'
|
|
153
|
+
import { Button } from '@/components/ui/button'
|
|
154
|
+
import {
|
|
155
|
+
Card,
|
|
156
|
+
CardContent,
|
|
157
|
+
CardDescription,
|
|
158
|
+
CardFooter,
|
|
159
|
+
CardHeader,
|
|
160
|
+
CardTitle,
|
|
161
|
+
} from '@/components/ui/card'
|
|
162
|
+
import './index.css'
|
|
163
|
+
|
|
164
|
+
const ExamplePage: FC = () => {
|
|
165
|
+
useLoad(() => {
|
|
166
|
+
console.log('Page loaded.')
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
useDidShow(() => {
|
|
170
|
+
console.log('Page showed.')
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
return (
|
|
174
|
+
<View className="p-4">
|
|
175
|
+
<Card>
|
|
176
|
+
<CardHeader>
|
|
177
|
+
<CardTitle>Hello Taro!</CardTitle>
|
|
178
|
+
<CardDescription>
|
|
179
|
+
页面布局用 Taro 基础组件,交互与视觉优先用项目内置 UI 组件。
|
|
180
|
+
</CardDescription>
|
|
181
|
+
</CardHeader>
|
|
182
|
+
<CardContent>
|
|
183
|
+
<View className="text-sm text-muted-foreground">
|
|
184
|
+
组件位于 src/components/ui,推荐按需从 @/components/ui/* 引入。
|
|
185
|
+
</View>
|
|
186
|
+
</CardContent>
|
|
187
|
+
<CardFooter className="justify-end">
|
|
188
|
+
<Button size="sm" onClick={() => console.log('clicked')}>
|
|
189
|
+
点击
|
|
190
|
+
</Button>
|
|
191
|
+
</CardFooter>
|
|
192
|
+
</Card>
|
|
193
|
+
</View>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export default ExamplePage
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
#### 页面配置
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
// src/pages/example/index.config.ts
|
|
204
|
+
import { definePageConfig } from '@tarojs/taro'
|
|
205
|
+
|
|
206
|
+
export default definePageConfig({
|
|
207
|
+
navigationBarTitleText: '示例页面',
|
|
208
|
+
enablePullDownRefresh: true,
|
|
209
|
+
backgroundTextStyle: 'dark',
|
|
210
|
+
})
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
#### 应用配置
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
// src/app.config.ts
|
|
217
|
+
import { defineAppConfig } from '@tarojs/taro'
|
|
218
|
+
|
|
219
|
+
export default defineAppConfig({
|
|
220
|
+
pages: [
|
|
221
|
+
'pages/index/index',
|
|
222
|
+
'pages/example/index',
|
|
223
|
+
],
|
|
224
|
+
window: {
|
|
225
|
+
backgroundTextStyle: 'light',
|
|
226
|
+
navigationBarBackgroundColor: '#fff',
|
|
227
|
+
navigationBarTitleText: 'App',
|
|
228
|
+
navigationBarTextStyle: 'black',
|
|
229
|
+
},
|
|
230
|
+
// TabBar 配置 (可选)
|
|
231
|
+
// tabBar: {
|
|
232
|
+
// list: [
|
|
233
|
+
// { pagePath: 'pages/index/index', text: '首页' },
|
|
234
|
+
// ],
|
|
235
|
+
// },
|
|
236
|
+
})
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### 发送请求
|
|
240
|
+
|
|
241
|
+
**IMPORTANT: 禁止直接使用 Taro.request、Taro.uploadFile、Taro.downloadFile,使用 Network.request、Network.uploadFile、Network.downloadFile 替代。**
|
|
242
|
+
|
|
243
|
+
Network 是对 Taro.request、Taro.uploadFile、Taro.downloadFile 的封装,自动添加项目域名前缀,参数与 Taro 一致。
|
|
244
|
+
|
|
245
|
+
✅ 正确使用方式
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import { Network } from '@/network'
|
|
249
|
+
|
|
250
|
+
// GET 请求
|
|
251
|
+
const data = await Network.request({
|
|
252
|
+
url: '/api/hello'
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
// POST 请求
|
|
256
|
+
const result = await Network.request({
|
|
257
|
+
url: '/api/user/login',
|
|
258
|
+
method: 'POST',
|
|
259
|
+
data: { username, password }
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
// 文件上传
|
|
263
|
+
await Network.uploadFile({
|
|
264
|
+
url: '/api/upload',
|
|
265
|
+
filePath: tempFilePath,
|
|
266
|
+
name: 'file'
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
// 文件下载
|
|
270
|
+
await Network.downloadFile({
|
|
271
|
+
url: '/api/download/file.pdf'
|
|
272
|
+
})
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
❌ 错误用法
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
import Taro from '@tarojs/taro'
|
|
279
|
+
|
|
280
|
+
// ❌ 会导致自动域名拼接无法生效,除非是特殊指定域名
|
|
281
|
+
const data = await Network.request({
|
|
282
|
+
url: 'http://localhost/api/hello'
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
// ❌ 不要直接使用 Taro.request
|
|
286
|
+
await Taro.request({ url: '/api/hello' })
|
|
287
|
+
|
|
288
|
+
// ❌ 不要直接使用 Taro.uploadFile
|
|
289
|
+
await Taro.uploadFile({ url: '/api/upload', filePath, name: 'file' })
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### Zustand 状态管理
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
// src/stores/user.ts
|
|
296
|
+
import { create } from 'zustand'
|
|
297
|
+
|
|
298
|
+
interface UserState {
|
|
299
|
+
userInfo: UserInfo | null
|
|
300
|
+
token: string
|
|
301
|
+
setUserInfo: (info: UserInfo) => void
|
|
302
|
+
setToken: (token: string) => void
|
|
303
|
+
logout: () => void
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
interface UserInfo {
|
|
307
|
+
id: string
|
|
308
|
+
name: string
|
|
309
|
+
avatar: string
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export const useUserStore = create<UserState>((set) => ({
|
|
313
|
+
userInfo: null,
|
|
314
|
+
token: '',
|
|
315
|
+
setUserInfo: (info) => set({ userInfo: info }),
|
|
316
|
+
setToken: (token) => set({ token }),
|
|
317
|
+
logout: () => set({ userInfo: null, token: '' }),
|
|
318
|
+
}))
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### Taro 生命周期 Hooks
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import {
|
|
325
|
+
useLoad, // 页面加载 (onLoad)
|
|
326
|
+
useReady, // 页面初次渲染完成 (onReady)
|
|
327
|
+
useDidShow, // 页面显示 (onShow)
|
|
328
|
+
useDidHide, // 页面隐藏 (onHide)
|
|
329
|
+
usePullDownRefresh, // 下拉刷新 (onPullDownRefresh)
|
|
330
|
+
useReachBottom, // 触底加载 (onReachBottom)
|
|
331
|
+
useShareAppMessage, // 分享 (onShareAppMessage)
|
|
332
|
+
useRouter, // 获取路由参数
|
|
333
|
+
} from '@tarojs/taro'
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### 路由导航
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
import Taro from '@tarojs/taro'
|
|
340
|
+
|
|
341
|
+
// 保留当前页面,跳转到新页面
|
|
342
|
+
Taro.navigateTo({ url: '/pages/detail/index?id=1' })
|
|
343
|
+
|
|
344
|
+
// 关闭当前页面,跳转到新页面
|
|
345
|
+
Taro.redirectTo({ url: '/pages/detail/index' })
|
|
346
|
+
|
|
347
|
+
// 跳转到 tabBar 页面
|
|
348
|
+
Taro.switchTab({ url: '/pages/index/index' })
|
|
349
|
+
|
|
350
|
+
// 返回上一页
|
|
351
|
+
Taro.navigateBack({ delta: 1 })
|
|
352
|
+
|
|
353
|
+
// 获取路由参数
|
|
354
|
+
const router = useRouter()
|
|
355
|
+
const { id } = router.params
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### 图标使用 (lucide-react-taro)
|
|
359
|
+
|
|
360
|
+
**IMPORTANT: 禁止使用 lucide-react,必须使用 lucide-react-taro 替代。**
|
|
361
|
+
|
|
362
|
+
lucide-react-taro 是 Lucide 图标库的 Taro 适配版本,专为小程序环境优化,API 与 lucide-react 一致:
|
|
363
|
+
|
|
364
|
+
```tsx
|
|
365
|
+
import { View } from '@tarojs/components'
|
|
366
|
+
import { House, Settings, User, Search, Camera, Zap } from 'lucide-react-taro'
|
|
367
|
+
|
|
368
|
+
const IconDemo = () => {
|
|
369
|
+
return (
|
|
370
|
+
<View className="flex gap-4">
|
|
371
|
+
{/* 基本用法 */}
|
|
372
|
+
<House />
|
|
373
|
+
{/* 自定义尺寸和颜色 */}
|
|
374
|
+
<Settings size={32} color="#1890ff" />
|
|
375
|
+
{/* 自定义描边宽度 */}
|
|
376
|
+
<User size={24} strokeWidth={1.5} />
|
|
377
|
+
{/* 绝对描边宽度(描边不随 size 缩放) */}
|
|
378
|
+
<Camera size={48} strokeWidth={2} absoluteStrokeWidth />
|
|
379
|
+
{/* 组合使用 */}
|
|
380
|
+
<Zap size={32} color="#ff6b00" strokeWidth={1.5} className="my-icon" />
|
|
381
|
+
</View>
|
|
382
|
+
)
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
常用属性:
|
|
387
|
+
- `size` - 图标大小(默认 24)
|
|
388
|
+
- `color` - 图标颜色(默认 currentColor,小程序中建议显式设置)
|
|
389
|
+
- `strokeWidth` - 线条粗细(默认 2)
|
|
390
|
+
- `absoluteStrokeWidth` - 绝对描边宽度,启用后描边不随 size 缩放
|
|
391
|
+
- `className` / `style` - 自定义样式
|
|
392
|
+
|
|
393
|
+
更多图标请访问:https://lucide.dev/icons
|
|
394
|
+
|
|
395
|
+
### TabBar 图标生成 (CLI 工具)
|
|
396
|
+
|
|
397
|
+
**IMPORTANT: 微信小程序的 TabBar 不支持 base64 或 SVG 图片,必须使用本地 PNG 文件。**
|
|
398
|
+
|
|
399
|
+
lucide-react-taro 提供了 CLI 工具来生成 TabBar 所需的 PNG 图标:
|
|
400
|
+
|
|
401
|
+
```bash
|
|
402
|
+
# 生成带选中状态的图标
|
|
403
|
+
npx taro-lucide-tabbar House Settings User -c "#999999" -a "#1890ff"
|
|
404
|
+
|
|
405
|
+
# 指定输出目录和尺寸
|
|
406
|
+
npx taro-lucide-tabbar House Settings User -c "#999999" -a "#1890ff" -o ./src/assets/tabbar -s 81
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
CLI 参数:
|
|
410
|
+
- `--color, -c` (默认 #000000): 图标颜色
|
|
411
|
+
- `--active-color, -a`: 选中状态颜色
|
|
412
|
+
- `--size, -s` (默认 81): 图标尺寸
|
|
413
|
+
- `--output, -o` (默认 ./tabbar-icons): 输出目录
|
|
414
|
+
- `--stroke-width` (默认 2): 描边宽度
|
|
415
|
+
|
|
416
|
+
在 `app.config.ts` 中使用生成的图标:
|
|
417
|
+
|
|
418
|
+
> IMPORTANT:iconPath 和 selectedIconPath 必须以 `./` 开头,否则图标无法渲染
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
export default defineAppConfig({
|
|
422
|
+
tabBar: {
|
|
423
|
+
color: '#999999',
|
|
424
|
+
selectedColor: '#1890ff',
|
|
425
|
+
backgroundColor: '#ffffff',
|
|
426
|
+
borderStyle: 'black',
|
|
427
|
+
list: [
|
|
428
|
+
{
|
|
429
|
+
pagePath: 'pages/index/index',
|
|
430
|
+
text: '首页',
|
|
431
|
+
iconPath: './assets/tabbar/house.png',
|
|
432
|
+
selectedIconPath: './assets/tabbar/house-active.png',
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
pagePath: 'pages/settings/index',
|
|
436
|
+
text: '设置',
|
|
437
|
+
iconPath: './assets/tabbar/settings.png',
|
|
438
|
+
selectedIconPath: './assets/tabbar/settings-active.png',
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
pagePath: 'pages/user/index',
|
|
442
|
+
text: '用户',
|
|
443
|
+
iconPath: './assets/tabbar/user.png',
|
|
444
|
+
selectedIconPath: './assets/tabbar/user-active.png',
|
|
445
|
+
},
|
|
446
|
+
],
|
|
447
|
+
},
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
### Tailwind CSS 样式开发
|
|
451
|
+
|
|
452
|
+
IMPORTANT:必须使用 tailwindcss 实现样式,只有在必要情况下才能 fallback 到 css / less
|
|
453
|
+
|
|
454
|
+
> 项目已集成 Tailwind CSS 4.x + weapp-tailwindcss,支持跨端原子化样式:
|
|
455
|
+
|
|
456
|
+
```tsx
|
|
457
|
+
import { View, Text } from '@tarojs/components'
|
|
458
|
+
import { Button } from '@/components/ui/button'
|
|
459
|
+
|
|
460
|
+
<View className="flex flex-col items-center justify-center min-h-screen bg-gray-100">
|
|
461
|
+
<Text className="text-2xl font-bold text-blue-600 mb-4">标题</Text>
|
|
462
|
+
<View className="w-full px-4">
|
|
463
|
+
<Button className="w-full" size="lg">
|
|
464
|
+
按钮
|
|
465
|
+
</Button>
|
|
466
|
+
</View>
|
|
467
|
+
</View>
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### 性能优化
|
|
471
|
+
|
|
472
|
+
#### 图片懒加载
|
|
473
|
+
|
|
474
|
+
```tsx
|
|
475
|
+
import { Image } from '@tarojs/components'
|
|
476
|
+
|
|
477
|
+
<Image src={imageUrl} lazyLoad mode="aspectFill" />
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
#### 虚拟列表
|
|
481
|
+
|
|
482
|
+
```tsx
|
|
483
|
+
import { VirtualList } from '@tarojs/components'
|
|
484
|
+
|
|
485
|
+
<VirtualList
|
|
486
|
+
height={500}
|
|
487
|
+
itemData={list}
|
|
488
|
+
itemCount={list.length}
|
|
489
|
+
itemSize={100}
|
|
490
|
+
renderItem={({ index, style, data }) => (
|
|
491
|
+
<View style={style}>{data[index].name}</View>
|
|
492
|
+
)}
|
|
493
|
+
/>
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
#### 分包加载
|
|
497
|
+
|
|
498
|
+
```typescript
|
|
499
|
+
// src/app.config.ts
|
|
500
|
+
export default defineAppConfig({
|
|
501
|
+
pages: ['pages/index/index'],
|
|
502
|
+
subPackages: [
|
|
503
|
+
{
|
|
504
|
+
root: 'packageA',
|
|
505
|
+
pages: ['pages/detail/index'],
|
|
506
|
+
},
|
|
507
|
+
],
|
|
508
|
+
})
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### 小程序限制
|
|
512
|
+
|
|
513
|
+
| 限制项 | 说明 |
|
|
514
|
+
| -------- | ---------------------------------------- |
|
|
515
|
+
| 主包体积 | ≤ 2MB |
|
|
516
|
+
| 总包体积 | ≤ 20MB |
|
|
517
|
+
| 域名配置 | 生产环境需在小程序后台配置合法域名 |
|
|
518
|
+
| 本地开发 | 需在微信开发者工具开启「不校验合法域名」 |
|
|
519
|
+
|
|
520
|
+
### 权限配置
|
|
521
|
+
|
|
522
|
+
```typescript
|
|
523
|
+
// src/app.config.ts
|
|
524
|
+
export default defineAppConfig({
|
|
525
|
+
// ...其他配置
|
|
526
|
+
permission: {
|
|
527
|
+
'scope.userLocation': {
|
|
528
|
+
desc: '你的位置信息将用于小程序位置接口的效果展示'
|
|
529
|
+
}
|
|
530
|
+
},
|
|
531
|
+
requiredPrivateInfos: ['getLocation', 'chooseAddress']
|
|
532
|
+
})
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
### 位置服务
|
|
536
|
+
|
|
537
|
+
```typescript
|
|
538
|
+
// 需先在 app.config.ts 中配置 permission
|
|
539
|
+
async function getLocation(): Promise<Taro.getLocation.SuccessCallbackResult> {
|
|
540
|
+
return await Taro.getLocation({ type: 'gcj02' })
|
|
541
|
+
}
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
## 后端核心开发规范
|
|
545
|
+
|
|
546
|
+
本项目后端基于 NestJS + TypeScript 构建,提供高效、可扩展的服务端能力。
|
|
547
|
+
|
|
548
|
+
### 项目结构
|
|
549
|
+
|
|
550
|
+
```sh
|
|
551
|
+
.
|
|
552
|
+
├── server/ # NestJS 后端服务
|
|
553
|
+
│ └── src/
|
|
554
|
+
│ ├── main.ts # 服务入口
|
|
555
|
+
│ ├── app.module.ts # 根模块
|
|
556
|
+
│ ├── app.controller.ts # 根控制器
|
|
557
|
+
│ └── app.service.ts # 根服务
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
### 开发命令
|
|
561
|
+
|
|
562
|
+
```sh
|
|
563
|
+
pnpm dev:server // 启动开发服务 (热重载, 默认端口 3000)
|
|
564
|
+
pnpm build:server // 构建生产版本
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
### 新建模块流程 (CLI)
|
|
568
|
+
|
|
569
|
+
快速生成样板代码:
|
|
570
|
+
|
|
571
|
+
```bash
|
|
572
|
+
cd server
|
|
573
|
+
|
|
574
|
+
# 生成完整的 CRUD 资源 (包含 Module, Controller, Service, DTO, Entity)
|
|
575
|
+
npx nest g resource modules/product
|
|
576
|
+
|
|
577
|
+
# 仅生成特定部分
|
|
578
|
+
npx nest g module modules/order
|
|
579
|
+
npx nest g controller modules/order
|
|
580
|
+
npx nest g service modules/order
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
### 环境变量配置
|
|
584
|
+
|
|
585
|
+
在 server/ 根目录创建 .env 文件:
|
|
586
|
+
|
|
587
|
+
```sh
|
|
588
|
+
## 服务端口
|
|
589
|
+
PORT=3000
|
|
590
|
+
|
|
591
|
+
## 微信小程序配置
|
|
592
|
+
WX_APP_ID=你的AppID
|
|
593
|
+
WX_APP_SECRET=你的AppSecret
|
|
594
|
+
|
|
595
|
+
## JWT 密钥
|
|
596
|
+
JWT_SECRET=your-super-secret-key
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
在代码中使用 @nestjs/config 读取环境变量:
|
|
600
|
+
|
|
601
|
+
```typescript
|
|
602
|
+
import { ConfigService } from '@nestjs/config';
|
|
603
|
+
|
|
604
|
+
// 在 Service 中注入
|
|
605
|
+
constructor(private configService: ConfigService) {}
|
|
606
|
+
|
|
607
|
+
getWxConfig() {
|
|
608
|
+
return {
|
|
609
|
+
appId: this.configService.get<string>('WX_APP_ID'),
|
|
610
|
+
secret: this.configService.get<string>('WX_APP_SECRET'),
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
### 标准响应封装
|
|
616
|
+
|
|
617
|
+
建议使用拦截器 (Interceptor) 统一 API 响应格式:
|
|
618
|
+
|
|
619
|
+
```typeScript
|
|
620
|
+
// src/common/interceptors/transform.interceptor.ts
|
|
621
|
+
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
622
|
+
import { Observable } from 'rxjs';
|
|
623
|
+
import { map } from 'rxjs/operators';
|
|
624
|
+
|
|
625
|
+
export interface Response<T> {
|
|
626
|
+
code: number;
|
|
627
|
+
data: T;
|
|
628
|
+
message: string;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
@Injectable()
|
|
632
|
+
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {
|
|
633
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
|
|
634
|
+
return next.handle().pipe(
|
|
635
|
+
map((data) => ({
|
|
636
|
+
code: 200,
|
|
637
|
+
data,
|
|
638
|
+
message: 'success',
|
|
639
|
+
})),
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
在 main.ts 中全局注册:
|
|
646
|
+
|
|
647
|
+
```typescript
|
|
648
|
+
app.useGlobalInterceptors(new TransformInterceptor());
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
### 微信登录后端实现
|
|
652
|
+
|
|
653
|
+
```typescript
|
|
654
|
+
// src/modules/auth/auth.service.ts
|
|
655
|
+
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
656
|
+
import { HttpService } from '@nestjs/axios';
|
|
657
|
+
import { ConfigService } from '@nestjs/config';
|
|
658
|
+
import { lastValueFrom } from 'rxjs';
|
|
659
|
+
|
|
660
|
+
@Injectable()
|
|
661
|
+
export class AuthService {
|
|
662
|
+
constructor(
|
|
663
|
+
private httpService: HttpService,
|
|
664
|
+
private configService: ConfigService,
|
|
665
|
+
) {}
|
|
666
|
+
|
|
667
|
+
async code2Session(code: string) {
|
|
668
|
+
const appId = this.configService.get('WX_APP_ID');
|
|
669
|
+
const secret = this.configService.get('WX_APP_SECRET');
|
|
670
|
+
const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${secret}&js_code=${code}&grant_type=authorization_code`;
|
|
671
|
+
|
|
672
|
+
const { data } = await lastValueFrom(this.httpService.get(url));
|
|
673
|
+
|
|
674
|
+
if (data.errcode) {
|
|
675
|
+
throw new UnauthorizedException(`微信登录失败: ${data.errmsg}`);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
return data; // 包含 openid, session_key
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
### 异常处理
|
|
684
|
+
|
|
685
|
+
使用全局异常过滤器 (Filter) 统一错误响应:
|
|
686
|
+
|
|
687
|
+
```typescript
|
|
688
|
+
// src/common/filters/http-exception.filter.ts
|
|
689
|
+
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
|
|
690
|
+
import { Response } from 'express';
|
|
691
|
+
|
|
692
|
+
@Catch(HttpException)
|
|
693
|
+
export class HttpExceptionFilter implements ExceptionFilter {
|
|
694
|
+
catch(exception: HttpException, host: ArgumentsHost) {
|
|
695
|
+
const ctx = host.switchToHttp();
|
|
696
|
+
const response = ctx.getResponse<Response>();
|
|
697
|
+
const status = exception.getStatus();
|
|
698
|
+
const exceptionResponse = exception.getResponse();
|
|
699
|
+
|
|
700
|
+
response.status(status).json({
|
|
701
|
+
code: status,
|
|
702
|
+
message: typeof exceptionResponse === 'string' ? exceptionResponse : (exceptionResponse as any).message,
|
|
703
|
+
data: null,
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
在 main.ts 中注册:
|
|
710
|
+
|
|
711
|
+
```
|
|
712
|
+
app.useGlobalFilters(new HttpExceptionFilter());
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
### 数据库 (Drizzle ORM)
|
|
716
|
+
|
|
717
|
+
推荐使用 [Drizzle ORM](https://orm.drizzle.team/),已预安装。
|
|
718
|
+
|
|
719
|
+
### 类型校验 (Zod)
|
|
720
|
+
|
|
721
|
+
项目集成了 [Zod](https://zod.dev/) 用于运行时类型校验。
|
|
722
|
+
|
|
723
|
+
#### 定义 Schema
|
|
724
|
+
|
|
725
|
+
```typescript
|
|
726
|
+
import { z } from 'zod';
|
|
727
|
+
|
|
728
|
+
// 基础类型
|
|
729
|
+
const userSchema = z.object({
|
|
730
|
+
id: z.number(),
|
|
731
|
+
name: z.string().min(1).max(50),
|
|
732
|
+
email: z.string().email(),
|
|
733
|
+
age: z.number().int().positive().optional(),
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
// 从 schema 推导 TypeScript 类型
|
|
737
|
+
type User = z.infer<typeof userSchema>;
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
#### 请求校验
|
|
741
|
+
|
|
742
|
+
```typescript
|
|
743
|
+
// src/modules/user/dto/create-user.dto.ts
|
|
744
|
+
import { z } from 'zod';
|
|
745
|
+
|
|
746
|
+
export const createUserSchema = z.object({
|
|
747
|
+
nickname: z.string().min(1, '昵称不能为空').max(20, '昵称最多20个字符'),
|
|
748
|
+
avatar: z.string().url('头像必须是有效的URL').optional(),
|
|
749
|
+
phone: z.string().regex(/^1[3-9]\d{9}$/, '手机号格式不正确').optional(),
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
export type CreateUserDto = z.infer<typeof createUserSchema>;
|
|
753
|
+
|
|
754
|
+
// 在 Controller 中使用
|
|
755
|
+
@Post()
|
|
756
|
+
create(@Body() body: unknown) {
|
|
757
|
+
const result = createUserSchema.safeParse(body);
|
|
758
|
+
if (!result.success) {
|
|
759
|
+
throw new BadRequestException(result.error.errors);
|
|
760
|
+
}
|
|
761
|
+
return this.userService.create(result.data);
|
|
762
|
+
}
|
|
763
|
+
```
|