Index.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 fast\Random;
  7. use think\Config;
  8. use think\Db;
  9. use think\Exception;
  10. use think\Hook;
  11. use think\Session;
  12. use think\Validate;
  13. /**
  14. * 后台首页
  15. * @internal
  16. */
  17. class Index extends Backend
  18. {
  19. protected $noNeedLogin = ['login','getCaptcha','checkCaptcha','register'];
  20. protected $noNeedRight = ['index', 'logout'];
  21. protected $layout = '';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. //移除HTML标签
  26. $this->request->filter('trim,strip_tags,htmlspecialchars');
  27. }
  28. /**
  29. * 后台首页
  30. */
  31. public function index()
  32. {
  33. $cookieArr = ['adminskin' => "/^skin\-([a-z\-]+)\$/i", 'multiplenav' => "/^(0|1)\$/", 'multipletab' => "/^(0|1)\$/", 'show_submenu' => "/^(0|1)\$/"];
  34. foreach ($cookieArr as $key => $regex) {
  35. $cookieValue = $this->request->cookie($key);
  36. if (!is_null($cookieValue) && preg_match($regex, $cookieValue)) {
  37. config('fastadmin.' . $key, $cookieValue);
  38. }
  39. }
  40. //左侧菜单
  41. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  42. 'dashboard' => 'hot',
  43. 'addon' => ['new', 'red', 'badge'],
  44. 'auth/rule' => __('Menu'),
  45. 'general' => ['new', 'purple'],
  46. ], $this->view->site['fixedpage']);
  47. $action = $this->request->request('action');
  48. if ($this->request->isPost()) {
  49. if ($action == 'refreshmenu') {
  50. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  51. }
  52. }
  53. $this->assignconfig('cookie', ['prefix' => config('cookie.prefix')]);
  54. $this->view->assign('menulist', $menulist);
  55. $this->view->assign('navlist', $navlist);
  56. $this->view->assign('fixedmenu', $fixedmenu);
  57. $this->view->assign('referermenu', $referermenu);
  58. $this->view->assign('title', __('Home'));
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 管理员登录
  63. */
  64. public function login()
  65. {
  66. $url = $this->request->get('url', 'index/index');
  67. if ($this->auth->isLogin()) {
  68. $this->success(__("You've logged in, do not login again"), $url);
  69. }
  70. if ($this->request->isPost()) {
  71. $keeplogin = $this->request->post('keeplogin');
  72. $captcha = $this->request->post('captcha');
  73. if(!$captcha) {
  74. $username = $this->request->post('username');
  75. $password = $this->request->post('password');
  76. $token = $this->request->post('__token__');
  77. $rule = [
  78. 'username' => 'require|length:3,30',
  79. 'password' => 'require|length:3,30',
  80. '__token__' => 'require|token',
  81. ];
  82. $data = [
  83. 'username' => $username,
  84. 'password' => $password,
  85. '__token__' => $token,
  86. ];
  87. if (Config::get('fastadmin.login_captcha')) {
  88. $rule['captcha'] = 'require|captcha';
  89. $data['captcha'] = $this->request->post('captcha');
  90. }
  91. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  92. $result = $validate->check($data);
  93. if (!$result) {
  94. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  95. }
  96. AdminLog::setTitle(__('Login'));
  97. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  98. if ($result === true) {
  99. // 登陆成功后,短信通知账号拥有者
  100. $mobile = Admin::get($this->auth->id)['mobile'];
  101. (new Sample)->send_verify($mobile,$username,'SMS_243348221');
  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. }else{
  110. $mobile = $this->request->post('mobile');
  111. $sample = new Sample();
  112. if(!$sample->checkRegSms($mobile,$captcha)){
  113. $this->error('验证码无效');
  114. }
  115. $tel = Admin::get(['mobile'=>$mobile]);
  116. // 是否已注册过账号
  117. if($tel){ // 注册过,直接登录
  118. $result = $this->auth->login($mobile, '', $keeplogin ? 86400 : 0,1);
  119. if ($result === true) {
  120. Hook::listen("admin_login_after", $this->request);
  121. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $tel, 'avatar' => $this->auth->avatar]);
  122. } else {
  123. $msg = $this->auth->getError();
  124. $msg = $msg ? $msg : __('Username or password is incorrect');
  125. $this->error($msg, $url, ['token' => $this->request->token()]);
  126. }
  127. }else{ // 没注册过, 先注册
  128. $this->register($mobile);
  129. $this->auth->login($mobile, '', $keeplogin ? 86400 : 0,1);
  130. Hook::listen("admin_login_after", $this->request);
  131. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $tel, 'avatar' => $this->auth->avatar]);
  132. }
  133. }
  134. }
  135. // 根据客户端的cookie,判断是否可以自动登录
  136. if ($this->auth->autologin()) {
  137. Session::delete("referer");
  138. $this->redirect($url);
  139. }
  140. $background = Config::get('fastadmin.login_background');
  141. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  142. $this->view->assign('background', $background);
  143. $this->view->assign('title', __('Login'));
  144. Hook::listen("admin_login_init", $this->request);
  145. return $this->view->fetch();
  146. }
  147. /**
  148. * 退出登录
  149. */
  150. public function logout()
  151. {
  152. if ($this->request->isPost()) {
  153. $this->auth->logout();
  154. Hook::listen("admin_logout_after", $this->request);
  155. $this->success(__('Logout successful'), 'index/login');
  156. }
  157. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  158. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  159. return $html;
  160. }
  161. /**
  162. * 获取验证码
  163. */
  164. public function getCaptcha(){
  165. $mobile = input('mobile');
  166. $rand = rand(100000,999999);
  167. $sample = new Sample();
  168. $sample->send_verify($mobile,$rand,'SMS_170560412');
  169. return $rand;
  170. }
  171. /**
  172. * 注册账号
  173. */
  174. public function register($username){
  175. if (!Validate::is($username, '\S{6,16}')) {
  176. $this->error(__("Please input correct password"));
  177. }
  178. Db::startTrans();
  179. try{
  180. $params['salt'] = Random::alnum();
  181. $params['username'] = $username;
  182. $params['nickname'] = $username;
  183. $params['mobile'] = $username;
  184. $params['password'] = md5(md5($username) . $params['salt']);
  185. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  186. $result = (new Admin())->save($params);
  187. if ($result === false) {
  188. $this->error($this->model->getError());
  189. }
  190. Db::commit();
  191. }catch(Exception $e){
  192. Db::rollback();
  193. }
  194. }
  195. }