65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?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) {
|
||
}
|
||
}
|
||
}
|