| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\api\controller;
- use think\exception\HttpResponseException;
- use think\Request;
- use think\Response;
- use app\api\service\auth\MysqlAdapter;
- use think\facade\Cache;
- class Base
- {
- /**
- * @var Request Request 实例
- */
- protected $request;
- /**
- * 默认响应输出类型,支持json/xml
- * @var string
- */
- protected string $responseType = 'json';
- /**
- * 无需登录的方法,同时也就不需要鉴权了
- * @var array
- */
- protected $noNeedLogin = [];
- //用户信息
- protected $userinfo =[];
- /**
- * 构造方法
- * @param Request|null $request
- */
- public function __construct(Request $request = null)
- {
- $this->request = is_null($request) ? request() : $request;
- // 控制器初始化
- $this->_initialize();
- }
- /**
- * 初始化操作
- * @access protected
- */
- protected function _initialize()
- {
- $token=request()->header('token');
- $actionname = $this->request->action();
- $noNeedLoginSet=is_string($this->noNeedLogin)?[$this->noNeedLogin]:$this->noNeedLogin;
- $noNeedLogin = in_array('*',$noNeedLoginSet) || in_array($actionname,$noNeedLoginSet);
- //需要登陆
- if(!$noNeedLogin && !MysqlAdapter::getUserToken($token)){
- $response = Response::create(__('请先登录!'), 'html', 401);
- throw new HttpResponseException($response);
- }
-
- //获取用户信息
- if(!$noNeedLogin) {
- $uid= MysqlAdapter::userinfo($token);
- if(!$uid) Response::create(__('请先登录!'), 'html', 401);
- $userinfo = Cache::store('redis')->get('user_info_'.$uid);
- if(!$userinfo) Response::create(__('请先登录!'), 'html', 401);
- $this->userinfo = $userinfo;
- }
- }
- /**
- * 操作成功返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为1
- * @param string|null $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function success(string $msg = '', $data = null, int $code = 200, string $type = null, array $header = [])
- {
- if (empty($msg)) {
- $msg = "操作成功";
- }
- $this->result($msg, $data, $code, $type, $header);
- }
- /**
- * 操作失败返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为0
- * @param string|null $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function error(string $msg = '', $data = null, int $code = 0, string $type = null, array $header = [])
- {
- if (empty($msg)) {
- $msg = "操作失败";
- }
- $this->result($msg, $data, $code, $type, $header);
- }
- /**
- * 返回封装后的 API 数据到客户端
- * @access protected
- * @param mixed $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为0
- * @param string|null $type 输出类型,支持json/xml/jsonp
- * @param array $header 发送的 Header 信息
- * @return void
- * @throws HttpResponseException
- */
- protected function result($msg, $data = null, int $code = 0, string $type = null, array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data,
- ];
- // 如果未设置类型则自动判断
- $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
- if (isset($header['statuscode'])) {
- $code = $header['statuscode'];
- unset($header['statuscode']);
- } else {
- //未设置状态码,根据code值判断
- $code = $code < 200 ? 200 : $code;
- }
- $response = Response::create($result, $type, $code)->header($header);
- throw new HttpResponseException($response);
- }
- }
|