Base.php 1.4 KB

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