jinhongtl/lottery
Composer 安装命令:
composer require jinhongtl/lottery
包简介
Lottery framework
关键字:
README 文档
README
这是一个通用抽奖框架,连接不同的组件以支持抽奖
安装
php composer.phar require jinhongtl/lottery
使用
- 创建一个类实现lottery\LotteryInterface接口
- new一个抽奖类(lottery\Lottery),通过run()方法进行抽奖
- 配置抽奖算法(有默认抽奖算法,可省略)
自定义抽奖逻辑
通过实现lottery\LotteryInterface接口中的方法,来自定义自己的抽奖逻辑
<?php
namespace micro\controllers;
use lottery\LotteryInterface;
use lottery\Prize;
use Yii;
class RushLottery implements LotteryInterface
{
/** 进入抽奖前的条件校验*/
public static function check($userId, $activityId): bool
{
return true;
}
/** 是否允许中奖,包括总抽奖概率与用户中奖次数限制的校验 */
public static function checkLimit($userId, $activityId): bool
{
return true;
}
/** 获取满足抽奖条件的奖品 */
public static function getPrizes($userId, $activityId): array
{
$prizesDb = [
[
'id' => 1,
'name' => '红包1元',
'chance' => 0,
],
[
'id' => 2,
'name' => '耳机1个',
'chance' => 0,
],
];
$prizes = [];
foreach ($prizesDb as $prize) {
$prizes[] = Yii::createObject([
'class' => Prize::class,
'id' => $prize['id'],
'name' => $prize['name'],
'chance' => $prize['chance'],
]);
}
return $prizes;
}
/** 扣除抽奖机会 */
public static function deductionLotteryNum($userId, $activityId): bool
{
return true;
}
/** 预抽奖逻辑 */
public static function preLottery($userId, $activityId): bool
{
return true;
}
/** 如写入奖品 */
public static function afterLottery($userId, $activityId, Prize $prize)
{
}
}
使用
$userId = 1;
$activityId = 1;
try {
$lottery = new Lottery(new RushLottery());
$prize = $lottery->run($userId, $activityId);
return $prize;
} catch (UnLuckyException $ex) {
return ['code' => 1000, 'message' => '没抽中,下次再抽~'];
} catch (Exception $ex) {
return ['code' => 1000, 'message' => '您与奖品擦肩而过~'];
}
自定义抽奖算法
jinhongtl/lottery 适用场景与选型建议
jinhongtl/lottery 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 4, 最近一次更新时间为 2022 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「helper」 「lottery」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jinhongtl/lottery 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jinhongtl/lottery 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jinhongtl/lottery 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Datetime helpers for timezone handling
Library with classes to help you do batch.
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
HTML and form generation
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-15