基于 Vercel Serverless Functions 构建的后端服务,用于将学习记录同步到飞书多维表格。
- ✅ 接收前端 POST 请求,批量写入学习记录
- ✅ 自动获取飞书 tenant_access_token
- ✅ 逐条写入飞书多维表格(带限流保护)
- ✅ 内置错误重试机制
- ✅ 完整的 CORS 支持(允许前端跨域调用)
- ✅ 清晰的同步结果统计
./学习数据后端/
├── api/
│ ├── sync.js # 主同步接口
│ └── hello.js # 测试接口
├── package.json # 项目依赖
├── vercel.json # Vercel 配置
└── README.md # 本文档
- 访问 飞书开放平台
- 创建企业自建应用
- 获取
App ID和App Secret - 开启权限:
bitable:app- 多维表格读写权限auth:tenant_access_token- 获取租户访问令牌
- 在飞书创建多维表格
- 获取:
Base Token:从表格 URL 中获取Table ID:从表格 URL 中获取
- 创建字段(字段名必须与下面一致):
日期- 日期类型主题- 多行文本学习时长(分钟)- 数字标签- 多选笔记- 多行文本
- 访问 vercel.com
- 使用 GitHub/GitLab 账号登录
- 在 Vercel 控制台点击 "Add New" → "Project"
- 连接你的 GitHub 账号
- 导入包含本项目的仓库
在 Vercel 项目设置 → Environment Variables 中添加:
| 变量名 | 说明 | 示例 |
|---|---|---|
FEISHU_APP_ID |
飞书应用 ID | cli_xxxxxxxxxxxxxx |
FEISHU_APP_SECRET |
飞书应用密钥 | xxxxxxxxxxxxxx |
FEISHU_BASE_TOKEN |
飞书表格 Base Token | BbcKb4xxxxxxxxx |
FEISHU_TABLE_ID |
飞书表格 Table ID | tblxxxxxxxxxxxx |
- 在 Vercel 项目中点击 "Deploy"
- 等待部署完成(通常 1-2 分钟)
部署成功后,你将获得类似这样的地址:
https://your-project-name.vercel.app
接口地址:
- 测试接口:
https://your-project-name.vercel.app/api/hello - 同步接口:
https://your-project-name.vercel.app/api/sync
GET /api/hello返回示例:
{
"success": true,
"message": "学习数据同步 API 运行正常!",
"timestamp": "2024-01-01T12:00:00.000Z"
}POST /api/sync
Content-Type: application/json
{
"records": [
{
"date": "2024-01-01",
"topic": "React Hooks 学习",
"duration": 60,
"tags": ["React", "前端"],
"notes": "学习了 useState 和 useEffect"
},
{
"date": "2024-01-02",
"topic": "Node.js 异步编程",
"duration": 90,
"tags": ["Node.js", "后端"],
"notes": "Promise 和 async/await"
}
]
}返回示例:
{
"success": true,
"message": "同步完成",
"total": 2,
"success": 2,
"failed": 0,
"errors": []
}// 前端调用示例
const syncToFeishu = async (records) => {
const response = await fetch('https://your-project-name.vercel.app/api/sync', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ records })
});
const result = await response.json();
console.log('同步结果:', result);
return result;
};
// 使用示例
syncToFeishu([
{
date: '2024-01-01',
topic: '学习主题',
duration: 60,
tags: ['标签1', '标签2'],
notes: '学习笔记'
}
]);- 权限配置:确保飞书应用已获得多维表格的读写权限
- 限流保护:接口内置 200ms 间隔限流,避免触发飞书 API 限制
- 重试机制:单条记录写入失败会自动重试最多 3 次
- 环境变量:不要将敏感信息提交到代码仓库,通过 Vercel 环境变量配置
检查项:
- 环境变量是否正确配置
- 飞书应用权限是否开启
- 飞书表格字段名是否完全匹配
原因:触发了飞书 API 限流 解决:接口已内置重试机制,通常会自动恢复
检查:vercel.json 中的 headers 配置是否正确,确保有 Access-Control-Allow-Origin: *
- Vercel Serverless Functions - 无服务器后端
- 飞书开放平台 API - 多维表格存储
- Node.js - 运行环境
MIT