# FastAPI 微型后端工程 这是一个简易的 FastAPI 后端,用 SQLite 保存数据,提供 Todo 和用户管理接口。 ## start DATABASE_PATH=/tmp/py-web.sqlite3 .venv/bin/uvicorn index:app --reload ## 工程结构 ```text . ├── app │ ├── main.py # 创建 FastAPI 应用,注册中间件和路由 │ ├── database.py # 数据库连接、建表、依赖注入 │ ├── schemas.py # 请求体和响应体的数据结构 │ └── routers │ ├── todos.py # Todo 接口 │ └── users.py # 用户接口 ├── index.py # 兼容入口,暴露 app 给 uvicorn ├── requirements.txt └── app.sqlite3 # 启动后自动创建,保存数据 ``` ## 安装依赖 ```bash python3 -m venv .venv source .venv/bin/activate python3 -m pip install -r requirements.txt ``` ## 启动服务 ```bash uvicorn index:app --reload ``` 启动后访问: - API 文档:http://127.0.0.1:8000/docs - 健康检查:http://127.0.0.1:8000/health 默认数据库文件是 `app.sqlite3`。如果想指定其他数据库文件: ```bash DATABASE_PATH=/tmp/py-web.sqlite3 uvicorn index:app --reload ``` 如果遇到 `attempt to write a readonly database`,通常说明数据库文件不是当前用户创建的。可以换一个 `DATABASE_PATH`,或者删除旧的数据库文件后重新启动服务。 ## 用户接口 ```text GET /users POST /users GET /users/{user_id} PATCH /users/{user_id} DELETE /users/{user_id} ``` ## 寺院隔离模块 寺院管理接口: ```text GET /temples POST /temples GET /temples/{temple_id} PATCH /temples/{temple_id} DELETE /temples/{temple_id} ``` 商品、订单、法事属于寺院隔离数据。调用这些接口时需要带请求头: ```text X-Temple-Id: 1 ``` 隔离接口: ```text GET /products POST /products GET /products/{product_id} PATCH /products/{product_id} DELETE /products/{product_id} GET /orders POST /orders GET /orders/{order_id} PATCH /orders/{order_id} DELETE /orders/{order_id} GET /ritual-services POST /ritual-services GET /ritual-services/{ritual_id} PATCH /ritual-services/{ritual_id} DELETE /ritual-services/{ritual_id} ``` 商品详情模型包含商品基础信息、按钮名称、展示配置、封面和多图、上架/开售/结束/下架时间、商品规格、商品详情 HTML、微信分享配置、功德证书预留和自动处理。商品规格字段为名称、单价、库存、排序和是否超度。 创建用户: ```bash curl -X POST http://127.0.0.1:8000/users \ -H "Content-Type: application/json" \ -d '{"name":"张三","phone":"13800000000","nickname":"小张","avatar":"","is_admin":false,"total_spent":0}' ``` 更新用户: ```bash curl -X PATCH http://127.0.0.1:8000/users/1 \ -H "Content-Type: application/json" \ -d '{"nickname":"张老板","is_admin":true,"total_spent":299.9}' ``` ## 前端调用示例 ```js async function createUser() { const response = await fetch("http://127.0.0.1:8000/users", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name: "张三", phone: "13800000000", nickname: "小张", avatar: "", is_admin: false, total_spent: 0, }), }); if (!response.ok) { throw new Error("创建用户失败"); } } return response.json(); } ``` ## Todo 接口 ```text GET /todos POST /todos GET /todos/{todo_id} PATCH /todos/{todo_id} DELETE /todos/{todo_id} ``` ## 前端工程 前端目录在 `front`,使用 Vue 3、Vite、vue-router 和 Ant Design Vue。用户管理页面已接入后端 `/users` 接口。 安装依赖: ```bash cd front pnpm install ``` 启动前端: ```bash pnpm dev ``` 前端默认运行在: ```text http://127.0.0.1:5173 ``` 前端路由: ```text /login 静态登录页 /temples 寺院管理 /products 商品管理 /orders 订单管理 /ritual-services 法事管理 /users 用户管理 /todos Todo 管理占位页 ``` Vite 会把前端请求 `/api/users` 代理到后端: ```text http://127.0.0.1:8000/users ``` 如果后端端口不是 `8000`,可以启动前端时指定: ```bash VITE_API_TARGET=http://127.0.0.1:8010 pnpm dev ``` 前端应用名默认是“自在福田”,可以启动时覆盖: ```bash VITE_APP_NAME=自在福田 pnpm dev ```