Files
YwxAppThink/app/backend/controller/Login.php
T

112 lines
2.8 KiB
PHP

<?php
/*
* @Author: YwxApp <ywx@ywxapp.cn>
* @Date: 2026-04-29 02:32:33
* @LastEditors: YwxApp <ywx@ywxapp.cn>
* @LastEditTime: 2026-08-10 09:32:39
* @Description:
* @FilePath: \ywxapp_dev\app\backend\controller\Login.php
* @CustomString: Copyright (c) 2026 YwxApp
*/
declare(strict_types=1);
namespace app\backend\controller;
use app\backend\validate\Login as LoginValidate;
use think\exception\ValidateException;
use think\facade\View;
use think\Request;
use ywxapp\controller\BackendBase;
/**
* Login 类
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Login extends BackendBase
{
/**
* Summary of needLogin
* @var array
*/
protected $noNeedLogin = ['*'];
/**
* Summary of needRight
* @var array
*/
protected $noNeedVerify = ['*'];
/**
* 控制器初始化 initialize
* @return void
*/
protected function initialize()
{
$this->model = new \ywxapp\model\BackendAdmin();
}
/**
* 显示资源列表
*
* @param \think\Request $request
* @return \think\Response
*/
public function index()
{
View::assign('title', '登录');
View::layout(false);
return View::fetch();
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save()
{
if ($this->request->isAjax() && $this->request->isPost()) {
$data = $this->request->param();
try {
validate(LoginValidate::class)->check($data);
$this->auth->login($data['username'], $data['password']);
if ($this->auth->isLogin) {
$adminId = $this->auth->model->id ?? null;
$info = $this->model->with('roles')->find($adminId);
if (! $info) {
$this->result->error(lang('Member not found'), 1);
}
// 获取扁平权限列表(供前端按钮控制):返回权限 key 字符串数组
$permissions = $info->getPermissionNames();
$flatMenus = $info->getAccessibleMenus();
$this->result->success([
'permissions' => $permissions,
'menus' => $flatMenus
]);
}
} catch (ValidateException $e) {
$this->result->error('验证错误: ' . $e->getMessage(), 1);
}
}
$this->result->error("访问错误");
}
/**
* 退出登录
*
* @param \think\Request $request
* @return \think\Response
*/
public function logout()
{
$this->auth->logout();
$this->result->success('退出登录成功');
}
}