get($this->prefix . $token); if ($adminInfo) { if ((int) ($adminInfo['terminal'] ?? 0) === AdminTerminalEnum::BCR) { // 长会话每次请求查 DB;即使事务撤销后旧缓存尚未删除,也必须拒绝。 $session = AdminSession::where('token', $token)->where('expire_time', '>', time())->find(); $admin = $session ? Admin::where('id', $session->admin_id)->where('disable', 0) ->field('id,root,work_wechat_userid')->find() : null; if (!$session || (int) $session->terminal !== AdminTerminalEnum::BCR || (int) $session->admin_id !== (int) $adminInfo['admin_id'] || !$admin) { $this->deleteAdminInfo($token); return false; } // 并发续期后以数据库的新到期时间为准。 $adminInfo['expire_time'] = (int) $session->expire_time; // 解绑企微/权限变化不能因长缓存而延迟生效。 $adminInfo['root'] = (int) $admin->root; $adminInfo['role_id'] = $admin->role_id; $adminInfo['work_wechat_userid'] = (string) ($admin->work_wechat_userid ?? ''); } return $adminInfo; } //从数据获取信息被设置缓存(可能后台清除缓存) $adminInfo = $this->setAdminInfo($token); if ($adminInfo) { return $adminInfo; } return false; } /** * @notes 通过有效token设置管理信息缓存 * @param $token * @return array|false|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author 令狐冲 * @date 2021/7/5 12:12 */ public function setAdminInfo($token) { $adminSession = AdminSession::where([['token', '=', $token], ['expire_time', '>', time()]]) ->find(); if (empty($adminSession)) { return []; } $admin = Admin::where('id', '=', $adminSession->admin_id) ->append(['role_id']) ->find(); if (!$admin || ((int) $adminSession->terminal === AdminTerminalEnum::BCR && (int) $admin->disable !== 0)) { return []; } $roleName = ''; $roleLists = SystemRole::column('name', 'id'); if ($admin['root'] == 1) { $roleName = '系统管理员'; } else { foreach ($admin['role_id'] as $roleId) { $roleName .= $roleLists[$roleId] ?? ''; $roleName .= '/'; } $roleName = trim($roleName, '/'); } $adminInfo = [ 'admin_id' => $admin->id, 'root' => $admin->root, 'name' => $admin->name, 'account' => $admin->account, 'role_name' => $roleName, 'role_id' => $admin->role_id, 'token' => $token, 'terminal' => $adminSession->terminal, 'expire_time' => $adminSession->expire_time, 'login_ip' => request()->ip(), 'work_wechat_userid' => $admin->work_wechat_userid ?? '', ]; $this->set($this->prefix . $token, $adminInfo, new \DateTime(Date('Y-m-d H:i:s', $adminSession->expire_time))); return $adminInfo; } /** * @notes 删除缓存 * @param $token * @return bool * @author 令狐冲 * @date 2021/7/3 16:57 */ public function deleteAdminInfo($token) { return $this->delete($this->prefix . $token); } }