Api.php 3.5 KB

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