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. $uid= MysqlAdapter::userinfo($token);
  54. if(!$uid) Response::create(__('请先登录!'), 'html', 401);
  55. $userinfo = Cache::store('redis')->get('user_info_'.$uid);
  56. if(!$userinfo) Response::create(__('请先登录!'), 'html', 401);
  57. $this->userinfo = $userinfo;
  58. }
  59. }
  60. /**
  61. * 操作成功返回的数据
  62. * @param string $msg 提示信息
  63. * @param mixed $data 要返回的数据
  64. * @param int $code 错误码,默认为1
  65. * @param string|null $type 输出类型
  66. * @param array $header 发送的 Header 信息
  67. */
  68. protected function success(string $msg = '', $data = null, int $code = 200, string $type = null, array $header = [])
  69. {
  70. if (empty($msg)) {
  71. $msg = "操作成功";
  72. }
  73. $this->result($msg, $data, $code, $type, $header);
  74. }
  75. /**
  76. * 操作失败返回的数据
  77. * @param string $msg 提示信息
  78. * @param mixed $data 要返回的数据
  79. * @param int $code 错误码,默认为0
  80. * @param string|null $type 输出类型
  81. * @param array $header 发送的 Header 信息
  82. */
  83. protected function error(string $msg = '', $data = null, int $code = 0, string $type = null, array $header = [])
  84. {
  85. if (empty($msg)) {
  86. $msg = "操作失败";
  87. }
  88. $this->result($msg, $data, $code, $type, $header);
  89. }
  90. /**
  91. * 返回封装后的 API 数据到客户端
  92. * @access protected
  93. * @param mixed $msg 提示信息
  94. * @param mixed $data 要返回的数据
  95. * @param int $code 错误码,默认为0
  96. * @param string|null $type 输出类型,支持json/xml/jsonp
  97. * @param array $header 发送的 Header 信息
  98. * @return void
  99. * @throws HttpResponseException
  100. */
  101. protected function result($msg, $data = null, int $code = 0, string $type = null, array $header = [])
  102. {
  103. $result = [
  104. 'code' => $code,
  105. 'msg' => $msg,
  106. 'data' => $data,
  107. ];
  108. // 如果未设置类型则自动判断
  109. $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  110. if (isset($header['statuscode'])) {
  111. $code = $header['statuscode'];
  112. unset($header['statuscode']);
  113. } else {
  114. //未设置状态码,根据code值判断
  115. $code = $code < 200 ? 200 : $code;
  116. }
  117. $response = Response::create($result, $type, $code)->header($header);
  118. throw new HttpResponseException($response);
  119. }
  120. }