// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\member\controller; use app\member\validate\Login as LoginValidate; use think\exception\ValidateException; use think\facade\Event; use think\facade\View; use think\Request; use ywxapp\controller\MemberBase; /** * Login 类 * * @author ywxapp */ class Login extends MemberBase { /** * 不需要登录验证的方法 * @var array */ protected $noNeedLogin = ['*']; /** * 不需要权限验证的方法 * @var array */ protected $noNeedVerify = ['*']; /** * 控制器初始化 initialize * @return void */ protected function initialize() {} /** * 显示资源列表 * * @param \think\Request $request * @return \think\Response */ public function index() { $this->view->layout(false); View::assign('title', '登录'); return View::fetch(); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function save() { if ($this->request->isPost()) { $data = $this->request->param(); try { validate(LoginValidate::class)->check($data); $this->app->event->trigger('UserLoginBefore', $data); $this->auth->login($data['username'], $data['password'], true); if ($this->auth->isLogin) { return $this->result->success(); } } 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('退出登录成功'); } }