79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | saiadmin [ saiadmin快速开发框架 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Author: sai <1430792918@qq.com>
|
||
// +----------------------------------------------------------------------
|
||
namespace plugin\saiadmin\basic;
|
||
|
||
use plugin\saiadmin\app\cache\UserInfoCache;
|
||
use plugin\saiadmin\exception\ApiException;
|
||
|
||
/**
|
||
* 基类 控制器继承此类
|
||
*/
|
||
class BaseController extends OpenController
|
||
{
|
||
|
||
/**
|
||
* 当前登陆管理员信息
|
||
*/
|
||
protected $adminInfo;
|
||
|
||
/**
|
||
* 当前登陆管理员ID(未登录时为 null)
|
||
*/
|
||
protected ?int $adminId = null;
|
||
|
||
/**
|
||
* 当前登陆管理员账号(未登录时为空字符串)
|
||
*/
|
||
protected string $adminName = '';
|
||
|
||
/**
|
||
* 逻辑层注入
|
||
*/
|
||
protected $logic;
|
||
|
||
/**
|
||
* 验证器注入
|
||
*/
|
||
protected $validate;
|
||
|
||
/**
|
||
* 初始化
|
||
*/
|
||
protected function init(): void
|
||
{
|
||
// 登录模式赋值(仅当 check_admin 有效时赋值,避免登录接口等未带 token 时访问 null 导致报错)
|
||
$isLogin = request()->header('check_login', false);
|
||
$result = request()->header('check_admin');
|
||
if ($isLogin && $result !== null && (is_array($result) || is_object($result))) {
|
||
$arr = is_array($result) ? $result : (array) $result;
|
||
$adminId = $arr['id'] ?? null;
|
||
if ($adminId !== null) {
|
||
$this->adminId = (int) $adminId;
|
||
$this->adminName = $arr['username'] ?? '';
|
||
$this->adminInfo = UserInfoCache::getUserInfo($adminId);
|
||
|
||
// 用户数据传递给逻辑层
|
||
$this->logic && $this->logic->init($this->adminInfo);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 验证器调用
|
||
*/
|
||
protected function validate(string $scene, $data): bool
|
||
{
|
||
if ($this->validate) {
|
||
if (!$this->validate->scene($scene)->check($data)) {
|
||
throw new ApiException($this->validate->getError());
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
}
|