98 lines
3.7 KiB
PHP
98 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use Slowlyo\OwlAdmin\Controllers\AuthController as AdminAuthController;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Slowlyo\OwlAdmin\Models\AdminUser;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use App\Enums\SocialiteType;
|
|
use App\Models\UserSocialite;
|
|
|
|
class AuthController extends AdminAuthController
|
|
{
|
|
public function login(Request $request)
|
|
{
|
|
if (Admin::config('admin.auth.login_captcha')) {
|
|
if (!$request->has('captcha')) {
|
|
return $this->response()->fail(__('admin.required', ['attribute' => __('admin.captcha')]));
|
|
}
|
|
|
|
if (strtolower(admin_decode($request->sys_captcha)) != strtolower($request->captcha)) {
|
|
return $this->response()->fail(__('admin.captcha_error'));
|
|
}
|
|
}
|
|
|
|
try {
|
|
$validator = Validator::make($request->all(), [
|
|
'username' => 'required',
|
|
'password' => 'required',
|
|
], [
|
|
'username' . '.required' => __('admin.required', ['attribute' => __('admin.username')]),
|
|
'password.required' => __('admin.required', ['attribute' => __('admin.password')]),
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
abort(Response::HTTP_BAD_REQUEST, $validator->errors()->first());
|
|
}
|
|
$adminModel = Admin::config("admin.auth.model", AdminUser::class);
|
|
$user = $adminModel::query()->where('username', $request->username)->first();
|
|
if ($user && Hash::check($request->password, $user->password)) {
|
|
$module = Admin::currentModule(true);
|
|
$prefix = $module ? $module . '.' : '';
|
|
$token = $user->createToken($prefix . 'admin')->plainTextToken;
|
|
|
|
// 更新第三方账户
|
|
$openid = $request->input('openid');
|
|
$open_type = $request->input('open_type');
|
|
if ($openid && $open_type) {
|
|
UserSocialite::where(['openid' => $openid, 'type' => SocialiteType::from($open_type)])->update([
|
|
'user_id' => $user->id,
|
|
'user_type' => $user->getMorphClass(),
|
|
]);
|
|
}
|
|
|
|
return $this->response()->success(compact('token'), __('admin.login_successful'));
|
|
}
|
|
|
|
abort(Response::HTTP_BAD_REQUEST, __('admin.login_failed'));
|
|
} catch (\Exception $e) {
|
|
return $this->response()->fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function currentUser()
|
|
{
|
|
$userInfo = Admin::user()->only(['name', 'avatar', 'id']);
|
|
|
|
$menus = amisMake()
|
|
->DropdownButton()
|
|
->hideCaret()
|
|
->trigger('hover')
|
|
->label($userInfo['name'])
|
|
->align('right')
|
|
->btnClassName('navbar-user')
|
|
->menuClassName('min-w-0 px-2')
|
|
->set('icon', $userInfo['avatar'])
|
|
->buttons([
|
|
amisMake()
|
|
->VanillaAction()
|
|
->iconClassName('pr-2')
|
|
->icon('fa fa-user-gear')
|
|
->label(__('admin.user_setting'))
|
|
->onClick('window.location.hash = "#/user_setting"'),
|
|
amisMake()
|
|
->VanillaAction()
|
|
->iconClassName('pr-2')
|
|
->label(__('admin.logout'))
|
|
->icon('fa-solid fa-right-from-bracket')
|
|
->onClick('window.$owl.logout()'),
|
|
]);
|
|
|
|
return $this->response()->success(array_merge($userInfo, compact('menus')));
|
|
}
|
|
}
|