241 lines
9.0 KiB
PHP
241 lines
9.0 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), 'user_type' => $user->getMorphClass()])->update([
|
|
'user_id' => $user->id,
|
|
]);
|
|
}
|
|
|
|
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 loginPage()
|
|
{
|
|
$captcha = null;
|
|
$enableCaptcha = Admin::config('admin.auth.login_captcha');
|
|
|
|
// 验证码
|
|
if ($enableCaptcha) {
|
|
$captcha = amisMake()->InputGroupControl()->body([
|
|
amisMake()->TextControl()->name('captcha')->placeholder(__('admin.captcha'))->required(),
|
|
amisMake()->HiddenControl()->name('sys_captcha'),
|
|
amisMake()->Service()->id('captcha-service')->api('get:' . admin_url('/captcha'))->body(
|
|
amisMake()
|
|
->Image()
|
|
->src('${captcha_img}')
|
|
->height('1.917rem')
|
|
->className('p-0 border captcha-box')
|
|
->set(
|
|
'clickAction',
|
|
['actionType' => 'reload', 'target' => 'captcha-service']
|
|
)
|
|
),
|
|
]);
|
|
}
|
|
|
|
// icp 备案号
|
|
$icp_no = Admin::setting()->get('web_icp');
|
|
|
|
$form = amisMake()->Form()->id('login-form')->title()->api(admin_url('/login'))->body([
|
|
amisMake()->TextControl()->name('username')->placeholder(__('admin.username'))->required(),
|
|
amisMake()
|
|
->TextControl()
|
|
->type('input-password')
|
|
->name('password')
|
|
->placeholder(__('admin.password'))
|
|
->required(),
|
|
$captcha,
|
|
amisMake()->CheckboxControl()->name('remember_me')->option(__('admin.remember_me'))->value(true),
|
|
|
|
// 登录按钮
|
|
amisMake()
|
|
->VanillaAction()
|
|
->actionType('submit')
|
|
->label(__('admin.login'))
|
|
->level('primary')
|
|
->className('w-full'),
|
|
])->actions([]); // 清空默认的提交按钮
|
|
|
|
$failAction = [];
|
|
if ($enableCaptcha) {
|
|
// 登录失败后刷新验证码
|
|
$failAction = [
|
|
// 登录失败事件
|
|
'submitFail' => [
|
|
'actions' => [
|
|
// 刷新验证码外层Service
|
|
['actionType' => 'reload', 'componentId' => 'captcha-service'],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
$form->onEvent(array_merge([
|
|
// 页面初始化事件
|
|
'inited' => [
|
|
'actions' => [
|
|
// 读取本地存储的登录参数
|
|
[
|
|
'actionType' => 'custom',
|
|
'script' => <<<JS
|
|
let loginParams = localStorage.getItem('loginParams')
|
|
if(loginParams){
|
|
loginParams = JSON.parse(loginParams)
|
|
doAction({
|
|
actionType: 'setValue',
|
|
componentId: 'login-form',
|
|
args: { value: loginParams }
|
|
})
|
|
}
|
|
JS
|
|
,
|
|
|
|
],
|
|
],
|
|
],
|
|
// 登录成功事件
|
|
'submitSucc' => [
|
|
'actions' => [
|
|
// 保存登录参数到本地, 并跳转到首页
|
|
[
|
|
'actionType' => 'custom',
|
|
'script' => <<<JS
|
|
let _data = {}
|
|
if(event.data.remember_me){
|
|
_data = { username: event.data.username, password: event.data.password }
|
|
}
|
|
window.\$owl.afterLoginSuccess(_data, event.data.result.data.token)
|
|
JS,
|
|
|
|
],
|
|
],
|
|
],
|
|
], $failAction));
|
|
|
|
$card = amisMake()->Card()->className('w-96 m:w-full')->body([
|
|
amisMake()->Flex()->justify('space-between')->alignItems('center')->className('px-2.5 pb-2.5')->items([
|
|
amisMake()->Image()->src(url(Admin::config('admin.logo')))->width(40)->height(40),
|
|
amisMake()->Tpl()->className('font-medium')->tpl('<div style="font-size: 24px">' . Admin::config('admin.name') . '</div>'),
|
|
]),
|
|
$form,
|
|
]);
|
|
|
|
return amisMake()->Page()->css([
|
|
'.captcha-box .cxd-Image--thumb' => [
|
|
'padding' => '0',
|
|
'cursor' => 'pointer',
|
|
'border' => 'var(--Form-input-borderWidth) solid var(--Form-input-borderColor)',
|
|
|
|
'border-top-right-radius' => '4px',
|
|
'border-bottom-right-radius' => '4px',
|
|
],
|
|
'.cxd-Image-thumb' => ['width' => 'auto'],
|
|
'.bg' => [
|
|
'width' => '100%',
|
|
'height' => '100vh',
|
|
'display' => 'flex',
|
|
'align-items' => 'center',
|
|
'justify-content' => 'center',
|
|
'position' => 'relative',
|
|
'background' => 'linear-gradient(200deg, rgb(198, 225, 255) 0%, rgb(64, 128, 255) 100%)',
|
|
],
|
|
])->body(
|
|
amisMake()->Page()->css([
|
|
'.cxd-Page-body' => [
|
|
'display' => 'flex',
|
|
'flex-direction' => 'column',
|
|
'justify-content' => 'center',
|
|
]
|
|
])->className('bg')->body(
|
|
[amisMake()->Wrapper()->className("w-full flex items-center justify-center")->body($card),
|
|
// https://beian.miit.gov.cn
|
|
amisMake()->Link()->className('block text-center text-white')->href('https://beian.miit.gov.cn')->blank()->body($icp_no)]
|
|
)
|
|
);
|
|
}
|
|
|
|
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')));
|
|
}
|
|
}
|