Base.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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) {
  10. parent::__construct($app);
  11. }
  12. /**
  13. * @param string $message
  14. * @param int $code
  15. * @param null $data
  16. */
  17. public function error($message='',$code=1003,$data=null){
  18. $this->result($message,$data,$code);
  19. }
  20. /**
  21. * @param string $message
  22. * @param int $code
  23. * @param null $data
  24. */
  25. public function success($message='',$data=null,$code=0){
  26. $this->result($message,$data,$code);
  27. }
  28. /**
  29. * @param string $msg
  30. * @param null $data
  31. * @param int $code
  32. * @param string|null $type
  33. * @param array $header
  34. * @param array $options
  35. */
  36. private function result(string $msg, $data = null, int $code = 0, string $type = "json", array $header = [], array
  37. $options = [])
  38. {
  39. $result = [
  40. 'code' => $code,
  41. 'message' => $msg,
  42. 'data' => $data,
  43. ];
  44. $code = 200;
  45. if (isset($header['statuscode'])) {
  46. $code = $header['statuscode'];
  47. unset($header['statuscode']);
  48. }
  49. $response = Response::create($result, $type, $code)->header($header)->options($options);
  50. throw new HttpResponseException($response);
  51. }
  52. }