api/README.md
2025-04-09 10:49:02 +08:00

104 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 美搭 (Meida) API 服务
一个基于FastAPI构建的API服务项目。
## 项目结构
```
meida-api/
├── app/ # 应用目录
│ ├── api/ # API相关模块
│ │ └── v1/ # API版本1
│ ├── core/ # 核心配置
│ ├── db/ # 数据库相关
│ ├── models/ # 数据模型
│ │ └── users.py # 用户模型
│ ├── schemas/ # 数据验证模式
│ │ └── user.py # 用户数据模式
│ └── services/ # 业务服务层
│ └── user.py # 用户服务
├── main.py # 应用入口
├── init_db.py # 数据库初始化脚本
└── requirements.txt # 项目依赖
```
## 安装与运行
1. 创建虚拟环境 (推荐)
```bash
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或
venv\Scripts\activate # Windows
```
2. 安装依赖
```bash
pip install -r requirements.txt
```
3. 初始化数据库 (如果需要)
```bash
python init_db.py
```
4. 运行服务
```bash
python main.py
```
```bash
uvicorn main:app --reload
```
5. 访问API文档
- Swagger文档: http://localhost:8000/docs
- ReDoc文档: http://localhost:8000/redoc
## API端点
### 基础端点
- `/health` - 健康检查
### V1 API
- `/api/v1/` - API基本信息
- `/api/v1/products` - 获取产品列表
- `/api/v1/products/{product_id}` - 获取特定产品详情
### 用户API
- `/api/v1/users/` - 获取所有用户 (GET) / 创建用户 (POST)
- `/api/v1/users/{user_id}` - 获取/更新/删除特定用户
- `/api/v1/users/openid/{openid}` - 通过openid获取用户
- `/api/v1/users/me` - 获取当前登录用户信息 (需要认证)
- `/api/v1/users/me` - 更新当前登录用户信息 (需要认证)
### 认证API
- `/api/v1/auth/login/wechat` - 微信登录/注册
## 认证
本API使用JWT令牌进行认证。认证流程如下
1. 调用微信登录接口获取令牌:
```
POST /api/v1/auth/login/wechat
{
"code": "微信临时登录凭证"
}
```
2. 在需要认证的请求头中添加令牌:
```
Authorization: Bearer <access_token>
```
## 数据模型
### 用户模型
- id: 自增长主键
- openid: 用户唯一标识
- unionid: 统一标识
- avatar: 用户头像
- nickname: 用户昵称
- create_time: 创建时间