AgencyBaseApi.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Admin;
  4. use app\common\controller\Api;
  5. use think\exception\HttpResponseException;
  6. use think\Request;
  7. use think\Response;
  8. class AgencyBaseApi extends Api
  9. {
  10. protected $noNeedLogin = ["*"];
  11. protected $cNoNeedLogin = [];
  12. protected $admin = null;
  13. /**
  14. * AgencyBaseApi constructor.
  15. * @param Request|null $request
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. * @throws \think\exception\DbException
  19. */
  20. public function __construct(Request $request = null)
  21. {
  22. parent::__construct($request);
  23. $urls = explode("/", $request->path());
  24. $method = $urls[count($urls) - 1];
  25. if (in_array($method, $this->cNoNeedLogin))
  26. return;
  27. $session_token = $request->header("session_token");
  28. $responseException = new HttpResponseException(Response::create([
  29. 'code' => 401,
  30. 'msg' => "请先登录",
  31. 'time' => Request::instance()->server('REQUEST_TIME'),
  32. 'data' => null,
  33. ], 'json')->header([]));
  34. if (is_null($session_token))
  35. throw $responseException;
  36. $admin = Admin::where([
  37. "session_token" => $session_token,
  38. "status" => \E_MASSAGER_STATUS::Normal
  39. ])->find();
  40. if (!$admin)
  41. throw $responseException;
  42. $this->admin = $admin;
  43. }
  44. }