五项家庭化改造:以个人为中心 · 家谱 · 三种角色 · 附件存储可配置 · 独立页样式

1. 独立页(登录/安装/错误)套上 HTML 骨架 —— 修「首页为什么没有样式」:
   新增 templates/layout-bare.php,View::render 对独立页也走完整 <head> + CSS。
2. 以个人为中心:Kinship::label() 现算称呼、Auth::homeTitle() 出「X的家书」;
   同一批人在不同登录者眼里自动变成公公婆婆 / 岳父岳母(不存一次性称谓)。
3. 附件存储可配置:Storage 门面(local / 阿里云 OSS),库里只存相对键,
   云端失败自动回退本地;设置页可选驱动并填 OSS 参数(照 MovieLog 的方案)。
4. 三种角色:admin / member / guest,访客只读、写操作一律 403;
   accounts.role 枚举扩容,老库靠 Schema::ensure() 自动升级。
5. 家谱:只存相邻两代的边(parent / spouse),其余称呼全部推导 ——
   兄弟姐妹不存边(共同父母 + birth_rank 排行推长子/次女、哥哥/妹妹),
   认不清的人用虚拟人物 is_placeholder 占位,关系天然双向。

顺带修掉本地 e2e 挖出的 8 个线上级 bug(性别写死、长幼方向反、关系方向静默写反、
birth_rank 不从表单取回、隐式可空参数致响应头失效、无文件字段被误判为上传失败、
layout 变量撞车覆盖 $me、世代 0 显示成「下 0 代」)。

验证:kinship 单测 37/0 · HTTP e2e 81/0 · 老库升级专测 14/0 ·
反向验证 11/11(把每个修复改回旧写法,对应断言都会变红)。
This commit is contained in:
2026-09-30 08:13:07 +08:00
parent 8eb19267bc
commit 1669ab48d0
30 changed files with 3034 additions and 161 deletions
+44 -5
View File
@@ -72,9 +72,10 @@ final class Person
{
$phone = $d['phone'] ?? '';
$sql = 'INSERT INTO persons
(family_id, kind, name, nickname, gender, birthday, avatar,
phone_hash, phone_encrypted, id_card_encrypted, email, address, note, sort_order)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
(family_id, kind, name, nickname, gender, birth_rank, birthday, avatar,
phone_hash, phone_encrypted, id_card_encrypted, email, address, note,
is_placeholder, sort_order)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
$st = Database::get()->prepare($sql);
$st->execute([
$familyId,
@@ -82,6 +83,7 @@ final class Person
$d['name'],
$d['nickname'] !== '' ? $d['nickname'] : null,
$d['gender'] !== '' ? $d['gender'] : null,
!empty($d['birth_rank']) ? (int)$d['birth_rank'] : null,
$d['birthday'] !== '' ? $d['birthday'] : null,
$d['avatar'] !== '' ? $d['avatar'] : null,
$phone !== '' ? Crypto::hash($phone) : null,
@@ -90,6 +92,7 @@ final class Person
$d['email'] !== '' ? $d['email'] : null,
$d['address'] !== '' ? $d['address'] : null,
$d['note'] !== '' ? $d['note'] : null,
!empty($d['is_placeholder']) ? 1 : 0,
(int)($d['sort_order'] ?? 1000),
]);
return (int)Database::get()->lastInsertId();
@@ -99,14 +102,15 @@ final class Person
{
$phone = $d['phone'] ?? '';
$sql = 'UPDATE persons SET
name = ?, nickname = ?, gender = ?, birthday = ?, avatar = ?,
name = ?, nickname = ?, gender = ?, birth_rank = ?, birthday = ?, avatar = ?,
phone_hash = ?, phone_encrypted = ?, id_card_encrypted = ?,
email = ?, address = ?, note = ?, sort_order = ?
email = ?, address = ?, note = ?, is_placeholder = ?, sort_order = ?
WHERE id = ?';
Database::get()->prepare($sql)->execute([
$d['name'],
$d['nickname'] !== '' ? $d['nickname'] : null,
$d['gender'] !== '' ? $d['gender'] : null,
!empty($d['birth_rank']) ? (int)$d['birth_rank'] : null,
$d['birthday'] !== '' ? $d['birthday'] : null,
$d['avatar'] !== '' ? $d['avatar'] : null,
$phone !== '' ? Crypto::hash($phone) : null,
@@ -115,6 +119,7 @@ final class Person
$d['email'] !== '' ? $d['email'] : null,
$d['address'] !== '' ? $d['address'] : null,
$d['note'] !== '' ? $d['note'] : null,
!empty($d['is_placeholder']) ? 1 : 0,
(int)($d['sort_order'] ?? 1000),
$id,
]);
@@ -132,6 +137,30 @@ final class Person
Database::get()->prepare('DELETE FROM persons WHERE id = ?')->execute([$id]);
}
/** 全部家庭成员,以 id 为键 —— 家谱推导要按 id 频繁取人,这样最省事 */
public static function map(int $familyId, string $kind = 'member'): array
{
$st = Database::get()->prepare('SELECT * FROM persons WHERE family_id = ? AND kind = ?');
$st->execute([$familyId, $kind]);
$out = [];
foreach ($st->fetchAll() as $r) {
$out[(int)$r['id']] = $r;
}
return $out;
}
/** 只给「选人」用的精简列表(下拉框、关系选择器) */
public static function options(int $familyId, int $exceptId = 0): array
{
$st = Database::get()->prepare(
'SELECT id, name, nickname, gender, is_placeholder FROM persons
WHERE family_id = ? AND kind = "member" AND id <> ?
ORDER BY sort_order ASC, id ASC'
);
$st->execute([$familyId, $exceptId]);
return $st->fetchAll();
}
/**
* 最近要过生日的人(首页用)。
* 直接算「今天是今年的第几天」与生日的差值,跨年自动处理。
@@ -184,6 +213,16 @@ final class Person
if ($idc !== '' && !preg_match('/^[0-9Xx]{15,18}$/', $idc)) {
$err['id_card'] = '身份证号应为 15 或 18 位';
}
$rank = $d['birth_rank'] ?? '';
if ($rank !== '' && $rank !== null && (!ctype_digit((string)$rank) || (int)$rank < 1 || (int)$rank > 99)) {
$err['birth_rank'] = '排行请填 1-99 的整数(老大填 1)';
}
return $err;
}
/** 角色与人物的中文名 */
public static function genderWord(string $g): string
{
return ['m' => '男', 'f' => '女'][$g] ?? '';
}
}