'管理员', 'member' => '家庭成员', 'guest' => '访客'][self::role()] ?? '成员'; } /** * 首页标题 —— 「以我为中心」最直接的一处落点。 * 登录后看到的不是「家书」,而是「<我的名字>的家书」。 */ public static function homeTitle(): string { $name = self::name(); return $name === '' ? '家书' : $name . '的家书'; } /** 未登录跳登录页 */ public static function requireLogin(): void { if (!self::check()) { redirect('auth/login'); } } /** 需要「能写」的权限(访客会被挡下) */ public static function requireWrite(): void { self::requireLogin(); if (!self::canWrite()) { http_response_code(403); \App\View::render('error', [ 'title' => '访客只读', 'message' => '你的身份是访客,只能查看,不能新增或修改内容。', ], 'home'); exit; } } public static function requireAdmin(): void { self::requireLogin(); if (!self::isAdmin()) { http_response_code(403); \App\View::render('error', [ 'title' => '没有权限', 'message' => '这一页只有家庭管理员可以操作。', ], 'home'); exit; } } /** * 手机号 + 密码登录。 * 返回 true 表示成功并已写入会话。 */ public static function login(string $phone, string $password): bool { // 简单节流:5 次失败后 60 秒内不再尝试。 // 防爆破的关键不在判定多严,而在于「尝试次数有成本」。 $fail = $_SESSION['login_fail'] ?? ['n' => 0, 't' => 0]; if ($fail['n'] >= 5 && (time() - $fail['t']) < 60) { usleep(500000); return false; } $hash = Crypto::hash($phone); $stmt = Database::get()->prepare( 'SELECT a.id AS account_id, a.password_hash, a.role, a.status, p.id AS person_id, p.family_id, p.name, p.avatar FROM accounts a JOIN persons p ON p.id = a.person_id WHERE p.phone_hash = ? LIMIT 1' ); $stmt->execute([$hash]); $row = $stmt->fetch(); if (!$row || $row['status'] !== 'active' || !password_verify($password, (string)$row['password_hash'])) { $_SESSION['login_fail'] = ['n' => $fail['n'] + 1, 't' => time()]; usleep(300000); return false; } unset($_SESSION['login_fail']); session_regenerate_id(true); $_SESSION['account_id'] = (int)$row['account_id']; $_SESSION['person_id'] = (int)$row['person_id']; $_SESSION['family_id'] = (int)$row['family_id']; $_SESSION['name'] = $row['name']; $_SESSION['role'] = $row['role']; $_SESSION['avatar'] = $row['avatar']; Database::get()->prepare('UPDATE accounts SET last_login_at = NOW() WHERE id = ?') ->execute([$row['account_id']]); // 进门就把「这个人的默认日历本」补上(幂等)。 // // ⚠️ 为什么必须在这里兜一道,而不是只靠 Schema::ensure(): // 全新安装时 install.php 是**先** Schema::ensure()、**后**建人 —— // ensure() 里那段 seedDefaultsForFamily() 遍历的是"已开通账号的人", // 那时一个人都还没有,于是**一本日历都不建**;紧接着 schema_version // 被写成最新版,升级路径以后也不会再跑 → 这个人永远没有观影/旅行两本。 // 症状很隐蔽:日程页能用,只是"我的日历本"空着,观影也不上日历。 // 放在登录处最稳:不管人是装的、别人加的、还是老库升级来的,进来就有。 try { \App\Calendar::seedDefaults((int)$row['family_id'], (int)$row['person_id']); } catch (\Throwable $ex) { // 日历本建不出来不该把人挡在门外;下次登录会再试一次 error_log('[familybook] 补建默认日历本失败(已跳过):' . $ex->getMessage() . ' @ ' . $ex->getFile() . ':' . $ex->getLine()); } return true; } public static function logout(): void { $_SESSION = []; if (ini_get('session.use_cookies')) { $p = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $p['path'], $p['domain'], $p['secure'], $p['httponly']); } session_destroy(); } /** 头像改动后刷新会话里的缓存 */ public static function refreshAvatar(string $avatar): void { $_SESSION['avatar'] = $avatar; } }