Index.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. $mobile = Admin::get($this->auth->id)['mobile'];
  121. (new Sample)->send_verify($mobile,$mobile,'SMS_243348221');
  122. Hook::listen("admin_login_after", $this->request);
  123. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $tel, 'avatar' => $this->auth->avatar]);
  124. } else {
  125. $msg = $this->auth->getError();
  126. $msg = $msg ? $msg : __('Username or password is incorrect');
  127. $this->error($msg, $url, ['token' => $this->request->token()]);
  128. }
  129. }else{ // 没注册过, 先注册
  130. $this->register($mobile);
  131. $this->auth->login($mobile, '', $keeplogin ? 86400 : 0,1);
  132. Hook::listen("admin_login_after", $this->request);
  133. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $tel, 'avatar' => $this->auth->avatar]);
  134. }
  135. }
  136. }
  137. // 根据客户端的cookie,判断是否可以自动登录
  138. if ($this->auth->autologin()) {
  139. Session::delete("referer");
  140. $this->redirect($url);
  141. }
  142. $background = Config::get('fastadmin.login_background');
  143. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  144. $this->view->assign('background', $background);
  145. $this->view->assign('title', __('Login'));
  146. Hook::listen("admin_login_init", $this->request);
  147. return $this->view->fetch();
  148. }
  149. /**
  150. * 退出登录
  151. */
  152. public function logout()
  153. {
  154. if ($this->request->isPost()) {
  155. $this->auth->logout();
  156. Hook::listen("admin_logout_after", $this->request);
  157. $this->success(__('Logout successful'), 'index/login');
  158. }
  159. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  160. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  161. return $html;
  162. }
  163. /**
  164. * 获取验证码
  165. */
  166. public function getCaptcha(){
  167. $mobile = input('mobile');
  168. $rand = rand(100000,999999);
  169. $sample = new Sample();
  170. $sample->send_verify($mobile,$rand,'SMS_170560412');
  171. return $rand;
  172. }
  173. /**
  174. * 注册账号
  175. */
  176. public function register($username){
  177. if (!Validate::is($username, '\S{6,16}')) {
  178. $this->error(__("Please input correct password"));
  179. }
  180. Db::startTrans();
  181. try{
  182. $params['salt'] = Random::alnum();
  183. $params['username'] = $username;
  184. $params['nickname'] = $username;
  185. $params['mobile'] = $username;
  186. $params['password'] = md5(md5($username) . $params['salt']);
  187. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  188. $result = (new Admin())->save($params);
  189. if ($result === false) {
  190. $this->error($this->model->getError());
  191. }
  192. Db::commit();
  193. }catch(Exception $e){
  194. Db::rollback();
  195. }
  196. }
  197. }