Common.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\Seeker;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. class Common extends Controller
  8. {
  9. public function initialize()
  10. {
  11. parent::initialize();
  12. }
  13. public function login(){
  14. if(request()->isPost()){
  15. $username = input('?post.account')?input('post.account','','trim'):'';
  16. $password = input('?post.password')?input('post.password','','trim'):'';
  17. if(!$username||!$password){
  18. $this->error('用户名或密码错误');
  19. }
  20. $ret = (new \app\common\util\ThrottlesUtil(config('app.login_throttles')))->tooManyAttempts($username); // 登录限流
  21. if($ret){
  22. $this->error('账号已被锁定,请稍后重试');
  23. }
  24. $info = Db::name('user')->where('account',$username)->where('del',0)->find();
  25. if(empty($info)) $this->error('用户信息不存在');
  26. if($info['enable']==0) $this->error('该账号被禁用');
  27. if(!password_verify($password,$info['password'])){
  28. $this->error('用户名或密码错误');
  29. }
  30. $rolesId = Db::name('user_roles')->where('user_id',$info['id'])->value('roles_id');
  31. if(!$rolesId){
  32. $this->error('用户未设置角色,无法登陆');
  33. }
  34. $orgs = model('Org')->getListByRoles($info['id']);
  35. if(empty($orgs)){
  36. $this->error('用户没有组织,无法登陆');
  37. }
  38. /* 更新登录信息 */
  39. $data = array(
  40. 'last_login_time' => date('Y-m-d H:i:s')
  41. );
  42. Db::name('user')->where('id',$info['id'])->update($data);
  43. /* 记录登录SESSION和COOKIES */
  44. $auth = array(
  45. 'id' => $info['id'],
  46. 'account' => $info['account'],
  47. 'real_name' => $info['real_name'],
  48. 'last_login_time' => $data['last_login_time'],
  49. 'rolesId' => $rolesId?$rolesId:0
  50. );
  51. session('user_auth',$auth);
  52. session('user_auth_sign',data_auth_sign($auth));
  53. session('orgId',$orgs[0]['id']);
  54. session('orgName',$orgs[0]['name']);
  55. (new \app\common\util\ThrottlesUtil(config('app.login_throttles')))->resetAttempts($username); // 登录成功,重置限流
  56. $url = $_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:url('Index/index');
  57. $this->success('登录成功',$url);
  58. }else{
  59. $config = Db::name('config')
  60. ->where('name','web_site_title')
  61. ->value('value');
  62. if(is_login()){
  63. $this->redirect(url('Index/index'));
  64. }
  65. $this->assign('title',$config);
  66. return $this->fetch();
  67. }
  68. }
  69. public function forget(){
  70. if(request()->isPost()) {
  71. $username = input('?post.account')?input('post.account','','trim'):'';
  72. $password = input('?post.password')?input('post.password','','trim'):'';
  73. $code = input('?post.code')?input('post.code','','trim'):'';
  74. if(!$username){
  75. $this->error('手机号不能为空');
  76. }
  77. if(!$code){
  78. $this->error('验证码不能为空');
  79. }
  80. if(!$password){
  81. $this->error('新密码不能为空');
  82. }
  83. if(!verify_sms($username,$code)){
  84. $this->error('验证码信息错误');
  85. }
  86. $info = Db::name('user')
  87. ->where('mobile',$username)
  88. ->where('del',0)->find();
  89. if(empty($info)) $this->error('用户信息不存在');
  90. if($info['enable']==0) $this->error('该账号被禁用');
  91. $pas = password_hash($password, PASSWORD_DEFAULT);
  92. $sdata = [
  93. 'update_time' => date('Y-m-d H:i:s'),
  94. 'password' =>$pas
  95. ];
  96. $res = Db::name('user')
  97. ->where('id',$info['id'])
  98. ->update($sdata);
  99. $res?$this->success('修改成功',url('common/login')):$this->error('修改失败');
  100. }else{
  101. $config = Db::name('config')
  102. ->where('name', 'web_site_title')
  103. ->value('value');
  104. $this->assign('title',$config);
  105. return $this->fetch();
  106. }
  107. }
  108. public function sms(){
  109. $phone = input('mobile');
  110. if(empty($phone)){
  111. $this->error('手机号不能为空');
  112. }
  113. $res = send_verify_sms($phone);
  114. if(!$res){
  115. $this->error('发送失败');
  116. }
  117. $this->success('发送成功');
  118. }
  119. /**
  120. * 退出登录
  121. */
  122. public function logout(){
  123. session('user_auth',null);
  124. session('user_auth_sign',null);
  125. $this->redirect(url('Common/login'));
  126. }
  127. /**
  128. * 无权限跳转页面
  129. */
  130. public function access(){
  131. return $this->fetch();
  132. }
  133. /**
  134. * 403页面
  135. */
  136. public function forbid(){
  137. return $this->fetch('403');
  138. }
  139. }