You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.0 KiB
117 lines
3.0 KiB
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\BaseController;
|
|
use auth\PermissAuth;
|
|
use think\facade\Request;
|
|
use think\facade\Validate;
|
|
|
|
/**
|
|
* @OA\OpenApi(
|
|
* @OA\Server(
|
|
* url="base.ahbmz.com",
|
|
* description="API server"
|
|
* ),
|
|
* @OA\Info(
|
|
* version="1.0.0",
|
|
* title="这是我的swagger文档第一个Api",
|
|
* description="用户Api接口",
|
|
* termsOfService="http://swagger.io/terms/",
|
|
* @OA\Contact(
|
|
* name="安徽云掌技术支持",
|
|
* email="txg@huamill.com",
|
|
* url="http://www.ahbmz.com"
|
|
* ),
|
|
* @OA\License(name="BMZMIT")
|
|
* )
|
|
* )
|
|
*/
|
|
class Common extends BaseController
|
|
{
|
|
protected $action_url = ''; // 请求URL api/manager/index
|
|
|
|
public function initialize()
|
|
{
|
|
//获取请求路径
|
|
$this->action_url = $this->getBaseActionUrl();
|
|
|
|
//是否跳过token、权限验证
|
|
if (!$this->filterJwtAuth()) {
|
|
/**
|
|
* JWT Token检测
|
|
*/
|
|
if ((!defined('UID') || !defined('SITE_ID'))) {
|
|
return send_http_status('', 40512);
|
|
}
|
|
|
|
/**
|
|
* 定义当前类的实例化[操作日志类使用]
|
|
*/
|
|
defined('CURR_THIS') ?: define('CURR_THIS', get_called_class());
|
|
|
|
/**
|
|
* 检测用户权限
|
|
*/
|
|
env('app_debug') ?: $this->checkPermissAuth(); // $this->checkPermissAuth();
|
|
}
|
|
/**
|
|
* 自定义全局验证器
|
|
*/
|
|
$this->uniqueSite();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* 自定义全局验证器,检测同站点下数据是否重复
|
|
*/
|
|
private function uniqueSite()
|
|
{
|
|
Validate::maker(function ($validate) {
|
|
$validate->extend('uniqueSite', 'extra_unique_validate');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 检测用户权限
|
|
*/
|
|
private function checkPermissAuth()
|
|
{
|
|
$url = str_replace(['//', '\\', '/'], '', $this->request->root());
|
|
$url .= '/' . $this->request->controller();
|
|
$url .= '/' . $this->request->action();
|
|
if (!(PermissAuth::getInstance())->check($url, UID)) {
|
|
return send_http_status('', 403);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* jwt 过滤请求地址,如果返回 true 说明不需要验证
|
|
* @return bool
|
|
*/
|
|
private function filterJwtAuth()
|
|
{
|
|
$filter_router = config('jwtauth.filter_router');
|
|
if (empty($filter_router)) {
|
|
return false;
|
|
}
|
|
if (in_array($this->action_url, array_map('strtolower', $filter_router))) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 获取请求路径 模块名/控制器名/方法名
|
|
* @return string
|
|
*/
|
|
private function getBaseActionUrl()
|
|
{
|
|
$url = str_replace(['//', '\\', '/'], '', $this->request->root());
|
|
$url .= '/' . $this->request->controller();
|
|
$url .= '/' . $this->request->action();
|
|
return strtolower($url);
|
|
}
|
|
|
|
}
|
|
|