channels.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. use App\Models\Node;
  3. use App\Models\Payment;
  4. use Illuminate\Support\Facades\Broadcast;
  5. /*
  6. |--------------------------------------------------------------------------
  7. | Broadcast Channels
  8. |--------------------------------------------------------------------------
  9. |
  10. | Here you may register all of the event broadcasting channels that your
  11. | application supports. The given channel authorization callbacks are
  12. | used to check if an authenticated user can listen to the channel.
  13. |
  14. */
  15. // 支付状态更新频道
  16. Broadcast::channel('payment-status.{tradeNo}', static function ($user, $tradeNo) {
  17. // 检查订单是否属于该用户
  18. return $user->id === Payment::whereTradeNo($tradeNo)->first()?->user->id;
  19. });
  20. // 节点相关操作频道
  21. Broadcast::channel('node.{type}.{nodeId}', static function ($user, $type, $nodeId) {
  22. // 验证用户权限和节点访问权限
  23. if (! $user->can("admin.node.$type")) {
  24. return false;
  25. }
  26. // 如果是特定节点操作,验证节点存在性和访问权限
  27. if ($nodeId !== 'all') {
  28. return Node::where('id', $nodeId)->exists();
  29. }
  30. return true;
  31. });