public function index() {
if (request()->isPost()) {
$phone = input('post.phone');
$passwd = input('post.passwd');
$teacher = db('teacher')->where(['passwd' => md5($passwd), 'phone' => $phone])->find();
if (!$teacher) {
return json(['error' => 1, 'msg' => '用户名或密码错误!']);
}
$cache = \think\Cache::init();
$redis = $cache->handler();
if (!$redis) {
return json(['error' => 1111, 'msg' => '系统错误!']);
}
$nid = session_id();
session_commit();
// echo 'new->',$nid.PHP_EOL;
// 登录之前,退出所有其他终端
while ($session_id_to_destroy = $redis->rPop(config('cache.prefix').'tea_sessions_'.$teacher['id'])) {
// hijack then destroy session specified.
session_id($session_id_to_destroy);
// echo 'del->',$session_id_to_destroy.PHP_EOL;
session_start();
session_destroy();
session_commit();
}
session_id($nid);
session_start();
// echo 'new->',session_id().PHP_EOL;die;
session('web.teacher', $teacher);
// 将登录成功的最新的会话id存到redis,以支持单点登录
$redis->rPush(config('cache.prefix').'tea_sessions_'.$teacher['id'], session_id());
// dump(session('web.teacher'));die;
return json(['error' => 0, 'msg' => '登录成功!']);
}
return $this->fetch();
}