Login.php 2.4 KB

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