feat: Log Util

This commit is contained in:
2024-10-27 21:54:35 +08:00
parent a60c6ea29e
commit 5285dd6972
12 changed files with 320 additions and 13 deletions

View File

@@ -1,94 +0,0 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Cache\Redis\Admin\UserCache;
use App\Constants\Admin\UserCode;
use App\Constants\AdminCode;
use App\Exception\AdminException;
use App\Extend\SystemUtil;
use App\Lib\Crypto\CryptoFactory;
use App\Model\AdminUser;
use App\Service\Admin\BaseService;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Hyperf\Config\config;
class LoginService extends BaseService
{
/**
* 注入管理员模型
* @var AdminUser $adminUserModel
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* 注入加密工厂
* @var CryptoFactory $cryptoFactory
*/
#[Inject]
protected CryptoFactory $cryptoFactory;
/**
* 注入用户缓存
* @var UserCache $userCache
*/
#[Inject]
protected UserCache $userCache;
/**
* 后台登录
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function handle(): array
{
$userInfo = $this->adminUserModel->getAdminInfoByAccount($this->request->input('account'));
if (!$userInfo) throw new AdminException('账号不存在');
if ($userInfo->status == UserCode::DISABLE) throw new AdminException(UserCode::getMessage($userInfo->status),AdminCode::LOGIN_ERROR);
// pass加密跟数据库做判断
$password = $this->cryptoFactory->cryptoClass('admin-password',$this->request->input('password'),$userInfo->salt)->encrypt();
if ($password != $userInfo->password) throw new AdminException('密码错误!');
$userInfo->last_login_time = date('Y-m-d H:i:s');
$userInfo->last_login_ip = SystemUtil::getClientIp();
$userInfo->save();
if (!$userInfo->save()) throw new AdminException('登录失败');
//生成 token
$token = $this->cryptoFactory->cryptoClass('admin-jwt',json_encode([
'id' => $userInfo->id,
'role' => $userInfo->role_id,
]))->encrypt();
//单点登录
$this->userCache->setAdminToken($userInfo->id, $token, (int)config('system.admin_jwt_expire'));
return $this->return->success('success',[
'token' => $token,
'info' => [
'admin_id' => $userInfo->id,
'avatar' => $userInfo->avatar,
'name' => $userInfo->chinese_name,
'role_id' => $userInfo->role_id,
]
]);
}
}

View File

@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Exception\AdminException;
use App\Lib\Log;
use App\Model\AdminRole;
use App\Service\Admin\BaseService;
use Exception;
@@ -25,7 +26,11 @@ class RoleService extends BaseService
#[Inject]
protected AdminRole $adminRoleModel;
private string $field = 'id as role_id, name, remark, status, created_at, updated_at';
/**
* 查询字段
* @var array|string[]
*/
private array $field = ['id as role_id', 'name', 'remark', 'status', 'create_time'];
/**
* 列表