Login.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User: 林志杰
  5. * Email: [email protected]
  6. * Time: 2020/1/2 12:58
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\validate\AdminUser as AdminUserValidate;
  10. use app\common\model\mysql\AdminUser;
  11. use think\facade\View;
  12. class Login extends AdminBase
  13. {
  14. public function initialize()
  15. {
  16. if ($this->isLogin()) {
  17. return $this->redirect(url('index/index'), 302);
  18. }
  19. }
  20. public function index()
  21. {
  22. return View::fetch();
  23. }
  24. public function md5()
  25. {
  26. halt(session(config('admin.session_admin')));
  27. echo md5('admin_singwa_abc');
  28. }
  29. public function check()
  30. {
  31. if (!$this->request->isPost()) {
  32. return show(config('status.error'), '请求方式错误');
  33. }
  34. // 参数校验 1、原生方式 2、TP6 验证机制
  35. $username = $this->request->param('username', '', 'trim');
  36. $password = $this->request->param('password', '', 'trim');
  37. $captcha = $this->request->param('captcha', '', 'trim');
  38. $data = [
  39. 'username' => $username,
  40. 'password' => $password,
  41. 'captcha' => $captcha,
  42. ];
  43. $validate = new AdminUserValidate();
  44. if ($validate->check($data)) {
  45. return show(config('status.error'), $validate->getError());
  46. }
  47. // if (empty($username) || empty($password) || empty($captcha)) {
  48. // return show(config('status.error'), '参数不能为空');
  49. // }
  50. // 验证码校验
  51. // if (!captcha_check($captcha)) {
  52. // // 验证码校验失败
  53. // return show(config('status.error'), '验证码不正确');
  54. // }
  55. try {
  56. $adminUserObj = new AdminUser();
  57. $adminUser = $adminUserObj->getAdminUserByUsername($username);
  58. if (empty($adminUser) || $adminUser->status != config('status.mysql.table_normal')) {
  59. return show(config('status.error'), '不存在该用户');
  60. }
  61. $adminUser = $adminUser->toArray();
  62. if ($adminUser['password'] !== md5($password . '_singwa_abc')) {
  63. return show(config('status.error'), '密码错误');
  64. }
  65. // 记录session
  66. session(config('admin.session_admin'), $adminUser);
  67. // 记录信息到mysql中
  68. $updateData = [
  69. 'last_login_time' => time(),
  70. 'last_login_ip' => request()->ip(),
  71. 'update_time' => time(),
  72. ];
  73. $res = $adminUserObj->updateById($adminUser['id'], $updateData);
  74. if (empty($res)) {
  75. return show(config('status.error'), '登陆失败');
  76. }
  77. } catch (\Exception $e) {
  78. // todo 记录日志 $e->getMessage()
  79. return show(config('status.error'), '内部异常,登陆失败');
  80. }
  81. return show(config('status.success'), '登陆成功');
  82. }
  83. }