123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- // +----------------------------------------------------------------------
- // | [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018-2020 rights reserved.
- // +----------------------------------------------------------------------
- // | Author: TABLE ME
- // +----------------------------------------------------------------------
- // | Date: 2020-08-30 14:59
- // +----------------------------------------------------------------------
- namespace app\api\middleware;
- use app\model\api\Site;
- use app\Request;
- use library\exceptions\AuthException;
- use library\interfaces\MiddlewareInterface;
- class SeretKeyMiddleware implements MiddlewareInterface
- {
- public function handle(Request $request, \Closure $next)
- {
- $secret_key =$request->header('SECRET-KEY');
- $request->site = $this->checkSecret($secret_key);
- return $next($request);
- }
- /**
- * 检查数据是否正常
- * @param $secret_key
- */
- private function checkSecret($secret_key) {
- if(empty($secret_key)) {
- throw new AuthException('当前站点已经不存在,暂无法登陆', -100);
- }
- $siteData = (new Site)->where('secret_key',$secret_key)->find();
- if(empty($siteData)) {
- throw new AuthException('当前站点已经不存在,暂无法登陆', -100);
- }
- //站点停用
- if(empty($siteData['status'])) {
- throw new AuthException('抱歉,前站点已经停用。', -100);
- }
- return $siteData->toArray();
- }
- }
|