| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\Admin;
- use app\admin\model\AdminLog;
- use app\common\controller\Backend;
- use fast\Random;
- use think\Config;
- use think\Db;
- use think\Exception;
- use think\Hook;
- use think\Session;
- use think\Validate;
- /**
- * 后台首页
- * @internal
- */
- class Index extends Backend
- {
- protected $noNeedLogin = ['login','getCaptcha','checkCaptcha','register'];
- protected $noNeedRight = ['index', 'logout'];
- protected $layout = '';
- public function _initialize()
- {
- parent::_initialize();
- //移除HTML标签
- $this->request->filter('trim,strip_tags,htmlspecialchars');
- }
- /**
- * 后台首页
- */
- public function index()
- {
- $cookieArr = ['adminskin' => "/^skin\-([a-z\-]+)\$/i", 'multiplenav' => "/^(0|1)\$/", 'multipletab' => "/^(0|1)\$/", 'show_submenu' => "/^(0|1)\$/"];
- foreach ($cookieArr as $key => $regex) {
- $cookieValue = $this->request->cookie($key);
- if (!is_null($cookieValue) && preg_match($regex, $cookieValue)) {
- config('fastadmin.' . $key, $cookieValue);
- }
- }
- //左侧菜单
- list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
- 'dashboard' => 'hot',
- 'addon' => ['new', 'red', 'badge'],
- 'auth/rule' => __('Menu'),
- 'general' => ['new', 'purple'],
- ], $this->view->site['fixedpage']);
- $action = $this->request->request('action');
- if ($this->request->isPost()) {
- if ($action == 'refreshmenu') {
- $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
- }
- }
- $this->assignconfig('cookie', ['prefix' => config('cookie.prefix')]);
- $this->view->assign('menulist', $menulist);
- $this->view->assign('navlist', $navlist);
- $this->view->assign('fixedmenu', $fixedmenu);
- $this->view->assign('referermenu', $referermenu);
- $this->view->assign('title', __('Home'));
- return $this->view->fetch();
- }
- /**
- * 管理员登录
- */
- public function login()
- {
- $url = $this->request->get('url', 'index/index');
- if ($this->auth->isLogin()) {
- $this->success(__("You've logged in, do not login again"), $url);
- }
- if ($this->request->isPost()) {
- $keeplogin = $this->request->post('keeplogin');
- $captcha = $this->request->post('captcha');
- if(!$captcha) {
- $username = $this->request->post('username');
- $password = $this->request->post('password');
- $token = $this->request->post('__token__');
- $rule = [
- 'username' => 'require|length:3,30',
- 'password' => 'require|length:3,30',
- '__token__' => 'require|token',
- ];
- $data = [
- 'username' => $username,
- 'password' => $password,
- '__token__' => $token,
- ];
- if (Config::get('fastadmin.login_captcha')) {
- $rule['captcha'] = 'require|captcha';
- $data['captcha'] = $this->request->post('captcha');
- }
- $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
- $result = $validate->check($data);
- if (!$result) {
- $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
- }
- AdminLog::setTitle(__('Login'));
- $result = $this->auth->login($username, $password, $keeplogin ? 2592000 : 0);
- if ($result === true) {
- // 登陆成功后,短信通知账号拥有者
- $mobile = Admin::get($this->auth->id)['mobile'];
- (new Sample)->send_verify($mobile,$username,'SMS_243348221');
- Hook::listen("admin_login_after", $this->request);
- $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
- } else {
- $msg = $this->auth->getError();
- $msg = $msg ? $msg : __('Username or password is incorrect');
- $this->error($msg, $url, ['token' => $this->request->token()]);
- }
- }else{
- $mobile = $this->request->post('mobile');
- $sample = new Sample();
- if(!$sample->checkRegSms($mobile,$captcha)){
- $this->error('验证码无效');
- }
- $tel = Admin::get(['mobile'=>$mobile]);
- // 是否已注册过账号
- if($tel){ // 注册过,直接登录86400
- $result = $this->auth->login($mobile, '', $keeplogin ? 2592000 : 0,1);
- if ($result === true) {
- $mobile = Admin::get($this->auth->id)['mobile'];
- (new Sample)->send_verify($mobile,$mobile,'SMS_243348221');
- Hook::listen("admin_login_after", $this->request);
- $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $tel, 'avatar' => $this->auth->avatar]);
- } else {
- $msg = $this->auth->getError();
- $msg = $msg ? $msg : __('Username or password is incorrect');
- $this->error($msg, $url, ['token' => $this->request->token()]);
- }
- }else{ // 没注册过, 先注册
- $this->register($mobile);
- $this->auth->login($mobile, '', $keeplogin ? 2592000 : 0,1);
- Hook::listen("admin_login_after", $this->request);
- $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $tel, 'avatar' => $this->auth->avatar]);
- }
- }
- }
- // 根据客户端的cookie,判断是否可以自动登录
- if ($this->auth->autologin()) {
- Session::delete("referer");
- $this->redirect($url);
- }
- $background = Config::get('fastadmin.login_background');
- $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
- $this->view->assign('background', $background);
- $this->view->assign('title', __('Login'));
- Hook::listen("admin_login_init", $this->request);
- return $this->view->fetch();
- }
- /**
- * 退出登录
- */
- public function logout()
- {
- if ($this->request->isPost()) {
- $this->auth->logout();
- Hook::listen("admin_logout_after", $this->request);
- $this->success(__('Logout successful'), 'index/login');
- }
- $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
- $html .= "<script>document.forms['logout_submit'].submit();</script>";
- return $html;
- }
- /**
- * 获取验证码
- */
- public function getCaptcha(){
- $mobile = input('mobile');
- $rand = rand(100000,999999);
- $sample = new Sample();
- $sample->send_verify($mobile,$rand,'SMS_170560412');
- return $rand;
- }
- /**
- * 注册账号
- */
- public function register($username){
- if (!Validate::is($username, '\S{6,16}')) {
- $this->error(__("Please input correct password"));
- }
- Db::startTrans();
- try{
- $params['salt'] = Random::alnum();
- $params['username'] = $username;
- $params['nickname'] = $username;
- $params['mobile'] = $username;
- $params['password'] = md5(md5($username) . $params['salt']);
- $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
- $result = Db::name('admin')->insertGetId($params);
- if ($result === false) {
- $this->error($this->model->getError());
- }
- //设置权限
- $admin['group_id'] = 4;
- $admin['uid'] = $result;
- Db::name('auth_group_access')->insert($admin);
- Db::commit();
- }catch(Exception $e){
- Db::rollback();
- }
- }
- }
|