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(把每个修复改回旧写法,对应断言都会变红)。
229 lines
8.8 KiB
PHP
229 lines
8.8 KiB
PHP
<?php
|
|
/**
|
|
* 家庭名册(家庭成员 + 将来的外部联系人共用一张表)。
|
|
*
|
|
* 之所以不拆成两张表:两者字段几乎一样;而且需要互相转换 ——
|
|
* 一个「认识的人」随时可能变成家庭成员,一个家庭成员也可能只作为联系人存在。
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App;
|
|
|
|
final class Person
|
|
{
|
|
/** 名册列表(连带账号信息,用于显示「是否已开通登录」) */
|
|
public static function all(int $familyId, string $q = '', string $kind = 'member'): array
|
|
{
|
|
$sql = 'SELECT p.*, a.id AS account_id, a.role AS account_role, a.status AS account_status,
|
|
a.last_login_at
|
|
FROM persons p
|
|
LEFT JOIN accounts a ON a.person_id = p.id
|
|
WHERE p.family_id = ? AND p.kind = ?';
|
|
$args = [$familyId, $kind];
|
|
if ($q !== '') {
|
|
$sql .= ' AND (p.name LIKE ? OR p.nickname LIKE ? OR p.email LIKE ?)';
|
|
$like = '%' . $q . '%';
|
|
$args[] = $like;
|
|
$args[] = $like;
|
|
$args[] = $like;
|
|
}
|
|
$sql .= ' ORDER BY p.sort_order ASC, p.id ASC';
|
|
$st = Database::get()->prepare($sql);
|
|
$st->execute($args);
|
|
return $st->fetchAll();
|
|
}
|
|
|
|
public static function find(int $id): ?array
|
|
{
|
|
$st = Database::get()->prepare('SELECT * FROM persons WHERE id = ?');
|
|
$st->execute([$id]);
|
|
$row = $st->fetch();
|
|
return $row ?: null;
|
|
}
|
|
|
|
/** 带账号信息的详情(详情页用) */
|
|
public static function findWithAccount(int $id): ?array
|
|
{
|
|
$st = Database::get()->prepare(
|
|
'SELECT p.*, a.id AS account_id, a.role AS account_role,
|
|
a.status AS account_status, a.last_login_at
|
|
FROM persons p LEFT JOIN accounts a ON a.person_id = p.id
|
|
WHERE p.id = ?'
|
|
);
|
|
$st->execute([$id]);
|
|
$row = $st->fetch();
|
|
return $row ?: null;
|
|
}
|
|
|
|
public static function findByPhoneHash(string $hash): ?array
|
|
{
|
|
$st = Database::get()->prepare('SELECT * FROM persons WHERE phone_hash = ? LIMIT 1');
|
|
$st->execute([$hash]);
|
|
$row = $st->fetch();
|
|
return $row ?: null;
|
|
}
|
|
|
|
/**
|
|
* 新增。手机号是敏感字段,进来就加密 + 哈希。
|
|
* @return int 新 id
|
|
*/
|
|
public static function create(int $familyId, array $d): int
|
|
{
|
|
$phone = $d['phone'] ?? '';
|
|
$sql = 'INSERT INTO persons
|
|
(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,
|
|
$d['kind'] ?? 'member',
|
|
$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,
|
|
$phone !== '' ? Crypto::encrypt($phone) : null,
|
|
($d['id_card'] ?? '') !== '' ? Crypto::encrypt($d['id_card']) : null,
|
|
$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();
|
|
}
|
|
|
|
public static function update(int $id, array $d): void
|
|
{
|
|
$phone = $d['phone'] ?? '';
|
|
$sql = 'UPDATE persons SET
|
|
name = ?, nickname = ?, gender = ?, birth_rank = ?, birthday = ?, avatar = ?,
|
|
phone_hash = ?, phone_encrypted = ?, id_card_encrypted = ?,
|
|
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,
|
|
$phone !== '' ? Crypto::encrypt($phone) : null,
|
|
($d['id_card'] ?? '') !== '' ? Crypto::encrypt($d['id_card']) : null,
|
|
$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,
|
|
]);
|
|
}
|
|
|
|
/** 只改头像(上传后单独调用,避免整条记录被表单覆盖) */
|
|
public static function setAvatar(int $id, ?string $avatar): void
|
|
{
|
|
Database::get()->prepare('UPDATE persons SET avatar = ? WHERE id = ?')
|
|
->execute([$avatar, $id]);
|
|
}
|
|
|
|
public static function delete(int $id): void
|
|
{
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* 最近要过生日的人(首页用)。
|
|
* 直接算「今天是今年的第几天」与生日的差值,跨年自动处理。
|
|
*/
|
|
public static function upcomingBirthdays(int $familyId, int $withinDays = 60): array
|
|
{
|
|
$st = Database::get()->prepare(
|
|
"SELECT id, name, avatar, birthday FROM persons
|
|
WHERE family_id = ? AND kind = 'member' AND birthday IS NOT NULL
|
|
ORDER BY DAYOFYEAR(birthday)"
|
|
);
|
|
$st->execute([$familyId]);
|
|
$rows = $st->fetchAll();
|
|
|
|
$out = [];
|
|
foreach ($rows as $r) {
|
|
$days = days_until_birthday($r['birthday']);
|
|
if ($days !== null && $days <= $withinDays) {
|
|
$r['days_left'] = $days;
|
|
$out[] = $r;
|
|
}
|
|
}
|
|
usort($out, function ($a, $b) {
|
|
return $a['days_left'] <=> $b['days_left'];
|
|
});
|
|
return $out;
|
|
}
|
|
|
|
/** 校验表单,返回错误数组(空数组表示通过) */
|
|
public static function validate(array $d): array
|
|
{
|
|
$err = [];
|
|
if (trim((string)($d['name'] ?? '')) === '') {
|
|
$err['name'] = '姓名不能为空';
|
|
}
|
|
$phone = trim((string)($d['phone'] ?? ''));
|
|
if ($phone !== '' && !preg_match('/^\+?[0-9\-]{6,20}$/', $phone)) {
|
|
$err['phone'] = '手机号格式看起来不对';
|
|
}
|
|
$email = trim((string)($d['email'] ?? ''));
|
|
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$err['email'] = '邮箱格式看起来不对';
|
|
}
|
|
$b = trim((string)($d['birthday'] ?? ''));
|
|
if ($b !== '' && !preg_match('/^\d{4}-\d{2}-\d{2}$/', $b)) {
|
|
$err['birthday'] = '生日请按 年-月-日 填写';
|
|
}
|
|
// 身份证号只做很松的校验:家庭场景里要不要存它,本来就值得三思
|
|
$idc = trim((string)($d['id_card'] ?? ''));
|
|
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] ?? '';
|
|
}
|
|
}
|