Api.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\common\controller;
  3. use think\App;
  4. use think\Response;
  5. use app\BaseController;
  6. use think\facade\Event;
  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 $responseType = 'json';
  19. /**
  20. * 应用站点系统设置
  21. * @var bool
  22. */
  23. protected $useSystemSettings = true;
  24. public function __construct(App $app)
  25. {
  26. parent::__construct($app);
  27. }
  28. /**
  29. * 控制器初始化方法
  30. */
  31. protected function initialize()
  32. {
  33. if ($this->useSystemSettings) {
  34. // ip检查
  35. ip_check();
  36. // 时区设定
  37. set_timezone();
  38. // 存储/上传资料配置
  39. Event::trigger('uploadConfigInit', $this->app);
  40. }
  41. parent::initialize();
  42. $this->request->filter('trim,strip_tags,htmlspecialchars');
  43. // 加载控制器语言包
  44. $langset = $this->app->lang->getLangSet();
  45. $this->app->lang->load([
  46. app_path() . 'lang' . DIRECTORY_SEPARATOR . $langset . DIRECTORY_SEPARATOR . (str_replace('/', DIRECTORY_SEPARATOR, $this->app->request->controllerPath)) . '.php'
  47. ]);
  48. }
  49. /**
  50. * 操作成功
  51. * @param string $msg 提示消息
  52. * @param mixed $data 返回数据
  53. * @param int $code 错误码
  54. * @param string|null $type 输出类型
  55. * @param array $header 发送的 header 信息
  56. * @param array $options Response 输出参数
  57. */
  58. protected function success(string $msg = '', $data = null, int $code = 1, string $type = null, array $header = [], array $options = [])
  59. {
  60. $this->result($msg, $data, $code, $type, $header, $options);
  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 error(string $msg = '', $data = null, int $code = 0, string $type = null, array $header = [], array $options = [])
  72. {
  73. $this->result($msg, $data, $code, $type, $header, $options);
  74. }
  75. /**
  76. * 返回 API 数据
  77. * @param string $msg 提示消息
  78. * @param mixed $data 返回数据
  79. * @param int $code 错误码
  80. * @param string|null $type 输出类型
  81. * @param array $header 发送的 header 信息
  82. * @param array $options Response 输出参数
  83. */
  84. public function result(string $msg, $data = null, int $code = 0, string $type = null, array $header = [], array $options = [])
  85. {
  86. $result = [
  87. 'code' => $code,
  88. 'msg' => $msg,
  89. 'time' => $this->request->server('REQUEST_TIME'),
  90. 'data' => $data,
  91. ];
  92. // 如果未设置类型则自动判断
  93. $type = $type ?: ($this->request->param(Config::get('route.var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  94. $code = 200;
  95. if (isset($header['statuscode'])) {
  96. $code = $header['statuscode'];
  97. unset($header['statuscode']);
  98. }
  99. $response = Response::create($result, $type, $code)->header($header)->options($options);
  100. throw new HttpResponseException($response);
  101. }
  102. }