Base.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\bbc\controller;
  4. use think\App;
  5. use think\exception\HttpResponseException;
  6. use think\Response;
  7. class Base extends \app\admin\controller\Base
  8. {
  9. public function __construct(App $app) {parent::__construct($app);}
  10. /**
  11. * @param string $message
  12. * @param int $code
  13. * @param null $data
  14. */
  15. public function error($message='',$code=1003,$data=null){
  16. $this->result($message,$data,$code);
  17. }
  18. /**
  19. * @param string $message
  20. * @param int $code
  21. * @param null $data
  22. */
  23. public function success($message='',$data=null,$code=0){
  24. $this->result($message,$data,$code);
  25. }
  26. /**
  27. * @param string $msg
  28. * @param null $data
  29. * @param int $code
  30. * @param string|null $type
  31. * @param array $header
  32. * @param array $options
  33. */
  34. private function result(string $msg, $data = null, int $code = 0, string $type = "json", array $header = [], array
  35. $options = [])
  36. {
  37. $result = [
  38. 'code' => $code,
  39. 'message' => $msg,
  40. 'data' => $data,
  41. ];
  42. $code = 200;
  43. if (isset($header['statuscode'])) {
  44. $code = $header['statuscode'];
  45. unset($header['statuscode']);
  46. }
  47. $response = Response::create($result, $type, $code)->header($header)->options($options);
  48. throw new HttpResponseException($response);
  49. }
  50. }