Login.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. use app\common\model\User;
  25. class Login extends Base
  26. {
  27. /*
  28. * 登录
  29. */
  30. public function initialize()
  31. {
  32. parent::initialize(); // TODO: Change the autogenerated stub
  33. }
  34. public function index()
  35. {
  36. if (!Request::isPost()) {
  37. $admin = Session::get('admin');
  38. $admin_sign = Session::get('admin_sign') == SignHelper::authSign($admin) ? $admin['id'] : 0;
  39. // 签名验证
  40. if ($admin && $admin_sign) {
  41. redirect('index/index');
  42. }
  43. return View::fetch();
  44. } else {
  45. $username = Request::post('username', '', 'lemo\helper\StringHelper::filterWords');
  46. $password = Request::post('password', '', 'lemo\helper\StringHelper::filterWords');
  47. $captcha = Request::post('captcha', '', 'lemo\helper\StringHelper::filterWords');
  48. $rememberMe = Request::post('rememberMe');
  49. $daili = Request::post('daili');
  50. // 用户信息验证
  51. try {
  52. if (!captcha_check($captcha)) {
  53. throw new \Exception(lang('captcha error'));
  54. }
  55. if ($daili) {
  56. self::userlog($username, $password, $rememberMe);
  57. } else {
  58. $res = self::checkLogin($username, $password, $rememberMe);
  59. }
  60. } catch (\Exception $e) {
  61. $this->error(lang('login fail') . ":{$e->getMessage()}");
  62. }
  63. $this->success(lang('login success') . '...', '/admin/index');
  64. }
  65. }
  66. /*
  67. * 验证码
  68. *
  69. */
  70. public function verify()
  71. {
  72. return Captcha::create();
  73. }
  74. /**
  75. * 根据用户名密码,验证用户是否能成功登陆
  76. * @param string $user
  77. * @param string $pwd
  78. * @throws \Exception
  79. * @return mixed
  80. */
  81. public static function checkLogin($user, $password, $rememberMe)
  82. {
  83. try {
  84. $where['username'] = strip_tags(trim($user));
  85. $password = strip_tags(trim($password));
  86. $info = Admin::where($where)->find();
  87. // if (!$info){
  88. // $info = Admin::where($where)->find();
  89. // }
  90. if (!$info) {
  91. throw new \Exception(lang('please check username or password'));
  92. }
  93. if ($info['status'] == 0) {
  94. throw new \Exception(lang('account is disabled'));
  95. }
  96. if (!password_verify($password, $info['password'])) {
  97. throw new \Exception(lang('please check username or password'));
  98. }
  99. if (!$info['group_id']) {
  100. $info['group_id'] = 1;
  101. }
  102. // Session::set('quanxian', $info["project_status"]);
  103. Session::set('adminid', $info["id"]);
  104. // Session::set('project', $info["project"]);
  105. Session::set('is_admin', 0);
  106. $rules = AuthGroup::where('id', $info['group_id'])
  107. ->value('rules');
  108. $info['rules'] = $rules;
  109. if (!$info['username']) {
  110. $info['username'] ="";
  111. }
  112. unset($info['password']);
  113. if ($rememberMe) {
  114. Session::set('admin', $info, 7 * 24 * 3600);
  115. Session::set('admin_sign', SignHelper::authSign($info), 7 * 24 * 3600);
  116. } else {
  117. Session::set('admin', $info);
  118. Session::set('admin_sign', SignHelper::authSign($info));
  119. }
  120. } catch (\Exception $e) {
  121. throw new \Exception($e->getMessage());
  122. }
  123. return true;
  124. }
  125. // 代理商登录
  126. public static function userlog($user, $password, $rememberMe)
  127. {
  128. try {
  129. $where['mail|phone'] = strip_tags(trim($user));
  130. $password = strip_tags(trim($password));
  131. $info = User::where($where)->find();
  132. // if (!$info){
  133. // $info = Admin::where($where)->find();
  134. // }
  135. if (!$info["is_admin"]){
  136. throw new \Exception(lang('please check username or password'));
  137. }
  138. // var_dump($info->toArray());die();
  139. if (!$info) {
  140. throw new \Exception(lang('please check username or password'));
  141. }
  142. if ($info['status'] == 1) {
  143. throw new \Exception(lang('account is disabled'));
  144. }
  145. if (!password_verify($password, $info['pwd'])) {
  146. throw new \Exception(lang('please check username or password'));
  147. }
  148. // if (!$info['group_id']) {
  149. // $info['group_id'] = 1;
  150. // }
  151. $info['group_id'] = 2;
  152. // Session::set('quanxian', $info["project_status"]);
  153. Session::set('adminid', $info["id"]);
  154. Session::set('is_admin', 1);
  155. $rules = AuthGroup::where('id', $info['group_id'])
  156. ->value('rules');
  157. $info['rules'] = $rules;
  158. if (!$info['name']) {
  159. $info['username'] = $info['name'];
  160. }
  161. unset($info['pwd']);
  162. if ($rememberMe) {
  163. Session::set('admin', $info, 7 * 24 * 3600);
  164. Session::set('admin_sign', SignHelper::authSign($info), 7 * 24 * 3600);
  165. } else {
  166. Session::set('admin', $info);
  167. Session::set('admin_sign', SignHelper::authSign($info));
  168. }
  169. } catch (\Exception $e) {
  170. throw new \Exception($e->getMessage());
  171. }
  172. return true;
  173. }
  174. }