SeretKeyMiddleware.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-30 14:59
  10. // +----------------------------------------------------------------------
  11. namespace app\api\middleware;
  12. use app\model\api\Site;
  13. use app\Request;
  14. use library\exceptions\AuthException;
  15. use library\interfaces\MiddlewareInterface;
  16. class SeretKeyMiddleware implements MiddlewareInterface
  17. {
  18. public function handle(Request $request, \Closure $next)
  19. {
  20. $secret_key =$request->header('SECRET-KEY');
  21. $request->site = $this->checkSecret($secret_key);
  22. return $next($request);
  23. }
  24. /**
  25. * 检查数据是否正常
  26. * @param $secret_key
  27. */
  28. private function checkSecret($secret_key) {
  29. if(empty($secret_key)) {
  30. throw new AuthException('当前站点已经不存在,暂无法登陆', -100);
  31. }
  32. $siteData = (new Site)->where('secret_key',$secret_key)->find();
  33. if(empty($siteData)) {
  34. throw new AuthException('当前站点已经不存在,暂无法登陆', -100);
  35. }
  36. //站点停用
  37. if(empty($siteData['status'])) {
  38. throw new AuthException('抱歉,前站点已经停用。', -100);
  39. }
  40. return $siteData->toArray();
  41. }
  42. }