Login.php 2.6 KB

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