2022-02-20 22:42:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\controller;
|
|
|
|
|
2023-06-25 18:36:53 +00:00
|
|
|
use Throwable;
|
2022-02-20 22:42:48 +00:00
|
|
|
use think\App;
|
2022-10-29 17:49:43 +00:00
|
|
|
use think\Response;
|
2024-03-21 17:03:48 +00:00
|
|
|
use think\facade\Db;
|
2022-02-20 22:42:48 +00:00
|
|
|
use app\BaseController;
|
|
|
|
use think\facade\Config;
|
2024-03-21 17:03:48 +00:00
|
|
|
use think\db\exception\PDOException;
|
2022-02-20 22:42:48 +00:00
|
|
|
use think\exception\HttpResponseException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API控制器基类
|
|
|
|
*/
|
|
|
|
class Api extends BaseController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 默认响应输出类型,支持json/xml/jsonp
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-06-23 18:04:44 +00:00
|
|
|
protected string $responseType = 'json';
|
2022-02-20 22:42:48 +00:00
|
|
|
|
2022-07-21 02:29:16 +00:00
|
|
|
/**
|
|
|
|
* 应用站点系统设置
|
|
|
|
* @var bool
|
|
|
|
*/
|
2023-06-23 18:04:44 +00:00
|
|
|
protected bool $useSystemSettings = true;
|
2022-07-21 02:29:16 +00:00
|
|
|
|
2022-02-20 22:42:48 +00:00
|
|
|
public function __construct(App $app)
|
|
|
|
{
|
|
|
|
parent::__construct($app);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 控制器初始化方法
|
2023-06-23 18:04:44 +00:00
|
|
|
* @access protected
|
2023-06-25 18:36:53 +00:00
|
|
|
* @throws Throwable
|
2022-02-20 22:42:48 +00:00
|
|
|
*/
|
2023-06-23 18:04:44 +00:00
|
|
|
protected function initialize(): void
|
2022-02-20 22:42:48 +00:00
|
|
|
{
|
2024-03-21 17:03:48 +00:00
|
|
|
// 系统站点配置
|
2022-07-21 02:29:16 +00:00
|
|
|
if ($this->useSystemSettings) {
|
2024-03-22 16:13:10 +00:00
|
|
|
// 检查数据库连接
|
|
|
|
try {
|
|
|
|
Db::execute("SELECT 1");
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->error(mb_convert_encoding($e->getMessage(), 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'));
|
|
|
|
}
|
|
|
|
|
2024-03-21 17:03:48 +00:00
|
|
|
ip_check(); // ip检查
|
|
|
|
set_timezone(); // 时区设定
|
2022-07-21 02:29:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 10:12:15 +00:00
|
|
|
parent::initialize();
|
2023-06-23 18:04:44 +00:00
|
|
|
|
2023-08-01 13:40:48 +00:00
|
|
|
/**
|
|
|
|
* 设置默认过滤规则
|
|
|
|
* @see filter()
|
|
|
|
*/
|
|
|
|
$this->request->filter('filter');
|
2022-03-09 10:12:15 +00:00
|
|
|
|
|
|
|
// 加载控制器语言包
|
2023-06-23 18:04:44 +00:00
|
|
|
$langSet = $this->app->lang->getLangSet();
|
2022-03-09 10:12:15 +00:00
|
|
|
$this->app->lang->load([
|
2023-06-23 18:04:44 +00:00
|
|
|
app_path() . 'lang' . DIRECTORY_SEPARATOR . $langSet . DIRECTORY_SEPARATOR . (str_replace('/', DIRECTORY_SEPARATOR, $this->app->request->controllerPath)) . '.php'
|
2022-03-09 10:12:15 +00:00
|
|
|
]);
|
2022-02-20 22:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 操作成功
|
2022-10-29 17:49:43 +00:00
|
|
|
* @param string $msg 提示消息
|
|
|
|
* @param mixed $data 返回数据
|
|
|
|
* @param int $code 错误码
|
|
|
|
* @param string|null $type 输出类型
|
|
|
|
* @param array $header 发送的 header 信息
|
|
|
|
* @param array $options Response 输出参数
|
2022-02-20 22:42:48 +00:00
|
|
|
*/
|
2024-03-11 16:43:40 +00:00
|
|
|
protected function success(string $msg = '', mixed $data = null, int $code = 1, string $type = null, array $header = [], array $options = []): void
|
2022-02-20 22:42:48 +00:00
|
|
|
{
|
|
|
|
$this->result($msg, $data, $code, $type, $header, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 操作失败
|
2022-10-29 17:49:43 +00:00
|
|
|
* @param string $msg 提示消息
|
|
|
|
* @param mixed $data 返回数据
|
|
|
|
* @param int $code 错误码
|
|
|
|
* @param string|null $type 输出类型
|
|
|
|
* @param array $header 发送的 header 信息
|
|
|
|
* @param array $options Response 输出参数
|
2022-02-20 22:42:48 +00:00
|
|
|
*/
|
2024-03-11 16:43:40 +00:00
|
|
|
protected function error(string $msg = '', mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = []): void
|
2022-02-20 22:42:48 +00:00
|
|
|
{
|
|
|
|
$this->result($msg, $data, $code, $type, $header, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 返回 API 数据
|
2022-10-29 17:49:43 +00:00
|
|
|
* @param string $msg 提示消息
|
|
|
|
* @param mixed $data 返回数据
|
|
|
|
* @param int $code 错误码
|
|
|
|
* @param string|null $type 输出类型
|
|
|
|
* @param array $header 发送的 header 信息
|
|
|
|
* @param array $options Response 输出参数
|
2022-02-20 22:42:48 +00:00
|
|
|
*/
|
2023-06-23 18:04:44 +00:00
|
|
|
public function result(string $msg, mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = [])
|
2022-02-20 22:42:48 +00:00
|
|
|
{
|
|
|
|
$result = [
|
|
|
|
'code' => $code,
|
|
|
|
'msg' => $msg,
|
|
|
|
'time' => $this->request->server('REQUEST_TIME'),
|
|
|
|
'data' => $data,
|
|
|
|
];
|
|
|
|
|
2024-10-15 01:06:06 +00:00
|
|
|
$type = $type ?: $this->responseType;
|
2024-10-15 01:45:05 +00:00
|
|
|
$code = $header['statusCode'] ?? 200;
|
2022-02-20 22:42:48 +00:00
|
|
|
|
|
|
|
$response = Response::create($result, $type, $code)->header($header)->options($options);
|
|
|
|
throw new HttpResponseException($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|