channels.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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. Broadcast::channel('payment-status.{tradeNo}', static function ($user, $tradeNo) {
  16. // 检查订单是否属于该用户
  17. return $user->id === Payment::whereTradeNo($tradeNo)->first()?->user->id;
  18. });
  19. Broadcast::channel('node.{type}.{nodeId}', static function ($user, $type, $nodeId) {
  20. // 验证用户权限和节点访问权限
  21. if (! $user->can("admin.node.$type")) {
  22. return false;
  23. }
  24. // 如果是特定节点操作,验证节点存在性和访问权限
  25. if ($nodeId !== 'all') {
  26. return Node::where('id', $nodeId)->exists();
  27. }
  28. return true;
  29. });