Base.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\api\controller;
  3. use think\exception\HttpResponseException;
  4. use think\Request;
  5. use think\Response;
  6. use app\api\service\auth\MysqlAdapter;
  7. use think\facade\Cache;
  8. class Base
  9. {
  10. /**
  11. * @var Request Request 实例
  12. */
  13. protected $request;
  14. /**
  15. * 默认响应输出类型,支持json/xml
  16. * @var string
  17. */
  18. protected string $responseType = 'json';
  19. /**
  20. * 无需登录的方法,同时也就不需要鉴权了
  21. * @var array
  22. */
  23. protected $noNeedLogin = [];
  24. //用户信息
  25. protected $userinfo =[];
  26. /**
  27. * 构造方法
  28. * @param Request|null $request
  29. */
  30. public function __construct(Request $request = null)
  31. {
  32. $this->request = is_null($request) ? request() : $request;
  33. // 控制器初始化
  34. $this->_initialize();
  35. }
  36. /**
  37. * 初始化操作
  38. * @access protected
  39. */
  40. protected function _initialize()
  41. {
  42. $token=request()->header('token');
  43. $actionname = $this->request->action();
  44. $noNeedLoginSet=is_string($this->noNeedLogin)?[$this->noNeedLogin]:$this->noNeedLogin;
  45. $noNeedLogin = in_array('*',$noNeedLoginSet) || in_array($actionname,$noNeedLoginSet);
  46. //需要登陆
  47. if(!$noNeedLogin && !MysqlAdapter::getUserToken($token)){
  48. $response = Response::create(__('请先登录!'), 'html', 401);
  49. throw new HttpResponseException($response);
  50. }
  51. //获取用户信息
  52. if(!$noNeedLogin) {
  53. $this->userinfo = MysqlAdapter::userinfo($token);
  54. if(!$this->userinfo) Response::create(__('请先登录!'), 'html', 401);
  55. $this->userinfo = Cache::get('user_info_'.$this->userinfo);
  56. }
  57. }
  58. /**
  59. * 操作成功返回的数据
  60. * @param string $msg 提示信息
  61. * @param mixed $data 要返回的数据
  62. * @param int $code 错误码,默认为1
  63. * @param string|null $type 输出类型
  64. * @param array $header 发送的 Header 信息
  65. */
  66. protected function success(string $msg = '', $data = null, int $code = 200, string $type = null, array $header = [])
  67. {
  68. if (empty($msg)) {
  69. $msg = "操作成功";
  70. }
  71. $this->result($msg, $data, $code, $type, $header);
  72. }
  73. /**
  74. * 操作失败返回的数据
  75. * @param string $msg 提示信息
  76. * @param mixed $data 要返回的数据
  77. * @param int $code 错误码,默认为0
  78. * @param string|null $type 输出类型
  79. * @param array $header 发送的 Header 信息
  80. */
  81. protected function error(string $msg = '', $data = null, int $code = 0, string $type = null, array $header = [])
  82. {
  83. if (empty($msg)) {
  84. $msg = "操作失败";
  85. }
  86. $this->result($msg, $data, $code, $type, $header);
  87. }
  88. /**
  89. * 返回封装后的 API 数据到客户端
  90. * @access protected
  91. * @param mixed $msg 提示信息
  92. * @param mixed $data 要返回的数据
  93. * @param int $code 错误码,默认为0
  94. * @param string|null $type 输出类型,支持json/xml/jsonp
  95. * @param array $header 发送的 Header 信息
  96. * @return void
  97. * @throws HttpResponseException
  98. */
  99. protected function result($msg, $data = null, int $code = 0, string $type = null, array $header = [])
  100. {
  101. $result = [
  102. 'code' => $code,
  103. 'msg' => $msg,
  104. 'data' => $data,
  105. ];
  106. // 如果未设置类型则自动判断
  107. $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  108. if (isset($header['statuscode'])) {
  109. $code = $header['statuscode'];
  110. unset($header['statuscode']);
  111. } else {
  112. //未设置状态码,根据code值判断
  113. $code = $code < 200 ? 200 : $code;
  114. }
  115. $response = Response::create($result, $type, $code)->header($header);
  116. throw new HttpResponseException($response);
  117. }
  118. }