100 lines
2.6 KiB
PHP
100 lines
2.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace app\adminapi\controller;
|
||
|
|
||
|
|
||
|
use app\adminapi\model\Admin as AdminModel;
|
||
|
use app\common\controller\AdminApi;
|
||
|
use think\Cookie;
|
||
|
use think\Hook;
|
||
|
|
||
|
|
||
|
class Admin extends AdminApi
|
||
|
{
|
||
|
|
||
|
protected $model = null;
|
||
|
|
||
|
|
||
|
protected $noNeedLogin = ['login'];
|
||
|
protected $noNeedRight = '*';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 初始化操作
|
||
|
* @access protected
|
||
|
*/
|
||
|
public function _initialize()
|
||
|
{
|
||
|
$this->model = new AdminModel;
|
||
|
parent::_initialize();
|
||
|
|
||
|
$auth = $this->auth;
|
||
|
//监听注册登录退出的事件
|
||
|
Hook::add('admin_login_successed', function ($user) use ($auth) {
|
||
|
$expire = input('post.keeplogin') ? 30 * 86400 : 0;
|
||
|
Cookie::set('adminid', $user->id, $expire);
|
||
|
Cookie::set('admintoken', $auth->getToken(), $expire);
|
||
|
});
|
||
|
Hook::add('admin_register_successed', function ($user) use ($auth) {
|
||
|
Cookie::set('adminid', $user->id);
|
||
|
Cookie::set('admintoken', $auth->getToken());
|
||
|
});
|
||
|
Hook::add('admin_delete_successed', function ($user) use ($auth) {
|
||
|
Cookie::delete('adminid');
|
||
|
Cookie::delete('admintoken');
|
||
|
});
|
||
|
Hook::add('admin_logout_successed', function ($user) use ($auth) {
|
||
|
Cookie::delete('adminid');
|
||
|
Cookie::delete('admintoken');
|
||
|
});
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 管理员登录
|
||
|
*
|
||
|
* @ApiMethod (POST)
|
||
|
* @ApiParams (name="account", type="string", required=true, description="账号")
|
||
|
* @ApiParams (name="password", type="string", required=true, description="密码")
|
||
|
*/
|
||
|
public function login()
|
||
|
{
|
||
|
$account = $this->request->post('account');
|
||
|
$password = $this->request->post('password');
|
||
|
if (!$account || !$password) {
|
||
|
$this->error(__('Invalid parameters'));
|
||
|
}
|
||
|
$ret = $this->auth->login($account, $password);
|
||
|
if ($ret) {
|
||
|
$data = ['userinfo' => $this->auth->getUserinfo()];
|
||
|
$this->success(__('Logged in successful'), $data);
|
||
|
} else {
|
||
|
$this->error($this->auth->getError());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 退出登录
|
||
|
* @ApiMethod (POST)
|
||
|
*/
|
||
|
public function logout()
|
||
|
{
|
||
|
// //设置contenttype不为表单格式防止触发宝塔防火墙
|
||
|
// header('Content-Type:application/json; charset=utf-8');
|
||
|
if (!$this->request->isPost()) {
|
||
|
$this->error(__('Invalid parameters'));
|
||
|
}
|
||
|
// $password = $this->request->post('datatime');
|
||
|
|
||
|
$this->auth->logout();
|
||
|
$this->success(__('Logout successful'));
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|