57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace app\admin\middleware;
|
|
|
|
use app\common\Trait\ApiResponse;
|
|
use think\facade\Session;
|
|
|
|
class Auth
|
|
{
|
|
use ApiResponse;
|
|
|
|
/**
|
|
* 无需 admin 角色即可访问的路由名称(方法名小写)
|
|
*/
|
|
protected $publicActions = [];
|
|
|
|
/**
|
|
* 需要 admin 角色才能访问的路由名称(方法名小写)
|
|
*/
|
|
protected $adminActions = [
|
|
'deleteoperator',
|
|
'deletecompany',
|
|
'addcompany',
|
|
'addoperator',
|
|
'getallusers',
|
|
'addaccount',
|
|
'updateaccountstatus',
|
|
'deleteaccount',
|
|
'getexportdata',
|
|
'deleteticket',
|
|
'batchupdate',
|
|
];
|
|
|
|
public function handle($request, \Closure $next)
|
|
{
|
|
if (!Session::has('user_info')) {
|
|
return $this->error('未登录,请先登录', [], 401);
|
|
}
|
|
|
|
$user = Session::get('user_info');
|
|
$role = $user['role'] ?? 'user';
|
|
|
|
$request->login_user_id = $user['id'] ?? null;
|
|
$request->login_role = $role;
|
|
|
|
// 获取当前请求的路由方法名
|
|
$action = strtolower($request->action());
|
|
|
|
// admin 专用接口
|
|
if (in_array($action, $this->adminActions) && $role !== 'admin') {
|
|
return $this->error('权限不足,仅管理员可执行此操作', [], 403);
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|