Login.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\base\controller;
  3. use think\Controller;
  4. use think\Session;
  5. use app\user\model\Admin;
  6. class Login extends Controller
  7. {
  8. private $adminModel;
  9. protected function _initialize(){
  10. $this->adminModel = new Admin();
  11. }
  12. public function login(){
  13. $this->view->engine->layout(false);
  14. if ($this->request->isPost()){
  15. // 判断验证码
  16. $username = $this->request->post('username');
  17. $password = $this->request->post('password');
  18. // 判断用户名
  19. if (empty($username)) {
  20. return $this->error("用户名不能为空", $this->request->baseUrl());
  21. }
  22. // 判断密码
  23. if (empty($password)) {
  24. return $this->error("密码不能为空", $this->request->baseUrl());
  25. }
  26. $res = $this->adminModel->login($username, $password);
  27. if($res){
  28. Session::set('admin', $res);
  29. $this->success("登录成功", url('user/index/index'));
  30. } else {
  31. $this->error("用户不存在或密码不正确", $this->request->baseUrl());
  32. }
  33. }
  34. return $this->fetch('login');
  35. }
  36. public function out(){
  37. session(null);
  38. $this->redirect(url('base/login/login'));
  39. }
  40. public function register(){
  41. $this->view->engine->layout(false);
  42. if($this->request->isPost()){
  43. $this->adminModel->register();
  44. }
  45. return $this->fetch('register');
  46. }
  47. }