PHPackages                             lzpeng/php-prize-drawer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. lzpeng/php-prize-drawer

ActiveLibrary

lzpeng/php-prize-drawer
=======================

抽奖组件

1.0.1(6y ago)119MITPHPPHP &gt;=7.0CI failing

Since Apr 15Pushed 6y ago1 watchersCompare

[ Source](https://github.com/liuzhanpeng/php-prize-drawer)[ Packagist](https://packagist.org/packages/lzpeng/php-prize-drawer)[ RSS](/packages/lzpeng-php-prize-drawer/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (4)Used By (0)

php-prize-drawer
================

[](#php-prize-drawer)

抽奖组件, 适应日常开发中的各种抽奖场景。

特点
--

[](#特点)

- 方便扩展以支持各种奖品配置来源，如本地数组、数据库、甚至第三方接口
- 支持多种奖品类型：Dummy(“假”的奖品，如“谢谢参与”)、Normal(普通奖品，用户线下兑奖)、扩展型奖品（需要调用第三方接口的，如红包、话费充值等)
- 方便通过配置实现多种奖品分配规则，如 在指定时间才能抽中“真”奖品、按天数平均分配奖品数量等
- 支持多种抽奖策略，如随机返回一个奖品、按机率返回一个奖品等
- 高度抽象、方便扩展

环境需求
----

[](#环境需求)

- php &gt;=7.0

安装
--

[](#安装)

composer require "lzpeng/php-prize-drawer:1.0.\*"

使用方式
----

[](#使用方式)

```
use Lzpeng\PrizeDrawer\Factory;

// 通过工厂类创建抽奖组件实例
$drawer = Factory::create([
    'provider' => [ // 奖品配置提供器
        'driver' => 'array',
        'params' => [...],
    ],
    'accessor' => [ // 奖品数量存取器
        'driver' => 'model',
        'params' => [...]
    ],
    'strategy' => [ // 抽奖策略
        'driver' => 'chance',
        'params' => [...]
    ],
    'filters' => [ // 奖品过滤器
        'time_limit' => [ // 在指定时间内才返回"真"奖品，否则返回"假"奖品
            'start_time' => '09:00',
            'end_time' => '17:00',
        ],
        'avg_by_days' => [ // 按天数比均分配奖品数量
            'start_date' => '2020-04-01',
            'end_date' => '2020-04-08',
        ]
    ],
    'events' => [
        'draw_after' => [ // 抽奖后的事件
            LoggerListener::class,
            NotifyListener::class,
        ]
    ]
]);

// 设置当前用户
// $drawer->setUser(new User($id, ['phone' => 13800138000]));
$drawer->setUser($openid);

// 可动态添加过滤器, 但一般情况下通过配置设置就可以了
// $drawer->addFilter($name, $filter);
// $drawer->removeFilter($name);

// 可动态添加/移除监听器, 但一般情况下通过配置设置就可以了
// $drawer->addListener('draw_after', $listener);
// $drawer->removeListener('draw_after', $listener = null);

$conn->beginTransaction(); // 一般情况下，都会使用数据库，为保证并发，需要使用事务
try {
    $prize = $drawer->draw(); // 抽奖, 返回一个奖品
    // do somethings
    $conn->commit();
} catch(Exception $ex) {
    $conn->rollBack();
    // handle exception
}

// 返回奖品列表
$prizes = $drawer->prizes();
```

扩展
--

[](#扩展)

### 自定义抽奖策略

[](#自定义抽奖策略)

1. 实现接口 \\Lzpeng\\PrizeDrawer\\Contracts\\StrategyInterface
2. 注册 Factory::registerStrategy('策略名称', function($params) { return new CustomStrategy($params); });

### 自定义过滤器

[](#自定义过滤器)

1. 实现接口 \\Lzpeng\\PrizeDrawer\\Contracts\\PrizesFilterInterface
2. 注册 Factory::registerPrizesFilter('过滤器名称', function($params) { return new CustomPrizesFilter($params); });

### 自定义奖品配置提供器

[](#自定义奖品配置提供器)

1. 实现接口 \\Lzpeng\\PrizeDrawer\\Contracts\\PrizesConfigProviderInterface
2. 注册 Factory::registerPrizesConfigProvider('提供器名称', function($params) { return new CustomPrizesConfigProvider($params); });

### 自定义已抽数量存取器

[](#自定义已抽数量存取器)

1. 实现接口\\Lzpeng\\PrizeDrawer\\Contracts\\PrizeQuantiyOfDrawAccessorInterface
2. 注册 Factory::registerPrizeQuantiyOfDrawAccessor('存取器名称', function($params) { return new CustomPrizeQuantiyOfDrawAccessor($params); });

License
-------

[](#license)

MIT

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

2

Last Release

2210d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b085c9e9145b16a586a1574c9ece3fc1793c14c0d120f9f3c74b301b19165925?d=identicon)[liuzhanpeng](/maintainers/liuzhanpeng)

---

Top Contributors

[![liuzhanpeng](https://avatars.githubusercontent.com/u/6972018?v=4)](https://github.com/liuzhanpeng "liuzhanpeng (29 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lzpeng-php-prize-drawer/health.svg)

```
[![Health](https://phpackages.com/badges/lzpeng-php-prize-drawer/health.svg)](https://phpackages.com/packages/lzpeng-php-prize-drawer)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
