14
251 天前
```
/**
* 用户登录
*/
public function login(Request $request)
{
// 用户已经登陆直接跳转到首页
$admin_info = Session::get('admin_info');
if(!empty($admin_info)){
$this->redirect('index/index');
}
if($request->isAjax())
{
$phone = $request->param('phone','','trim,strip_tags,htmlspecialchars');
$password = $request->param('password','','trim,strip_tags,htmlspecialchars');
$captcha = $request->param('captcha','','trim,strip_tags,htmlspecialchars');
if(!captcha_check($captcha)){
return responseJson('400', '验证码错误');
}
$admin_info = \app\manager\model\Admin::alias('a')
->join('role r', 'a.role = r.id')
->where('phone', $phone)
->field('a.*,r.role_name,auth_group')
->find();
if(!$admin_info){
return responseJson('400','账户信息不存在');
}
if($admin_info['is_delete'] || $admin_info['is_freeze']){
return responseJson('400','账号异常');
}
if(!password_verify($password,$admin_info['password'])){
return responseJson('400','账号密码不匹配');
}else{
$data = [
'login_ip' => get_real_ip(),
'login_time'=>time()
];
// 更新登录的 ip 地址和登录时间
$res = Db::name('admin')->where('phone',$phone)->data($data)->update();
if(!$res){
return responseJson('500','服务器错误');
}
unset($admin_info['password']);
$admin_info->auth_rule = \app\manager\model\Admin::getUserAuth($admin_info->auth_group);
Session::set('admin_info',$admin_info);
return responseJson('200','登陆成功');
}
}else{
return $this->fetch();
}
}
/**
* 退出登录
* @param
* @return
*/
public function logout()
{
Session::delete('admin_info');
return $this->redirect('account/login');
}
```
```
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 会话设置
// +----------------------------------------------------------------------
return [
'id' => '',
// SESSION_ID 的提交变量,解决 flash 上传跨域
'var_session_id' => '',
// SESSION 前缀
'prefix' => 'think',
// 驱动方式 支持 redis memcache memcached
'type' => '',
// 是否自动开启 SESSION
'auto_start' => true
];
```