Index.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Admin;
  4. use app\admin\model\AdminLog;
  5. use app\common\controller\Backend;
  6. use app\common\library\Sms;
  7. use app\common\service\Qiyu;
  8. use think\Config;
  9. use think\Hook;
  10. use think\Validate;
  11. /**
  12. * 后台首页
  13. * @internal
  14. */
  15. class Index extends Backend
  16. {
  17. protected $noNeedLogin = ['login','login_sms'];
  18. protected $noNeedRight = ['index', 'logout','kefu'];
  19. protected $layout = '';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. //移除HTML标签
  24. $this->request->filter('trim,strip_tags,htmlspecialchars');
  25. }
  26. /**
  27. * 后台首页
  28. */
  29. public function index()
  30. {
  31. //左侧菜单
  32. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  33. //'dashboard' => 'hot',
  34. //'addon' => ['new', 'red', 'badge'],
  35. 'auth/rule' => __('Menu'),
  36. //'general' => ['new', 'purple'],
  37. ], $this->view->site['fixedpage']);
  38. $action = $this->request->request('action');
  39. if ($this->request->isPost()) {
  40. if ($action == 'refreshmenu') {
  41. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  42. }
  43. }
  44. $this->view->assign('menulist', $menulist);
  45. $this->view->assign('navlist', $navlist);
  46. $this->view->assign('fixedmenu', $fixedmenu);
  47. $this->view->assign('referermenu', $referermenu);
  48. $this->view->assign('title', __('Home'));
  49. return $this->view->fetch();
  50. }
  51. public function kefu(){
  52. return json([
  53. 'url'=>Qiyu::instance()->login($this->admin()),
  54. ]);
  55. }
  56. /**
  57. * 管理员登录
  58. */
  59. public function login()
  60. {
  61. $url = $this->request->get('url', 'index/index');
  62. if ($this->auth->isLogin()) {
  63. $this->success(__("You've logged in, do not login again"), $url);
  64. }
  65. if ($this->request->isPost()) {
  66. $username = $this->request->post('username');
  67. $password = $this->request->post('password');
  68. $keeplogin = $this->request->post('keeplogin');
  69. $token = $this->request->post('__token__');
  70. $rule = [
  71. 'username' => 'require|length:3,30',
  72. 'password' => 'require|length:3,30',
  73. //'captcha_sms|手机验证码' => 'require|length:3,30',
  74. '__token__' => 'require|token',
  75. ];
  76. $data = [
  77. 'username' => $username,
  78. 'password' => $password,
  79. //'captcha_sms'=> input('captcha_sms'),
  80. '__token__' => $token,
  81. ];
  82. if (Config::get('fastadmin.login_captcha')) {
  83. $rule['captcha'] = 'require|captcha';
  84. $data['captcha'] = $this->request->post('captcha');
  85. }
  86. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  87. $result = $validate->check($data);
  88. if (!$result) {
  89. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  90. }
  91. $admin=Admin::where('username',$data['username'])->find();
  92. if(!$admin){
  93. $this->error('用户名或密码有错误');
  94. }
  95. /*$checkSms = Sms::check($admin['phone'],$data['captcha_sms'],'admin_login');
  96. if(!$checkSms){
  97. $this->error('手机验证码错误');
  98. }*/
  99. AdminLog::setTitle(__('Login'));
  100. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  101. if ($result === true) {
  102. Hook::listen("admin_login_after", $this->request);
  103. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  104. } else {
  105. $msg = $this->auth->getError();
  106. $msg = $msg ? $msg : __('Username or password is incorrect');
  107. $this->error($msg, $url, ['token' => $this->request->token()]);
  108. }
  109. }
  110. // 根据客户端的cookie,判断是否可以自动登录
  111. if ($this->auth->autologin()) {
  112. $this->redirect($url);
  113. }
  114. $background = Config::get('fastadmin.login_background');
  115. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  116. $this->view->assign('background', $background);
  117. $this->view->assign('title', __('Login'));
  118. Hook::listen("admin_login_init", $this->request);
  119. return $this->view->fetch();
  120. }
  121. /**
  122. * 退出登录
  123. */
  124. public function logout()
  125. {
  126. if ($this->request->isPost()) {
  127. $this->auth->logout();
  128. Hook::listen("admin_logout_after", $this->request);
  129. $this->success(__('Logout successful'), 'index/login');
  130. }
  131. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  132. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  133. return $html;
  134. }
  135. public function login_sms(){
  136. $this->validateFailException();
  137. $data=input();
  138. $this->validate($data,[
  139. 'username|用户名'=>['require'],
  140. ]);
  141. $admin=Admin::where('username',$data['username'])->find();
  142. if(!$admin){
  143. $this->error('用户不存在');
  144. }
  145. if($admin['id']==1 && !$admin['phone']){
  146. $this->success();
  147. }else{
  148. if(!$admin['phone']){
  149. $this->error('发送失败');
  150. }
  151. if($admin['status']=='hidden'){
  152. $this->error('请联系管理员陈伟15555556609开通权限');
  153. }
  154. }
  155. Sms::send($admin['phone'],null,'admin_login');
  156. $this->success();
  157. }
  158. }