> For the complete documentation index, see [llms.txt](https://polyv.gitbook.io/document/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polyv.gitbook.io/document/docs/live/js/new-sdk/interactions-launch-sdk/sdk/lottery.md).

# 抽奖

## 功能概述

允许开始前对抽奖进行奖品、中奖人数、中奖人员等设置，完成设置后可以开始抽奖，结束抽奖后可以查看中奖结果等信息。

* 本功能只支持讲师、嘉宾、助教、管理员这四种角色进行抽奖的发起和停止。
* 支持自定义设置中奖用户信息采集字段。
* 支持设置预设中奖用户。

## 代码示例

**注意**，在开始以下步骤前需要先进行全局初始化设置。可[参考文档](https://help.polyv.net/index.html#/live/js/new_sdk/interactions_launch_sdk/sdk/overview)。

### 基本流程

```javascript
const app = new Lottery();

// 订阅事件监听。
app
  .on('LotteryStart', () => {})
  .on('LotteryEnd', () => {})
  // UpdateInLottery 会更新 app.InLottery 最新抽奖状态
  .on('UpdateInLottery', () => {});

// 开始抽奖前
// 获取可参与抽奖的在线人数
app.checkedUserCount(lotteryRange);
// 获取中奖人信息收集配置
app.getCollectInfo();

// 发起抽奖。当抽奖状态变化后会触发 `updateInLottery` 事件，更新抽奖状态。
app.createLottery(createOptions);

// 结束抽奖。
app.endLottery(lotteryId);
// 获取中奖名单
app.getWinnersList(page, lotteryId);
```
