Api.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\common\controller;
  3. use Throwable;
  4. use think\App;
  5. use think\Response;
  6. use app\BaseController;
  7. use think\facade\Config;
  8. use think\exception\HttpResponseException;
  9. /**
  10. * API控制器基类
  11. */
  12. class Api extends BaseController
  13. {
  14. /**
  15. * 默认响应输出类型,支持json/xml/jsonp
  16. * @var string
  17. */
  18. protected string $responseType = 'json';
  19. /**
  20. * 应用站点系统设置
  21. * @var bool
  22. */
  23. protected bool $useSystemSettings = true;
  24. public function __construct(App $app)
  25. {
  26. parent::__construct($app);
  27. }
  28. /**
  29. * 控制器初始化方法
  30. * @access protected
  31. * @throws Throwable
  32. */
  33. protected function initialize(): void
  34. {
  35. if ($this->useSystemSettings) {
  36. // ip检查
  37. ip_check();
  38. // 时区设定
  39. set_timezone();
  40. }
  41. parent::initialize();
  42. /**
  43. * 设置默认过滤规则
  44. * @see filter()
  45. */
  46. $this->request->filter('filter');
  47. // 加载控制器语言包
  48. $langSet = $this->app->lang->getLangSet();
  49. $this->app->lang->load([
  50. app_path() . 'lang' . DIRECTORY_SEPARATOR . $langSet . DIRECTORY_SEPARATOR . (str_replace('/', DIRECTORY_SEPARATOR, $this->app->request->controllerPath)) . '.php'
  51. ]);
  52. }
  53. /**
  54. * 操作成功
  55. * @param string $msg 提示消息
  56. * @param mixed $data 返回数据
  57. * @param int $code 错误码
  58. * @param string|null $type 输出类型
  59. * @param array $header 发送的 header 信息
  60. * @param array $options Response 输出参数
  61. */
  62. protected function success(string $msg = '', mixed $data = null, int $code = 1, string $type = null, array $header = [], array $options = [])
  63. {
  64. $this->result($msg, $data, $code, $type, $header, $options);
  65. }
  66. /**
  67. * 操作失败
  68. * @param string $msg 提示消息
  69. * @param mixed $data 返回数据
  70. * @param int $code 错误码
  71. * @param string|null $type 输出类型
  72. * @param array $header 发送的 header 信息
  73. * @param array $options Response 输出参数
  74. */
  75. protected function error(string $msg = '', mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = [])
  76. {
  77. $this->result($msg, $data, $code, $type, $header, $options);
  78. }
  79. /**
  80. * 返回 API 数据
  81. * @param string $msg 提示消息
  82. * @param mixed $data 返回数据
  83. * @param int $code 错误码
  84. * @param string|null $type 输出类型
  85. * @param array $header 发送的 header 信息
  86. * @param array $options Response 输出参数
  87. */
  88. public function result(string $msg, mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = [])
  89. {
  90. $result = [
  91. 'code' => $code,
  92. 'msg' => $msg,
  93. 'time' => $this->request->server('REQUEST_TIME'),
  94. 'data' => $data,
  95. ];
  96. // 如果未设置类型则自动判断
  97. $type = $type ?: ($this->request->param(Config::get('route.var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  98. $code = 200;
  99. if (isset($header['statuscode'])) {
  100. $code = $header['statuscode'];
  101. unset($header['statuscode']);
  102. }
  103. $response = Response::create($result, $type, $code)->header($header)->options($options);
  104. throw new HttpResponseException($response);
  105. }
  106. }