Files
webman-buildadmin/app/common/service/UserPushService.php
zhenhui 24aab111b5 1.优化开奖和推送
2.新增控制连续开奖赔率
2026-04-20 10:02:27 +08:00

65 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\common\service;
use support\think\Db;
use Throwable;
use Webman\Push\Api;
/**
* 用户私有频道推送private-user-{uuid}(与移动端接口设计草案 7.1 一致)
*/
final class UserPushService
{
public const EVT_BET_ACCEPTED = 'bet.accepted';
/** 单注开奖结果(含未中奖 win_amount=0 */
public const EVT_BET_SETTLED = 'bet.settled';
public const EVT_WALLET_CHANGED = 'wallet.changed';
/** 命中配置为「大奖」的连胜档派彩(私有频道) */
public const EVT_JACKPOT_HIT = 'jackpot.hit';
private static function channelName(string $uuid): string
{
return 'private-user-' . $uuid;
}
private static function createApi(): Api
{
return new Api(
str_replace('0.0.0.0', '127.0.0.1', (string) config('plugin.webman.push.app.api')),
(string) config('plugin.webman.push.app.app_key'),
(string) config('plugin.webman.push.app.app_secret')
);
}
public static function uuidForUserId(int $userId): ?string
{
if ($userId <= 0) {
return null;
}
$u = Db::name('user')->where('id', $userId)->value('uuid');
return is_string($u) && $u !== '' ? $u : null;
}
/**
* @param array<string, mixed> $data
*/
public static function publish(int $userId, string $event, array $data): void
{
$uuid = self::uuidForUserId($userId);
if ($uuid === null) {
return;
}
try {
self::createApi()->trigger(self::channelName($uuid), $event, $data);
} catch (Throwable) {
}
}
}