@coze-arch/cli 0.0.1-alpha.e8683e → 0.0.1-alpha.e89608

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