Login.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\AdminUser;
  3. use App\Servers\Tencent\Sms;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * 管理后台登录控制器
  7. *
  8. * @author 刘相欣
  9. *
  10. * */
  11. class Login extends Admin {
  12. /**
  13. * 登录方法
  14. *
  15. * */
  16. public function index(AdminUser $AdminUser){
  17. if( request()->isMethod('post') ){
  18. // 接收数据
  19. $username = request('username','');
  20. // 接收数据
  21. $code = request('code','');
  22. // 验证
  23. if( !$username || !$code ) return json_send(['code'=>'error','msg'=>'用户名验证码必填']);
  24. // 查询用户
  25. $uid = $AdminUser->orWhere('username',$username)->orWhere('phone',$username)->value('uid');
  26. // 获取数据
  27. $admin = $AdminUser->getOne($uid);
  28. // 用户不存在
  29. if( !$admin || $admin['status'] ) return json_send(['code'=>'error','msg'=>'用户名不存在或已被停用']);
  30. // 比对密码
  31. if( md5($code) != $admin['password'] ) return json_send(['code'=>'error','msg'=>'密码错误或账号不存在']);
  32. // 查询用户的组别
  33. $group = DB::table('auth_group_access')->where(['user_uid'=>$admin['uid']])->pluck('group_id')->toArray();
  34. // 权限小组
  35. $admin['group_id'] = $group;
  36. // 删除敏感数据-密码
  37. unset($admin['password']);
  38. // 存储登录状态
  39. session(['userAuth'=>$admin]);
  40. // 表单令牌
  41. return json_send(['code'=>'success','msg'=>'登录成功','path'=>url('admin')]);
  42. }
  43. // 加载模板
  44. return $this->fetch();
  45. }
  46. /**
  47. * 退出登录方法
  48. *
  49. * */
  50. public function out(){
  51. // 清除
  52. session()->flush();
  53. // 跳转
  54. return $this->error('退出成功',url('admin/login/index'));
  55. }
  56. /**
  57. * 发送验证码
  58. *
  59. * */
  60. public function send_code(Sms $Sms){
  61. if( request()->isMethod('post') ){
  62. // 接收数据
  63. $username = request('username','');
  64. // 如果有数据,并且短信验证码时间未过时
  65. if( !$username ) return json_send(['code'=>'error','msg'=>'用户名必填']);
  66. // 获取数据
  67. $session = session('validSmsCode');
  68. // 如果有数据,并且验证码创建的时间在一分钟之内
  69. if( $session && time() - $session['create_time'] < 60 ) return json_send(['code'=>'error','msg'=>'请稍后再试']);
  70. // 查询用户
  71. $admin = DB::table('admin')->orWhere('username',$username)->orWhere('phone',$username)->first(['uid','username','phone','status']);
  72. // 用户不存在
  73. if( !$admin || $admin['status']) return json_send(['code'=>'error','msg'=>'用户名不存在或已被停用']);
  74. // 生成验证码
  75. $code = strval(rand(100000,999999));
  76. // 调用验证码发送
  77. $result = $Sms->send($admin['phone'],$code);
  78. // 如果失败
  79. if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>$result['error']]);
  80. // session数据
  81. $session = ['code'=>$code,'phone'=>$admin['phone'],'create_time'=>time()];
  82. // 存入session
  83. session(['validSmsCode'=>$session]);
  84. // 成功结果
  85. return json_send(['code'=>'success','msg'=>'发送成功']);
  86. }
  87. }
  88. }