2026-09-30 00:49:53 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
2026-09-30 09:44:55 +08:00
|
|
|
* 家庭名册(家庭成员 + 外部联系人共用一张表)。
|
2026-09-30 00:49:53 +08:00
|
|
|
*
|
|
|
|
|
* 之所以不拆成两张表:两者字段几乎一样;而且需要互相转换 ——
|
|
|
|
|
* 一个「认识的人」随时可能变成家庭成员,一个家庭成员也可能只作为联系人存在。
|
2026-09-30 09:44:55 +08:00
|
|
|
*
|
|
|
|
|
* 三处刻意的设计(都是被纠正过的):
|
|
|
|
|
* · **小名不在这里**。称呼是"谁在叫"的属性,存在 nicknames 表(见 Nickname 类)。
|
|
|
|
|
* · 生日不是一个字段,是三个(身份证 / 阳历 / 农历),见 Birthday 类。
|
|
|
|
|
* · 姓名与称呼的读取都要过 `Nickname::display()`,页面不要直接 echo name。
|
2026-09-30 00:49:53 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
|
|
final class Person
|
|
|
|
|
{
|
2026-09-30 09:44:55 +08:00
|
|
|
/**
|
|
|
|
|
* 名册列表(连带账号信息与「我叫他什么」)。
|
|
|
|
|
*
|
|
|
|
|
* `my_nickname` 是**当前登录者**给这个人起的称呼 —— 同一份名册,
|
|
|
|
|
* 爸爸看到的是"儿子",妈妈看到的是"小宝",靠的就是这个字段。
|
|
|
|
|
*/
|
|
|
|
|
public static function all(int $familyId, string $q = '', string $kind = 'member', int $viewerId = 0): array
|
2026-09-30 00:49:53 +08:00
|
|
|
{
|
|
|
|
|
$sql = 'SELECT p.*, a.id AS account_id, a.role AS account_role, a.status AS account_status,
|
2026-09-30 09:44:55 +08:00
|
|
|
a.last_login_at, n.nickname AS my_nickname
|
2026-09-30 00:49:53 +08:00
|
|
|
FROM persons p
|
|
|
|
|
LEFT JOIN accounts a ON a.person_id = p.id
|
2026-09-30 09:44:55 +08:00
|
|
|
LEFT JOIN nicknames n ON n.target_id = p.id AND n.viewer_id = ?';
|
|
|
|
|
$args = [$viewerId];
|
|
|
|
|
$sql .= ' WHERE p.family_id = ? AND p.kind = ?';
|
|
|
|
|
$args[] = $familyId;
|
|
|
|
|
$args[] = $kind;
|
2026-09-30 00:49:53 +08:00
|
|
|
if ($q !== '') {
|
2026-09-30 09:44:55 +08:00
|
|
|
$sql .= ' AND (p.name LIKE ? OR p.email LIKE ? OR n.nickname LIKE ?)';
|
2026-09-30 00:49:53 +08:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 09:44:55 +08:00
|
|
|
/** 带账号信息的详情(详情页用;$viewerId 用来取「我叫他什么」) */
|
|
|
|
|
public static function findWithAccount(int $id, int $viewerId = 0): ?array
|
2026-09-30 00:49:53 +08:00
|
|
|
{
|
|
|
|
|
$st = Database::get()->prepare(
|
|
|
|
|
'SELECT p.*, a.id AS account_id, a.role AS account_role,
|
2026-09-30 09:44:55 +08:00
|
|
|
a.status AS account_status, a.last_login_at, n.nickname AS my_nickname
|
|
|
|
|
FROM persons p
|
|
|
|
|
LEFT JOIN accounts a ON a.person_id = p.id
|
|
|
|
|
LEFT JOIN nicknames n ON n.target_id = p.id AND n.viewer_id = ?
|
2026-09-30 00:49:53 +08:00
|
|
|
WHERE p.id = ?'
|
|
|
|
|
);
|
2026-09-30 09:44:55 +08:00
|
|
|
$st->execute([$viewerId, $id]);
|
2026-09-30 00:49:53 +08:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-30 09:44:55 +08:00
|
|
|
* 新增。手机号是敏感字段,进来就加密 + 哈希;三个生日由 Birthday 规范化。
|
2026-09-30 00:49:53 +08:00
|
|
|
* @return int 新 id
|
|
|
|
|
*/
|
|
|
|
|
public static function create(int $familyId, array $d): int
|
|
|
|
|
{
|
2026-09-30 09:44:55 +08:00
|
|
|
$phone = (string)($d['phone'] ?? '');
|
|
|
|
|
$bd = Birthday::normalize($d);
|
2026-09-30 00:49:53 +08:00
|
|
|
$sql = 'INSERT INTO persons
|
2026-09-30 09:44:55 +08:00
|
|
|
(family_id, kind, name, gender, birth_rank, birthday,
|
|
|
|
|
birthday_id_card, birthday_lunar, birthday_lunar_leap, slogan, avatar,
|
2026-09-30 08:13:07 +08:00
|
|
|
phone_hash, phone_encrypted, id_card_encrypted, email, address, note,
|
|
|
|
|
is_placeholder, sort_order)
|
2026-09-30 09:44:55 +08:00
|
|
|
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
|
2026-09-30 00:49:53 +08:00
|
|
|
$st = Database::get()->prepare($sql);
|
|
|
|
|
$st->execute([
|
|
|
|
|
$familyId,
|
|
|
|
|
$d['kind'] ?? 'member',
|
|
|
|
|
$d['name'],
|
2026-09-30 09:44:55 +08:00
|
|
|
($d['gender'] ?? '') !== '' ? $d['gender'] : null,
|
2026-09-30 08:13:07 +08:00
|
|
|
!empty($d['birth_rank']) ? (int)$d['birth_rank'] : null,
|
2026-09-30 09:44:55 +08:00
|
|
|
$bd['solar'],
|
|
|
|
|
$bd['id_birthday'],
|
|
|
|
|
$bd['lunar'],
|
|
|
|
|
$bd['lunar_leap'],
|
|
|
|
|
($d['slogan'] ?? '') !== '' ? $d['slogan'] : null,
|
|
|
|
|
($d['avatar'] ?? '') !== '' ? $d['avatar'] : null,
|
2026-09-30 00:49:53 +08:00
|
|
|
$phone !== '' ? Crypto::hash($phone) : null,
|
|
|
|
|
$phone !== '' ? Crypto::encrypt($phone) : null,
|
|
|
|
|
($d['id_card'] ?? '') !== '' ? Crypto::encrypt($d['id_card']) : null,
|
2026-09-30 09:44:55 +08:00
|
|
|
($d['email'] ?? '') !== '' ? $d['email'] : null,
|
|
|
|
|
($d['address'] ?? '') !== '' ? $d['address'] : null,
|
|
|
|
|
($d['note'] ?? '') !== '' ? $d['note'] : null,
|
2026-09-30 08:13:07 +08:00
|
|
|
!empty($d['is_placeholder']) ? 1 : 0,
|
2026-09-30 00:49:53 +08:00
|
|
|
(int)($d['sort_order'] ?? 1000),
|
|
|
|
|
]);
|
|
|
|
|
return (int)Database::get()->lastInsertId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function update(int $id, array $d): void
|
|
|
|
|
{
|
2026-09-30 09:44:55 +08:00
|
|
|
$phone = (string)($d['phone'] ?? '');
|
|
|
|
|
$bd = Birthday::normalize($d);
|
2026-09-30 00:49:53 +08:00
|
|
|
$sql = 'UPDATE persons SET
|
2026-09-30 09:44:55 +08:00
|
|
|
name = ?, gender = ?, birth_rank = ?, birthday = ?,
|
|
|
|
|
birthday_id_card = ?, birthday_lunar = ?, birthday_lunar_leap = ?, slogan = ?, avatar = ?,
|
2026-09-30 00:49:53 +08:00
|
|
|
phone_hash = ?, phone_encrypted = ?, id_card_encrypted = ?,
|
2026-09-30 08:13:07 +08:00
|
|
|
email = ?, address = ?, note = ?, is_placeholder = ?, sort_order = ?
|
2026-09-30 00:49:53 +08:00
|
|
|
WHERE id = ?';
|
|
|
|
|
Database::get()->prepare($sql)->execute([
|
|
|
|
|
$d['name'],
|
2026-09-30 09:44:55 +08:00
|
|
|
($d['gender'] ?? '') !== '' ? $d['gender'] : null,
|
2026-09-30 08:13:07 +08:00
|
|
|
!empty($d['birth_rank']) ? (int)$d['birth_rank'] : null,
|
2026-09-30 09:44:55 +08:00
|
|
|
$bd['solar'],
|
|
|
|
|
$bd['id_birthday'],
|
|
|
|
|
$bd['lunar'],
|
|
|
|
|
$bd['lunar_leap'],
|
|
|
|
|
($d['slogan'] ?? '') !== '' ? $d['slogan'] : null,
|
|
|
|
|
($d['avatar'] ?? '') !== '' ? $d['avatar'] : null,
|
2026-09-30 00:49:53 +08:00
|
|
|
$phone !== '' ? Crypto::hash($phone) : null,
|
|
|
|
|
$phone !== '' ? Crypto::encrypt($phone) : null,
|
|
|
|
|
($d['id_card'] ?? '') !== '' ? Crypto::encrypt($d['id_card']) : null,
|
2026-09-30 09:44:55 +08:00
|
|
|
($d['email'] ?? '') !== '' ? $d['email'] : null,
|
|
|
|
|
($d['address'] ?? '') !== '' ? $d['address'] : null,
|
|
|
|
|
($d['note'] ?? '') !== '' ? $d['note'] : null,
|
2026-09-30 08:13:07 +08:00
|
|
|
!empty($d['is_placeholder']) ? 1 : 0,
|
2026-09-30 00:49:53 +08:00
|
|
|
(int)($d['sort_order'] ?? 1000),
|
|
|
|
|
$id,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 09:44:55 +08:00
|
|
|
/**
|
|
|
|
|
* 自助改自己的资料(个人信息页用)。
|
|
|
|
|
*
|
|
|
|
|
* 与上面的 update() 分开是有意的:那个是"管理员编名册",字段全、权限高;
|
|
|
|
|
* 这个只允许改**属于自己的**东西 —— 姓名、性别、三个生日、一句话、头像、联系方式。
|
|
|
|
|
* 排行、排序号、虚拟人物标记、以及身份证号(会牵动身份证生日)都只能由管理员改名册时动。
|
|
|
|
|
*/
|
|
|
|
|
public static function updateSelf(int $id, array $d): void
|
|
|
|
|
{
|
|
|
|
|
$bd = Birthday::normalize($d);
|
|
|
|
|
Database::get()->prepare(
|
|
|
|
|
'UPDATE persons SET
|
|
|
|
|
name = ?, gender = ?, birthday = ?,
|
|
|
|
|
birthday_id_card = ?, birthday_lunar = ?, birthday_lunar_leap = ?, slogan = ?, avatar = ?,
|
|
|
|
|
email = ?, address = ?, note = ?
|
|
|
|
|
WHERE id = ?'
|
|
|
|
|
)->execute([
|
|
|
|
|
$d['name'],
|
|
|
|
|
($d['gender'] ?? '') !== '' ? $d['gender'] : null,
|
|
|
|
|
$bd['solar'],
|
|
|
|
|
$bd['id_birthday'],
|
|
|
|
|
$bd['lunar'],
|
|
|
|
|
$bd['lunar_leap'],
|
|
|
|
|
($d['slogan'] ?? '') !== '' ? $d['slogan'] : null,
|
|
|
|
|
($d['avatar'] ?? '') !== '' ? $d['avatar'] : null,
|
|
|
|
|
($d['email'] ?? '') !== '' ? $d['email'] : null,
|
|
|
|
|
($d['address'] ?? '') !== '' ? $d['address'] : null,
|
|
|
|
|
($d['note'] ?? '') !== '' ? $d['note'] : null,
|
|
|
|
|
$id,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 00:49:53 +08:00
|
|
|
/** 只改头像(上传后单独调用,避免整条记录被表单覆盖) */
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 08:13:07 +08:00
|
|
|
/** 全部家庭成员,以 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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 09:44:55 +08:00
|
|
|
/** 只给「选人」用的精简列表(下拉框、关系选择器),带「我叫他什么」 */
|
|
|
|
|
public static function options(int $familyId, int $exceptId = 0, int $viewerId = 0): array
|
2026-09-30 08:13:07 +08:00
|
|
|
{
|
|
|
|
|
$st = Database::get()->prepare(
|
2026-09-30 09:44:55 +08:00
|
|
|
'SELECT p.id, p.name, p.gender, p.avatar, p.is_placeholder, n.nickname AS my_nickname
|
|
|
|
|
FROM persons p
|
|
|
|
|
LEFT JOIN nicknames n ON n.target_id = p.id AND n.viewer_id = ?
|
|
|
|
|
WHERE p.family_id = ? AND p.kind = "member" AND p.id <> ?
|
|
|
|
|
ORDER BY p.sort_order ASC, p.id ASC'
|
2026-09-30 08:13:07 +08:00
|
|
|
);
|
2026-09-30 09:44:55 +08:00
|
|
|
$st->execute([$viewerId, $familyId, $exceptId]);
|
2026-09-30 08:13:07 +08:00
|
|
|
return $st->fetchAll();
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 00:49:53 +08:00
|
|
|
/**
|
|
|
|
|
* 最近要过生日的人(首页用)。
|
2026-09-30 09:44:55 +08:00
|
|
|
*
|
|
|
|
|
* ⚠️ 这里返回的是「**一个生日事件**」而不是「一个人」:
|
|
|
|
|
* 一个人可能同时有身份证生日与农历生日两条(不重合时),
|
|
|
|
|
* 也可能它们正好落在同一天(那就是 1 条,kinds 里有两个)。
|
|
|
|
|
* 早先按"人"来算,于是农历生日完全不会被提醒 —— 而家里老人过的正是农历。
|
2026-09-30 00:49:53 +08:00
|
|
|
*/
|
|
|
|
|
public static function upcomingBirthdays(int $familyId, int $withinDays = 60): array
|
|
|
|
|
{
|
2026-09-30 09:44:55 +08:00
|
|
|
$today = date('Y-m-d');
|
2026-09-30 00:49:53 +08:00
|
|
|
$out = [];
|
2026-09-30 09:44:55 +08:00
|
|
|
foreach (self::map($familyId) as $p) {
|
|
|
|
|
foreach (Birthday::nextOccurrences($p, $today, $withinDays) as $occ) {
|
|
|
|
|
$out[] = [
|
|
|
|
|
'id' => (int)$p['id'],
|
|
|
|
|
'name' => (string)$p['name'],
|
|
|
|
|
'avatar' => $p['avatar'],
|
|
|
|
|
'date' => $occ['date'],
|
|
|
|
|
'days_left' => $occ['days'],
|
|
|
|
|
'kinds' => $occ['kinds'],
|
|
|
|
|
'kind_words' => array_map(fn($k) => Birthday::KINDS[$k] ?? $k, $occ['kinds']),
|
|
|
|
|
'lunar_label' => $occ['lunar_label'],
|
|
|
|
|
'age' => $occ['age'],
|
|
|
|
|
];
|
2026-09-30 00:49:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
usort($out, function ($a, $b) {
|
2026-09-30 09:44:55 +08:00
|
|
|
return $a['days_left'] <=> $b['days_left'] ?: strcmp($a['date'], $b['date']);
|
2026-09-30 00:49:53 +08:00
|
|
|
});
|
|
|
|
|
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'] = '邮箱格式看起来不对';
|
|
|
|
|
}
|
2026-09-30 09:44:55 +08:00
|
|
|
// 生日现在是「日期 + 时分秒」,但要宽松:只填日期也必须收下
|
|
|
|
|
foreach (['birthday_solar' => '阳历生日', 'birthday_lunar' => '农历生日'] as $key => $word) {
|
|
|
|
|
$v = trim((string)($d[$key] ?? ''));
|
|
|
|
|
if ($v === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!preg_match('/^\d{4}-\d{2}-\d{2}([ ]\d{2}:\d{2}(:\d{2})?)?$/', $v)) {
|
|
|
|
|
$err[$key] = $word . '格式不对';
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
[$yy, $mm, $dd] = array_map('intval', explode('-', substr($v, 0, 10)));
|
|
|
|
|
if (!checkdate($mm, $dd, $yy) || $yy < Lunar::MIN_YEAR || $yy > Lunar::MAX_YEAR) {
|
|
|
|
|
$err[$key] = $word . '看起来不是一个真实的日子';
|
|
|
|
|
}
|
2026-09-30 00:49:53 +08:00
|
|
|
}
|
|
|
|
|
// 身份证号只做很松的校验:家庭场景里要不要存它,本来就值得三思
|
|
|
|
|
$idc = trim((string)($d['id_card'] ?? ''));
|
2026-09-30 09:44:55 +08:00
|
|
|
if ($idc !== '' && !preg_match('/^\d{17}[\dXx]$|^\d{15}$/', $idc)) {
|
|
|
|
|
$err['id_card'] = '身份证号应为 18 位(或 15 位老证)';
|
2026-09-30 00:49:53 +08:00
|
|
|
}
|
2026-09-30 08:13:07 +08:00
|
|
|
$rank = $d['birth_rank'] ?? '';
|
|
|
|
|
if ($rank !== '' && $rank !== null && (!ctype_digit((string)$rank) || (int)$rank < 1 || (int)$rank > 99)) {
|
|
|
|
|
$err['birth_rank'] = '排行请填 1-99 的整数(老大填 1)';
|
|
|
|
|
}
|
2026-09-30 09:44:55 +08:00
|
|
|
$slogan = trim((string)($d['slogan'] ?? ''));
|
|
|
|
|
if (mb_strlen($slogan, 'UTF-8') > 200) {
|
|
|
|
|
$err['slogan'] = '一句话请控制在 200 字以内';
|
|
|
|
|
}
|
2026-09-30 00:49:53 +08:00
|
|
|
return $err;
|
|
|
|
|
}
|
2026-09-30 08:13:07 +08:00
|
|
|
|
|
|
|
|
/** 角色与人物的中文名 */
|
|
|
|
|
public static function genderWord(string $g): string
|
|
|
|
|
{
|
|
|
|
|
return ['m' => '男', 'f' => '女'][$g] ?? '';
|
|
|
|
|
}
|
2026-09-30 00:49:53 +08:00
|
|
|
}
|