Base.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\api\controller;
  3. use think\exception\HttpResponseException;
  4. use think\Request;
  5. use think\Response;
  6. class Base
  7. {
  8. /**
  9. * @var Request Request 实例
  10. */
  11. protected $request;
  12. /**
  13. * 默认响应输出类型,支持json/xml
  14. * @var string
  15. */
  16. protected string $responseType = 'json';
  17. /**
  18. * 构造方法
  19. * @param Request|null $request
  20. */
  21. public function __construct(Request $request = null)
  22. {
  23. $this->request = is_null($request) ? request() : $request;
  24. // 控制器初始化
  25. $this->_initialize();
  26. }
  27. /**
  28. * 初始化操作
  29. * @access protected
  30. */
  31. protected function _initialize()
  32. {
  33. // //跨域请求检测
  34. // check_cors_request();
  35. //
  36. // // 检测IP是否允许
  37. // check_ip_allowed();
  38. //移除HTML标签
  39. //$this->request->filter('trim,strip_tags,htmlspecialchars');
  40. }
  41. /**
  42. * 操作成功返回的数据
  43. * @param string $msg 提示信息
  44. * @param mixed $data 要返回的数据
  45. * @param int $code 错误码,默认为1
  46. * @param string|null $type 输出类型
  47. * @param array $header 发送的 Header 信息
  48. */
  49. protected function success(string $msg = '', $data = null, int $code = 200, string $type = null, array $header = [])
  50. {
  51. if (empty($msg)) {
  52. $msg = "操作成功";
  53. }
  54. $this->result($msg, $data, $code, $type, $header);
  55. }
  56. /**
  57. * 操作失败返回的数据
  58. * @param string $msg 提示信息
  59. * @param mixed $data 要返回的数据
  60. * @param int $code 错误码,默认为0
  61. * @param string|null $type 输出类型
  62. * @param array $header 发送的 Header 信息
  63. */
  64. protected function error(string $msg = '', $data = null, int $code = 0, string $type = null, array $header = [])
  65. {
  66. if (empty($msg)) {
  67. $msg = "操作失败";
  68. }
  69. $this->result($msg, $data, $code, $type, $header);
  70. }
  71. /**
  72. * 返回封装后的 API 数据到客户端
  73. * @access protected
  74. * @param mixed $msg 提示信息
  75. * @param mixed $data 要返回的数据
  76. * @param int $code 错误码,默认为0
  77. * @param string|null $type 输出类型,支持json/xml/jsonp
  78. * @param array $header 发送的 Header 信息
  79. * @return void
  80. * @throws HttpResponseException
  81. */
  82. protected function result($msg, $data = null, int $code = 0, string $type = null, array $header = [])
  83. {
  84. $result = [
  85. 'code' => $code,
  86. 'msg' => $msg,
  87. 'data' => $data,
  88. ];
  89. // 如果未设置类型则自动判断
  90. $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  91. if (isset($header['statuscode'])) {
  92. $code = $header['statuscode'];
  93. unset($header['statuscode']);
  94. } else {
  95. //未设置状态码,根据code值判断
  96. $code = $code < 200 ? 200 : $code;
  97. }
  98. $response = Response::create($result, $type, $code)->header($header);
  99. throw new HttpResponseException($response);
  100. }
  101. }