refactor:优化控制器基类

This commit is contained in:
妙码生花 2024-03-12 12:55:34 +08:00
parent 473db6603c
commit 4359962e1d
2 changed files with 4 additions and 6 deletions

View File

@ -6,7 +6,6 @@ use Throwable;
use think\Model;
use think\facade\Db;
use think\facade\Event;
use think\facade\Cookie;
use app\admin\library\Auth;
use think\db\exception\PDOException;
use think\exception\HttpResponseException;
@ -146,9 +145,8 @@ class Backend extends Api
$this->error(mb_convert_encoding($e->getMessage(), 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'));
}
$token = get_auth_token();
$this->auth = Auth::instance();
$routePath = $this->app->request->controllerPath . '/' . $this->request->action(true);
$token = $this->request->server('HTTP_BATOKEN', $this->request->request('batoken', Cookie::get('batoken') ?: false));
if (!action_in_arr($this->noNeedLogin)) {
$this->auth->init($token);
if (!$this->auth->isLogin()) {
@ -157,6 +155,7 @@ class Backend extends Api
], $this->auth::LOGIN_RESPONSE_CODE);
}
if (!action_in_arr($this->noNeedPermission)) {
$routePath = ($this->app->request->controllerPath ?? '') . '/' . $this->request->action(true);
if (!$this->auth->check($routePath)) {
$this->error(__('You have no permission'), [], 401);
}

View File

@ -4,7 +4,6 @@ namespace app\common\controller;
use Throwable;
use think\facade\Event;
use think\facade\Cookie;
use app\common\library\Auth;
use think\exception\HttpResponseException;
@ -37,9 +36,8 @@ class Frontend extends Api
public function initialize(): void
{
parent::initialize();
$token = get_auth_token(['ba', 'user', 'token']);
$this->auth = Auth::instance();
$routePath = $this->app->request->controllerPath . '/' . $this->request->action(true);
$token = $this->request->server('HTTP_BA_USER_TOKEN', $this->request->request('ba-user-token', Cookie::get('ba-user-token') ?: false));
if (!action_in_arr($this->noNeedLogin)) {
$this->auth->init($token);
if (!$this->auth->isLogin()) {
@ -48,6 +46,7 @@ class Frontend extends Api
], $this->auth::LOGIN_RESPONSE_CODE);
}
if (!action_in_arr($this->noNeedPermission)) {
$routePath = ($this->app->request->controllerPath ?? '') . '/' . $this->request->action(true);
if (!$this->auth->check($routePath)) {
$this->error(__('You have no permission'), [], 401);
}