Base.php 1.4 KB

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