| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\Http\Requests;
- use App\Authority;
- use App\Components\ApiResponse;
- use Illuminate\Foundation\Http\FormRequest;
- class AndroidGateRequest extends FormRequest
- {
- use ApiResponse;
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * 通用鉴权
- *
- * @return bool
- */
- public function authorize():bool
- {
- //去除参数
- $routes = explode("/",ltrim(explode("?",$this->getPathInfo())[0],"/api/"));
- //去除前缀
- array_shift($routes);
- $routeStr = implode("/",$routes);
- $authorities = app("AuthorityService")->getUserAndroidAuthority();
- foreach ($authorities as $authority){
- if ($this->method() != Authority::METHOD[$authority->method])continue;
- if ($authority->route == $routeStr)return true; //相等
- if (!str_contains($authority->route, "*"))continue;//无泛匹配符
- $route = explode("/",$authority->route);
- $crLen = count($routes);
- $trLen = count($route);
- if ($crLen<$trLen)continue;
- if ($crLen>$trLen && $route[$trLen-1]=='*'){
- $routes = array_slice($routes,0,$trLen);
- $crLen = $trLen;
- }
- if ($crLen!=$trLen)continue;
- foreach ($route as $index=>$item){
- if ($item=='*')$route[$index] = $routes[$index];
- }
- if (implode("/",$route)==$routeStr)return true;
- }
- return false;
- }
- public function failedAuthorization()
- {
- $this->response(false,403,"权限不足");
- }
- public function rules():array
- {
- return [];
- }
- }
|