AsyncResponse.php 635 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Components;
  3. use Illuminate\Support\Facades\Gate;
  4. trait AsyncResponse{
  5. protected function gate(string $authorityName)
  6. {
  7. if(!Gate::allows($authorityName)){
  8. echo json_encode(["success"=>false,"data"=>"无权操作"]);
  9. exit();
  10. }
  11. }
  12. public function error(string $message)
  13. {
  14. echo json_encode(["success"=>false,"data"=>$message]);
  15. exit();
  16. }
  17. public function success($message = null)
  18. {
  19. $result = ["success"=>true];
  20. if ($message)$result["data"] = $message;
  21. echo json_encode($result);
  22. exit();
  23. }
  24. }