You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://doc.hyperf.io
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Controller;
  12. use Hyperf\HttpServer\Contract\ResponseInterface;
  13. use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
  14. use Hyperf\HttpMessage\Stream\SwooleStream;
  15. class BaseController extends AbstractController
  16. {
  17. public function w7result($statusCode,$data = ''){
  18. return $this->response
  19. ->withHeader('Content-Type', 'application/text')
  20. ->withStatus($statusCode)
  21. ->withBody(new SwooleStream($data));
  22. }
  23. public function result($code, $data, $message = '成功'):Psr7ResponseInterface
  24. {
  25. $status = 'ok';
  26. if($code>0){
  27. $status = 'error';
  28. }
  29. $content = [
  30. "status"=>$status,
  31. "code" => $code,
  32. "result" => $data ? collect($data)->toArray() : [],
  33. "message" => $message
  34. ];
  35. return $this->response->json($content);
  36. }
  37. public function success($data, $message = '成功')
  38. {
  39. return $this->result(0,$data, $message);
  40. }
  41. /**
  42. * 请求参数判空
  43. * null&'null' / false&&'false' / '' / 'undefined' / 'unknown'
  44. * @param mixed $var 参数
  45. */
  46. public function empty($var)
  47. {
  48. return ! (
  49. isset($var)
  50. && $var
  51. && !empty($var)
  52. && !is_null($var)
  53. && $var != 'undefined'
  54. && $var != 'unknown'
  55. && $var != 'null'
  56. && $var != 'false'
  57. );
  58. }
  59. }