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.

73 lines
1.8 KiB

5 years ago
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 App\Service\v3\Interfaces\HelperServiceInterface;
  13. use Hyperf\HttpServer\Contract\ResponseInterface;
  14. use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
  15. use Hyperf\HttpMessage\Stream\SwooleStream;
  16. use Hyperf\Di\Annotation\Inject;
  17. class BaseController extends AbstractController
  18. {
  19. /**
  20. * @Inject
  21. * @var HelperServiceInterface
  22. */
  23. protected $helperService;
  24. public function w7result($statusCode,$data = ''){
  25. return $this->response
  26. ->withHeader('Content-Type', 'application/text')
  27. ->withStatus($statusCode)
  28. ->withBody(new SwooleStream($data));
  29. }
  30. public function result($code, $data, $message = '成功'):Psr7ResponseInterface
  31. {
  32. $status = 'ok';
  33. if($code>0){
  34. $status = 'error';
  35. }
  36. $content = [
  37. "status"=>$status,
  38. "code" => $code,
  39. "result" => $data ? collect($data)->toArray() : [],
  40. "message" => $message
  41. ];
  42. return $this->response->json($content);
  43. }
  44. public function success($data, $message = '成功')
  45. {
  46. return $this->result(0,$data, $message);
  47. }
  48. /**
  49. * 请求参数判空
  50. * null&'null' / false&&'false' / '' / 'undefined' / 'unknown'
  51. * @param mixed $var 参数
  52. */
  53. public function empty($var)
  54. {
  55. return ! (
  56. isset($var)
  57. && $var
  58. && !empty($var)
  59. && !is_null($var)
  60. && $var != 'undefined'
  61. && $var != 'unknown'
  62. && $var != 'null'
  63. && $var != 'false'
  64. );
  65. }
  66. }