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.
154 lines
4.7 KiB
154 lines
4.7 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AdminUsers;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\OperationLogService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
/**
|
|
* @var OperationLogService
|
|
*/
|
|
protected OperationLogService $logService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param OperationLogService $logService
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
OperationLogService $logService,
|
|
) {
|
|
$this->responseService = $responseService;
|
|
$this->logService = $logService;
|
|
}
|
|
|
|
/**
|
|
* 用户登录
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function login(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$validator = Validator::make($request->all(), [
|
|
'username' => 'required',
|
|
'password' => 'required'
|
|
], [
|
|
'username.required' => __('validation.auth.u_empty'),
|
|
'password.required' => __('validation.auth.p_empty'),
|
|
]);
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
|
|
$user = AdminUsers::where('username', $request->username)->first();
|
|
|
|
if (!$user || !Hash::check($request->password, $user->password)) {
|
|
throw new CustomException(__('validation.auth.u_p_error'), 401);
|
|
}
|
|
|
|
if ($user->status !== 1) {
|
|
throw new CustomException(__('validation.auth.u_disabled'), 403);
|
|
}
|
|
|
|
// 删除旧token
|
|
$user->tokens()->delete();
|
|
|
|
// 创建新token
|
|
$token = $user->createToken('auth-token')->plainTextToken;
|
|
|
|
// 记录日志
|
|
$this->logService->log('login', $user->name . '登录系统');
|
|
|
|
return $this->responseService->success([
|
|
'user' => [
|
|
'id' => $user->id,
|
|
'name' => $user->name,
|
|
'avatar' => $user->avatar
|
|
],
|
|
'token' => $token,
|
|
]);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (\Exception $e) {
|
|
$m_prefix = __('admin.login') . __('admin.failed');
|
|
return $this->responseService->systemError($m_prefix . ':' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 退出登录
|
|
* @return JsonResponse
|
|
*/
|
|
public function logout(): JsonResponse
|
|
{
|
|
try {
|
|
$user = Auth::guard('sanctum')->user();
|
|
|
|
if ($user) {
|
|
// 删除所有token
|
|
$user->tokens()->delete();
|
|
|
|
$this->logService->log('logout', $user['name'] . '退出系统');
|
|
}
|
|
|
|
return $this->responseService->success(null, __('admin.logout_successful'));
|
|
} catch (\Exception $e) {
|
|
$m_prefix = __('admin.logout') . __('admin.failed');
|
|
return $this->responseService->systemError($m_prefix . ':' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取当前登录用户信息
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
*/
|
|
public function me(): JsonResponse
|
|
{
|
|
try {
|
|
$user = Auth::guard('sanctum')->user();
|
|
|
|
if (!$user) {
|
|
throw new CustomException('未登录', 401);
|
|
}
|
|
|
|
// 查询用户角色和权限
|
|
$roles = $user->roles()->pluck('name')->toArray();
|
|
// 查询权限
|
|
// $permissions = $user->getAllPermissions()->pluck('name')->toArray();
|
|
|
|
return $this->responseService->success([
|
|
'id' => $user['id'],
|
|
'name' => $user['name'],
|
|
'email' => $user['email'],
|
|
'avatar' => $user['avatar'],
|
|
'roles' => $roles,
|
|
// 'permissions' => $permissions,
|
|
]);
|
|
} catch (CustomException $e) {
|
|
throw $e;
|
|
} catch (\Exception $e) {
|
|
$m_prefix = __('exception.get_user_info_error');
|
|
return $this->responseService->systemError($m_prefix . ':' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|