这是一个基于 Node.js 和 Express 开发的抽奖系统 API,支持创建抽奖活动、添加奖品、注册参与者、抽取获奖者等核心功能。
This is a lottery system API built with Node.js and Express, supporting core features such as creating lottery events, adding prizes, registering participants, and drawing winners.
- Node.js
- Express
- SQLite
- CORS
npm installnpm start服务器将在 http://localhost:3000 上运行。
The server will run on http://localhost:3000.
npm run dev- URL:
/health - 方法 / Method:
GET - 描述 / Description: 检查服务器是否正常运行 / Check if the server is running normally
- 响应示例 / Response Example:
{ "status": "ok", "message": "Lottery system is running" }
- URL:
/api/lotteries - 方法 / Method:
POST - 描述 / Description: 创建一个新的抽奖活动 / Create a new lottery event
- 请求体 / Request Body:
{ "name": "Test Lottery", "description": "A test lottery", "startTime": "2025-11-30T12:00:00Z", "endTime": "2025-12-01T12:00:00Z" } - 响应示例 / Response Example:
{ "id": 1, "name": "Test Lottery", "description": "A test lottery", "startTime": "2025-11-30T12:00:00Z", "endTime": "2025-12-01T12:00:00Z" }
- URL:
/api/lotteries - 方法 / Method:
GET - 描述 / Description: 获取所有抽奖活动列表 / Get all lottery events
- 响应示例 / Response Example:
[ { "id": 1, "name": "Test Lottery", "description": "A test lottery", "start_time": "2025-11-30T12:00:00Z", "end_time": "2025-12-01T12:00:00Z", "created_at": "2025-11-30T12:29:05Z" } ]
- URL:
/api/lotteries/:id - 方法 / Method:
GET - 描述 / Description: 获取指定抽奖活动的详细信息 / Get details of a specific lottery event
- 响应示例 / Response Example:
{ "id": 1, "name": "Test Lottery", "description": "A test lottery", "start_time": "2025-11-30T12:00:00Z", "end_time": "2025-12-01T12:00:00Z", "created_at": "2025-11-30T12:29:05Z", "prizes": [ { "id": 1, "lottery_id": 1, "name": "First Prize", "description": "Grand prize", "quantity": 1, "remaining": 0 } ], "participantsCount": 8 }
- URL:
/api/lotteries/:id - 方法 / Method:
DELETE - 描述 / Description: 删除指定的抽奖活动 / Delete a specific lottery event
- 响应示例 / Response Example:
{ "success": true, "message": "Lottery deleted successfully" }
- URL:
/api/lotteries/:lotteryId/prizes - 方法 / Method:
POST - 描述 / Description: 为指定抽奖活动添加奖品 / Add a prize to a specific lottery event
- 请求体 / Request Body:
{ "name": "First Prize", "description": "Grand prize", "quantity": 1 } - 响应示例 / Response Example:
{ "id": 1, "lotteryId": 1, "name": "First Prize", "description": "Grand prize", "quantity": 1, "remaining": 1 }
- URL:
/api/lotteries/:lotteryId/participants - 方法 / Method:
POST - 描述 / Description: 为指定抽奖活动注册参与者 / Register a participant for a specific lottery event
- 请求体 / Request Body:
{ "userId": "user1", "userName": "User One" } - 响应示例 / Response Example:
{ "success": true, "message": "Participant registered successfully" }
- URL:
/api/lotteries/:lotteryId/draw/:prizeId - 方法 / Method:
POST - 描述 / Description: 为指定抽奖活动的指定奖品抽取获奖者 / Draw a winner for a specific prize in a lottery event
- 响应示例 / Response Example:
{ "success": true, "winner": { "userId": "user1", "userName": "User One", "prizeId": 1, "prizeName": "First Prize" } }
- URL:
/api/lotteries/:lotteryId/winners - 方法 / Method:
GET - 描述 / Description: 获取指定抽奖活动的所有获奖者 / Get all winners for a specific lottery event
- 响应示例 / Response Example:
[ { "id": 1, "lottery_id": 1, "prize_id": 1, "user_id": "user1", "user_name": "User One", "won_at": "2025-11-30T12:29:05Z", "prize_name": "First Prize" } ]
- URL:
/api/prizes/:prizeId/winners - 方法 / Method:
GET - 描述 / Description: 获取指定奖品的所有获奖者 / Get all winners for a specific prize
- 响应示例 / Response Example:
[ { "id": 1, "lottery_id": 1, "prize_id": 1, "user_id": "user1", "user_name": "User One", "won_at": "2025-11-30T12:29:05Z", "prize_name": "First Prize" } ]
| 字段名 / Field | 类型 / Type | 描述 / Description |
|---|---|---|
| id | INTEGER | 主键,自增 / Primary key, auto-increment |
| name | TEXT | 抽奖活动名称 / Lottery name |
| description | TEXT | 抽奖活动描述 / Lottery description |
| start_time | DATETIME | 开始时间 / Start time |
| end_time | DATETIME | 结束时间 / End time |
| created_at | DATETIME | 创建时间 / Creation time |
| 字段名 / Field | 类型 / Type | 描述 / Description |
|---|---|---|
| id | INTEGER | 主键,自增 / Primary key, auto-increment |
| lottery_id | INTEGER | 关联的抽奖活动ID / Associated lottery ID |
| name | TEXT | 奖品名称 / Prize name |
| description | TEXT | 奖品描述 / Prize description |
| quantity | INTEGER | 奖品数量 / Prize quantity |
| remaining | INTEGER | 剩余奖品数量 / Remaining prizes |
| 字段名 / Field | 类型 / Type | 描述 / Description |
|---|---|---|
| id | INTEGER | 主键,自增 / Primary key, auto-increment |
| lottery_id | INTEGER | 关联的抽奖活动ID / Associated lottery ID |
| user_id | TEXT | 用户ID / User ID |
| user_name | TEXT | 用户名 / User name |
| created_at | DATETIME | 注册时间 / Registration time |
| 字段名 / Field | 类型 / Type | 描述 / Description |
|---|---|---|
| id | INTEGER | 主键,自增 / Primary key, auto-increment |
| lottery_id | INTEGER | 关联的抽奖活动ID / Associated lottery ID |
| prize_id | INTEGER | 关联的奖品ID / Associated prize ID |
| user_id | TEXT | 获奖者用户ID / Winner's user ID |
| user_name | TEXT | 获奖者用户名 / Winner's user name |
| won_at | DATETIME | 获奖时间 / Winning time |
node test-lottery.js测试脚本将自动测试抽奖系统的核心功能,包括创建抽奖活动、添加奖品、注册参与者、抽取获奖者等。
The test script will automatically test the core functions of the lottery system, including creating lottery events, adding prizes, registering participants, and drawing winners.
.
├── src/
│ ├── app.js # 主应用文件 / Main application file
│ ├── db.js # 数据库连接和初始化 / Database connection and initialization
│ ├── lotteryService.js # 抽奖业务逻辑 / Lottery business logic
│ └── routes.js # API 路由 / API routes
├── API-DOCS.md # API 文档 / API documentation
├── package.json # 项目配置 / Project configuration
├── test-lottery.js # 测试脚本 / Test script
└── lottery.db # SQLite 数据库文件 / SQLite database file
- 创建抽奖活动:支持设置抽奖名称、描述、开始时间和结束时间 / Create Lottery Events: Support setting lottery name, description, start time and end time
- 添加奖品:支持为抽奖活动添加多个奖品,每个奖品可设置名称、描述和数量 / Add Prizes: Support adding multiple prizes to a lottery, each with name, description and quantity
- 注册参与者:支持参与者注册,每个参与者只能注册一次 / Register Participants: Support participant registration, each participant can register only once
- 抽取获奖者:支持为指定奖品抽取获奖者,确保每个参与者只能获得一个相同奖品 / Draw Winners: Support drawing winners for specific prizes, ensuring each participant can win the same prize only once
- 查询功能:支持查询抽奖活动详情、参与者列表、获奖者列表等 / Query Functions: Support querying lottery details, participant lists, winner lists, etc.
- 事务处理:确保抽奖过程的原子性,避免数据不一致 / Transaction Processing: Ensure atomicity of the drawing process and avoid data inconsistency
- 抽奖活动只有在开始时间和结束时间之间才能注册参与者 / Lottery events only allow participant registration between start time and end time
- 每个参与者只能为每个抽奖活动注册一次 / Each participant can register for each lottery event only once
- 每个参与者可以获得不同奖品,但同一奖品只能获得一次 / Each participant can win different prizes, but can win the same prize only once
- 抽奖过程使用事务处理,确保数据一致性 / The drawing process uses transaction processing to ensure data consistency
- SQLite 数据库文件将自动创建在项目根目录下 / SQLite database file will be automatically created in the project root directory
- 添加用户认证和授权机制 / Add user authentication and authorization mechanisms
- 支持更多的抽奖算法(如权重抽奖) / Support more lottery algorithms (such as weighted lottery)
- 添加邮件或短信通知功能 / Add email or SMS notification functions
- 支持导出获奖者数据 / Support exporting winner data
- 添加更详细的日志记录 / Add more detailed logging
- 支持分布式部署 / Support distributed deployment
- 支持更多的数据库类型(如 MySQL、PostgreSQL) / Support more database types (such as MySQL, PostgreSQL) 待添加