前端接入指南 邮箱密码
1. 请求头配置
所有请求需携带:
Content-Type: application/json
x-app-id: 你的AppID
x-app-secret: 你的AppSecret
Authorization: Bearer token // 仅注销账号需要
2. API 接口
| 接口 | 方法 | 参数 |
| /api/v1/auth/register | POST | email, password(6-16位) |
| /api/v1/auth/login | POST | email, password |
| /api/v1/auth/forgot-password | POST | email |
| /api/v1/auth/reset-password | POST | token, newPassword |
| /api/v1/auth/account | DELETE | 需Authorization头 |
3. 代码示例
// JavaScript / Web
const API = 'https://auth.shangkeai.com';
const headers = {
'Content-Type': 'application/json',
'x-app-id': 'YOUR_APP_ID',
'x-app-secret': 'YOUR_APP_SECRET'
};
// 注册
const register = async (email, password) => {
const res = await fetch(`${API}/api/v1/auth/register`, {
method: 'POST', headers,
body: JSON.stringify({ email, password })
});
return res.json(); // { message, user, token }
};
// 登录
const login = async (email, password) => {
const res = await fetch(`${API}/api/v1/auth/login`, {
method: 'POST', headers,
body: JSON.stringify({ email, password })
});
return res.json(); // { message, user, token }
};
// 注销账号
const deleteAccount = async (token) => {
const res = await fetch(`${API}/api/v1/auth/account`, {
method: 'DELETE',
headers: { ...headers, 'Authorization': `Bearer ${token}` }
});
return res.json();
};
// Flutter - pubspec.yaml 添加: http: ^1.1.0
import 'package:http/http.dart' as http;
import 'dart:convert';
class AuthService {
static const _baseUrl = 'https://auth.shangkeai.com';
static const _appId = 'YOUR_APP_ID';
static const _appSecret = 'YOUR_APP_SECRET';
static Map<String, String> get _headers => {
'Content-Type': 'application/json',
'x-app-id': _appId,
'x-app-secret': _appSecret,
};
// 注册
static Future<Map> register(String email, String password) async {
final res = await http.post(
Uri.parse('$_baseUrl/api/v1/auth/register'),
headers: _headers,
body: jsonEncode({'email': email, 'password': password}),
);
return jsonDecode(res.body);
}
// 登录
static Future<Map> login(String email, String password) async {
final res = await http.post(
Uri.parse('$_baseUrl/api/v1/auth/login'),
headers: _headers,
body: jsonEncode({'email': email, 'password': password}),
);
return jsonDecode(res.body);
}
// 注销账号
static Future<Map> deleteAccount(String token) async {
final res = await http.delete(
Uri.parse('$_baseUrl/api/v1/auth/account'),
headers: {..._headers, 'Authorization': 'Bearer $token'},
);
return jsonDecode(res.body);
}
}
# 注册
curl -X POST https://auth.shangkeai.com/api/v1/auth/register \
-H "Content-Type: application/json" \
-H "x-app-id: YOUR_APP_ID" \
-H "x-app-secret: YOUR_APP_SECRET" \
-d '{"email":"user@example.com","password":"123456"}'
# 登录
curl -X POST https://auth.shangkeai.com/api/v1/auth/login \
-H "Content-Type: application/json" \
-H "x-app-id: YOUR_APP_ID" \
-H "x-app-secret: YOUR_APP_SECRET" \
-d '{"email":"user@example.com","password":"123456"}'
# 注销账号
curl -X DELETE https://auth.shangkeai.com/api/v1/auth/account \
-H "x-app-id: YOUR_APP_ID" \
-H "x-app-secret: YOUR_APP_SECRET" \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
4. 响应格式
| 场景 | 响应 |
| 成功 | {"message": "...", "user": {"id","email"}, "token": "..."} |
| 失败 | {"error": "错误信息"} |
多应用隔离:每个 App 使用独立的 app_id,用户数据完全隔离。同一邮箱可在不同 App 分别注册。
短信验证码登录 手机号验证码
1. 请求头配置
所有请求需携带应用凭证:
Content-Type: application/json
x-app-id: 你的AppID
x-app-secret: 你的AppSecret
Authorization: Bearer token // 仅注销账号需要
2. 已注册应用
| 应用名称 | App ID |
| 元智播 | app-yuanzhibo |
| 它从哪里来 | app-whereishefrom |
| 历史之声 | app-history-voice |
| 趣学AI化学实验室 | app-ai-funchem |
3. API 接口
| 接口 | 方法 | 参数 | 说明 |
| /api/v1/auth/send-code |
POST |
phone (11位手机号) |
发送6位验证码,有效期5分钟 |
| /api/v1/auth/login |
POST |
phone, code (6位验证码) |
验证码登录,自动注册新用户 |
| /api/v1/auth/account |
DELETE |
需Authorization头 |
永久删除账号 |
| /health |
GET |
无 |
健康检查(无需认证) |
4. 发送验证码
向手机号发送6位数字验证码,同一手机号60秒内只能发送一次。
// 发送验证码
const sendCode = async (phone) => {
const res = await fetch('https://auth.shangkeai.com/api/v1/auth/send-code', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-app-id': 'app-yuanzhibo',
'x-app-secret': 'secret_yuanzhibo_2024_metacast'
},
body: JSON.stringify({ phone })
});
return res.json();
// 成功: { "message": "验证码已发送" }
// 失败: { "error": "手机号格式不正确" }
};
// 验证码登录(自动注册)
const loginWithCode = async (phone, code) => {
const res = await fetch('https://auth.shangkeai.com/api/v1/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-app-id': 'app-yuanzhibo',
'x-app-secret': 'secret_yuanzhibo_2024_metacast'
},
body: JSON.stringify({ phone, code })
});
return res.json();
// 成功: { "message": "登录成功", "user": { "id": 1, "phone": "138****5678" }, "token": "eyJ...", "isNewUser": false }
};
# 发送验证码
curl -X POST https://auth.shangkeai.com/api/v1/auth/send-code \
-H "Content-Type: application/json" \
-H "x-app-id: app-yuanzhibo" \
-H "x-app-secret: secret_yuanzhibo_2024_metacast" \
-d '{"phone": "13812345678"}'
# 验证码登录
curl -X POST https://auth.shangkeai.com/api/v1/auth/login \
-H "Content-Type: application/json" \
-H "x-app-id: app-yuanzhibo" \
-H "x-app-secret: secret_yuanzhibo_2024_metacast" \
-d '{"phone": "13812345678", "code": "123456"}'
# 注销账号
curl -X DELETE https://auth.shangkeai.com/api/v1/auth/account \
-H "x-app-id: app-yuanzhibo" \
-H "x-app-secret: secret_yuanzhibo_2024_metacast" \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
# 健康检查
curl https://auth.shangkeai.com/health
5. 响应格式
发送验证码成功
{ "message": "验证码已发送" }
登录成功
{
"message": "登录成功", // 或 "注册成功"(新用户)
"user": {
"id": 1,
"phone": "13812345678"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"isNewUser": false // true 表示新注册用户
}
错误响应
| 状态码 | 错误信息 | 说明 |
| 400 | 手机号格式不正确 | 手机号不符合11位格式 |
| 400 | 发送过于频繁,请60秒后再试 | 同一手机号60秒内重复请求 |
| 400 | 请先获取验证码 | 该手机号未请求过验证码 |
| 400 | 验证码错误 | 验证码不匹配 |
| 400 | 验证码已过期,请重新获取 | 验证码超过5分钟有效期 |
| 401 | Missing app credentials | 缺少 x-app-id 或 x-app-secret |
| 401 | Invalid app credentials | 应用凭证无效 |
| 401 | No token provided | 注销账号时未提供Token |
| 401 | Invalid token | Token无效或已过期 |
6. 速率限制
| 限制类型 | 限制值 | 说明 |
| API请求频率 | 100次/15分钟 | 单IP所有API接口总请求数 |
| 验证码发送 | 1次/60秒 | 同一手机号验证码发送间隔 |
| 验证码有效期 | 5分钟 | 验证码过期后需重新获取 |
| Token有效期 | 7天 | JWT Token过期后需重新登录 |
响应Header:每次请求响应会包含 X-RateLimit-Limit、X-RateLimit-Remaining、X-RateLimit-Reset 头信息,用于监控请求配额。
多应用隔离:每个 App 使用独立的 app_id,用户数据完全隔离。同一手机号可在不同 App 分别注册。