|
|
- # 小程序登录API接口文档
-
- ## 概述
- 本文档描述小程序登录模块的API接口,包括登录、用户管理、token管理等功能。
-
- ## 基础信息
- - **基础URL**: `http://your-domain/applet/login`
- - **请求方式**: GET/POST
- - **数据格式**: JSON
- - **字符编码**: UTF-8
-
- ## 通用响应格式
- ```json
- {
- "success": true,
- "message": "操作成功",
- "code": 200,
- "result": {}
- }
- ```
-
- ## 接口列表
-
- ### 1. 微信小程序登录
-
- **接口地址**: `POST /wxLogin`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | code | String | 是 | 微信登录code |
-
- **请求示例**:
- ```bash
- curl -X POST "http://your-domain/applet/login/wxLogin" \
- -H "Content-Type: application/x-www-form-urlencoded" \
- -d "code=wx_login_code_here"
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "登录成功",
- "code": 200,
- "result": {
- "token": "eyJhbGciOiJIUzI1NiJ9...",
- "userInfo": {
- "id": "applet_user_123",
- "name": "微信用户12345678",
- "openid": "wx_openid_123",
- "phone": "13800138000",
- "avatar": "https://example.com/avatar.jpg",
- "bmi": 22.5,
- "fat": 15.2
- },
- "openid": "wx_openid_123",
- "sessionKey": "session_key_123"
- }
- }
- ```
-
- ### 2. 获取用户手机号
-
- **接口地址**: `POST /getPhoneNumber`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | code | String | 是 | 手机号获取code |
-
- **请求示例**:
- ```bash
- curl -X POST "http://your-domain/applet/login/getPhoneNumber" \
- -H "Content-Type: application/x-www-form-urlencoded" \
- -d "code=phone_code_here"
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "获取成功",
- "code": 200,
- "result": "13800138000"
- }
- ```
-
- ### 3. 绑定手机号到用户
-
- **接口地址**: `POST /bindPhoneNumber`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | token | String | 是 | 用户token |
- | phoneCode | String | 是 | 手机号获取code |
-
- **请求示例**:
- ```bash
- curl -X POST "http://your-domain/applet/login/bindPhoneNumber" \
- -H "Content-Type: application/x-www-form-urlencoded" \
- -d "token=user_token_here&phoneCode=phone_code_here"
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "绑定成功",
- "code": 200,
- "result": "绑定成功"
- }
- ```
-
- ### 4. 刷新token
-
- **接口地址**: `POST /refreshToken`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | token | String | 是 | 原token |
-
- **请求示例**:
- ```bash
- curl -X POST "http://your-domain/applet/login/refreshToken" \
- -H "Content-Type: application/x-www-form-urlencoded" \
- -d "token=old_token_here"
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "刷新成功",
- "code": 200,
- "result": "eyJhbGciOiJIUzI1NiJ9..."
- }
- ```
-
- ### 5. 退出登录
-
- **接口地址**: `POST /logout`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | token | String | 是 | 用户token |
-
- **请求示例**:
- ```bash
- curl -X POST "http://your-domain/applet/login/logout" \
- -H "Content-Type: application/x-www-form-urlencoded" \
- -d "token=user_token_here"
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "退出成功",
- "code": 200,
- "result": "退出成功"
- }
- ```
-
- ### 6. 检查登录状态
-
- **接口地址**: `GET /checkLogin`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | token | String | 是 | 用户token |
-
- **请求示例**:
- ```bash
- curl -X GET "http://your-domain/applet/login/checkLogin?token=user_token_here"
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "登录有效",
- "code": 200,
- "result": {
- "id": "applet_user_123",
- "name": "微信用户12345678",
- "openid": "wx_openid_123",
- "phone": "13800138000",
- "avatar": "https://example.com/avatar.jpg",
- "bmi": 22.5,
- "fat": 15.2
- }
- }
- ```
-
- ### 7. 获取用户信息
-
- **接口地址**: `GET /getUserInfo`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | token | String | 是 | 用户token |
-
- **请求示例**:
- ```bash
- curl -X GET "http://your-domain/applet/login/getUserInfo?token=user_token_here"
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "获取成功",
- "code": 200,
- "result": {
- "id": "applet_user_123",
- "name": "微信用户12345678",
- "openid": "wx_openid_123",
- "phone": "13800138000",
- "avatar": "https://example.com/avatar.jpg",
- "bmi": 22.5,
- "fat": 15.2
- }
- }
- ```
-
- ### 8. 更新用户信息
-
- **接口地址**: `POST /updateUserInfo`
-
- **请求参数**:
- | 参数名 | 类型 | 必填 | 说明 |
- |--------|------|------|------|
- | token | String | 是 | 用户token |
- | userInfo | Object | 是 | 用户信息对象 |
-
- **请求示例**:
- ```bash
- curl -X POST "http://your-domain/applet/login/updateUserInfo" \
- -H "Content-Type: application/json" \
- -d '{
- "token": "user_token_here",
- "userInfo": {
- "name": "新昵称",
- "avatar": "https://example.com/new_avatar.jpg",
- "bmi": 23.0,
- "fat": 16.0
- }
- }'
- ```
-
- **响应示例**:
- ```json
- {
- "success": true,
- "message": "更新成功",
- "code": 200,
- "result": "更新成功"
- }
- ```
-
- ## 错误码说明
-
- | 错误码 | 说明 |
- |--------|------|
- | 200 | 成功 |
- | 400 | 请求参数错误 |
- | 401 | 未授权/token无效 |
- | 403 | 禁止访问 |
- | 404 | 资源不存在 |
- | 500 | 服务器内部错误 |
-
- ## 常见错误响应
-
- ### 1. token无效
- ```json
- {
- "success": false,
- "message": "token无效",
- "code": 401,
- "result": null
- }
- ```
-
- ### 2. 用户不存在
- ```json
- {
- "success": false,
- "message": "用户不存在",
- "code": 404,
- "result": null
- }
- ```
-
- ### 3. 微信登录失败
- ```json
- {
- "success": false,
- "message": "微信登录失败: code无效",
- "code": 400,
- "result": null
- }
- ```
-
- ### 4. 手机号已被绑定
- ```json
- {
- "success": false,
- "message": "该手机号已被其他用户绑定",
- "code": 400,
- "result": null
- }
- ```
-
- ## 使用流程
-
- ### 1. 小程序登录流程
- 1. 小程序端调用 `wx.login()` 获取code
- 2. 调用 `/wxLogin` 接口进行登录
- 3. 保存返回的token用于后续请求
-
- ### 2. 手机号绑定流程
- 1. 小程序端调用 `wx.getPhoneNumber()` 获取手机号code
- 2. 调用 `/getPhoneNumber` 接口获取手机号
- 3. 调用 `/bindPhoneNumber` 接口绑定手机号到用户
-
- ### 3. token管理流程
- 1. 定期调用 `/refreshToken` 刷新token
- 2. 退出时调用 `/logout` 接口
- 3. 使用 `/checkLogin` 检查登录状态
-
- ## 安全注意事项
-
- 1. **token安全**: token应该安全存储,不要暴露给第三方
- 2. **HTTPS**: 生产环境必须使用HTTPS
- 3. **参数验证**: 客户端应该验证所有参数
- 4. **错误处理**: 客户端应该妥善处理各种错误情况
- 5. **日志记录**: 重要操作应该记录日志
-
- ## 性能优化建议
-
- 1. **缓存**: 用户信息可以适当缓存
- 2. **连接池**: 数据库和Redis连接池配置合理
- 3. **异步处理**: 非关键操作可以使用异步处理
- 4. **监控**: 添加接口调用监控和性能指标
-
- ## 测试建议
-
- 1. **单元测试**: 为每个接口编写单元测试
- 2. **集成测试**: 测试完整的登录流程
- 3. **压力测试**: 测试高并发场景
- 4. **安全测试**: 测试各种异常情况
|