Login.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/2
  12. */
  13. namespace app\admin\controller;
  14. use app\admin\model\Admin;
  15. use app\admin\model\AuthGroup;
  16. use app\BaseController;
  17. use app\common\controller\Backend;
  18. use app\common\controller\Base;
  19. use lemo\helper\SignHelper;
  20. use think\facade\Session;
  21. use think\facade\View;
  22. use think\facade\Request;
  23. use think\captcha\facade\Captcha;
  24. class Login extends Base {
  25. /*
  26. * 登录
  27. */
  28. public function initialize()
  29. {
  30. parent::initialize(); // TODO: Change the autogenerated stub
  31. }
  32. public function index(){
  33. if (!Request::isPost()) {
  34. $admin= Session::get('admin');
  35. $admin_sign= Session::get('admin_sign') == SignHelper::authSign($admin) ? $admin['id'] : 0;
  36. // 签名验证
  37. if ($admin && $admin_sign) {
  38. redirect('index/index');
  39. }
  40. return View::fetch();
  41. } else {
  42. $username = Request::post('username', '', 'lemo\helper\StringHelper::filterWords');
  43. $password = Request::post('password', '', 'lemo\helper\StringHelper::filterWords');
  44. $captcha = Request::post('captcha', '', 'lemo\helper\StringHelper::filterWords');
  45. $rememberMe = Request::post('rememberMe');
  46. // 用户信息验证
  47. try {
  48. if(!captcha_check($captcha)){
  49. throw new \Exception(lang('captcha error'));
  50. }
  51. $res = self::checkLogin($username, $password,$rememberMe);
  52. } catch (\Exception $e) {
  53. $this->error(lang('login fail').":{$e->getMessage()}");
  54. }
  55. $this->success(lang('login success').'...', '/admin/index');
  56. }
  57. }
  58. /*
  59. * 验证码
  60. *
  61. */
  62. public function verify()
  63. {
  64. return Captcha::create();
  65. }
  66. /**
  67. * 根据用户名密码,验证用户是否能成功登陆
  68. * @param string $user
  69. * @param string $pwd
  70. * @throws \Exception
  71. * @return mixed
  72. */
  73. public static function checkLogin($user, $password,$rememberMe) {
  74. try{
  75. $where['username'] = strip_tags(trim($user));
  76. $password = strip_tags(trim($password));
  77. $info = Admin::where($where)->find();
  78. // if (!$info){
  79. // $info = Admin::where($where)->find();
  80. // }
  81. if(!$info){
  82. throw new \Exception(lang('please check username or password'));
  83. }
  84. if($info['status']==0){
  85. throw new \Exception(lang('account is disabled'));
  86. }
  87. if(!password_verify($password,$info['password'])){
  88. throw new \Exception(lang('please check username or password'));
  89. }
  90. if(!$info['group_id']){
  91. $info['group_id'] = 1;
  92. }
  93. Session::set('quanxian', $info["project_status"]);
  94. Session::set('adminid', $info["id"]);
  95. Session::set('project', $info["project"]);
  96. $rules = AuthGroup::where('id',$info['group_id'])
  97. ->value('rules');
  98. $info['rules'] = $rules ;
  99. if(!$info['username']){
  100. $info['username'] = $info['username'];
  101. }
  102. unset($info['password']);
  103. if($rememberMe){
  104. Session::set('admin', $info,7*24*3600);
  105. Session::set('admin_sign', SignHelper::authSign($info),7*24*3600);
  106. }else{
  107. Session::set('admin', $info);
  108. Session::set('admin_sign', SignHelper::authSign($info));
  109. }
  110. }catch (\Exception $e) {
  111. throw new \Exception($e->getMessage());
  112. }
  113. return true;
  114. }
  115. }