2026-09-30 00:49:53 +08:00
|
|
|
<?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
|
2026-09-30 08:13:07 +08:00
|
|
|
(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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
|
2026-09-30 00:49:53 +08:00
|
|
|
$st = Database::get()->prepare($sql);
|
|
|
|
|
$st->execute([
|
|
|
|
|
$familyId,
|
|
|
|
|
$d['kind'] ?? 'member',
|
|
|
|
|
$d['name'],
|
|
|
|
|
$d['nickname'] !== '' ? $d['nickname'] : null,
|
|
|
|
|
$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 00:49:53 +08:00
|
|
|
$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,
|
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
|
|
|
|
|
{
|
|
|
|
|
$phone = $d['phone'] ?? '';
|
|
|
|
|
$sql = 'UPDATE persons SET
|
2026-09-30 08:13:07 +08:00
|
|
|
name = ?, nickname = ?, gender = ?, birth_rank = ?, birthday = ?, 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'],
|
|
|
|
|
$d['nickname'] !== '' ? $d['nickname'] : null,
|
|
|
|
|
$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 00:49:53 +08:00
|
|
|
$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,
|
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,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 只改头像(上传后单独调用,避免整条记录被表单覆盖) */
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 只给「选人」用的精简列表(下拉框、关系选择器) */
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 00:49:53 +08:00
|
|
|
/**
|
|
|
|
|
* 最近要过生日的人(首页用)。
|
|
|
|
|
* 直接算「今天是今年的第几天」与生日的差值,跨年自动处理。
|
|
|
|
|
*/
|
|
|
|
|
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 位';
|
|
|
|
|
}
|
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 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
|
|
|
}
|