Api.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\common\controller;
  3. use think\facade\Cache;
  4. use Throwable;
  5. use think\App;
  6. use think\Response;
  7. use think\facade\Db;
  8. use app\BaseController;
  9. use app\common\library\BlurUtils;
  10. use think\facade\Config;
  11. use think\db\exception\PDOException;
  12. use think\exception\HttpResponseException;
  13. /**
  14. * API控制器基类
  15. */
  16. class Api extends BaseController
  17. {
  18. /**
  19. * 默认响应输出类型,支持json/xml/jsonp
  20. * @var string
  21. */
  22. protected string $responseType = 'json';
  23. /**
  24. * 应用站点系统设置
  25. * @var bool
  26. */
  27. protected bool $useSystemSettings = true;
  28. public function __construct(App $app)
  29. {
  30. parent::__construct($app);
  31. }
  32. /**
  33. * 控制器初始化方法
  34. * @access protected
  35. * @throws Throwable
  36. */
  37. protected function initialize(): void
  38. {
  39. // 系统站点配置
  40. if ($this->useSystemSettings) {
  41. // 检查数据库连接
  42. try {
  43. Db::execute("SELECT 1");
  44. } catch (PDOException $e) {
  45. $this->error(mb_convert_encoding($e->getMessage(), 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'));
  46. }
  47. ip_check(); // ip检查
  48. set_timezone(); // 时区设定
  49. }
  50. parent::initialize();
  51. /**
  52. * 设置默认过滤规则
  53. * @see filter()
  54. */
  55. $this->request->filter('filter');
  56. // 加载控制器语言包
  57. $langSet = $this->app->lang->getLangSet();
  58. $this->app->lang->load([
  59. app_path() . 'lang' . DIRECTORY_SEPARATOR . $langSet . DIRECTORY_SEPARATOR . (str_replace('/', DIRECTORY_SEPARATOR, $this->app->request->controllerPath)) . '.php'
  60. ]);
  61. }
  62. /**
  63. * 操作成功
  64. * @param string $msg 提示消息
  65. * @param mixed $data 返回数据
  66. * @param int $code 错误码
  67. * @param string|null $type 输出类型
  68. * @param array $header 发送的 header 信息
  69. * @param array $options Response 输出参数
  70. */
  71. protected function success(string $msg = '', mixed $data = null, int $code = 1, string $type = null, array $header = [], array $options = []): void
  72. {
  73. $lastTime = Cache::get(get_auth_token());
  74. if(!empty($lastTime))
  75. {
  76. Cache::set(get_auth_token(),time());
  77. }
  78. $this->result($msg, $data, $code, $type, $header, $options);
  79. }
  80. /**
  81. * 操作失败
  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. protected function error(string $msg = '', mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = []): void
  90. {
  91. $this->result($msg, $data, $code, $type, $header, $options);
  92. }
  93. /**
  94. * 返回 API 数据
  95. * @param string $msg 提示消息
  96. * @param mixed $data 返回数据
  97. * @param int $code 错误码
  98. * @param string|null $type 输出类型
  99. * @param array $header 发送的 header 信息
  100. * @param array $options Response 输出参数
  101. */
  102. public function result(string $msg, mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = [])
  103. {
  104. $param = $this->request->post();
  105. $result = [
  106. 'code' => $code,
  107. 'msg' => $msg,
  108. 'time' => $this->request->server('REQUEST_TIME'),
  109. 'data' => $data,
  110. 'request_time'=>$param['request_time'] ?? '',
  111. 'request_no'=>$param['request_no'] ?? '',
  112. 'response_time'=>time(),
  113. 'response_no'=>time().rand(1000,9999),
  114. 'version'=>Config::get('app.version') ?? '1'
  115. ];
  116. // 如果未设置类型则自动判断
  117. $type = $type ?: ($this->request->param(Config::get('route.var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  118. $code = 200;
  119. if (isset($header['statuscode'])) {
  120. $code = $header['statuscode'];
  121. unset($header['statuscode']);
  122. }
  123. $response = Response::create($result, $type, $code)->header($header)->options($options);
  124. throw new HttpResponseException($response);
  125. }
  126. public function biSuccess($data)
  127. {
  128. $response = Response::create($data, 'json', 0);
  129. throw new HttpResponseException($response);
  130. }
  131. }