Files
FamilyBook/templates/people/show.php
T
laoyang 1669ab48d0 五项家庭化改造:以个人为中心 · 家谱 · 三种角色 · 附件存储可配置 · 独立页样式
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(把每个修复改回旧写法,对应断言都会变红)。
2026-09-30 08:13:07 +08:00

365 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 成员详情 —— 同时是「以我为中心」的呈现页与亲属关系的录入入口。
*
* 页面上每个人名旁边都带着「我该怎么称呼他」,同一个页面
* 爸爸看到「我的儿子」,爱人看到「我的丈夫」,全靠 Kinship 现算。
*/
$age = age_from($person['birthday'] ?? '');
$admin = \App\Auth::isAdmin();
$canW = \App\Auth::canWrite();
$pid = (int)$person['id'];
$p = $person;
$isSelf = (int)$p['id'] === (int)\App\Auth::personId();
$role = (string)($p['account_role'] ?? '');
$nameOf = function (int $id) use ($people): string {
return (string)($people[$id]['name'] ?? '(已移除)');
};
// 当前这个人的父母/配偶/子女(用于在录入表单里显示已填的内容)
$theirParents = \App\Kinship::parents($graph, $pid);
$theirSpouses = \App\Kinship::spouseIds($graph, $pid);
$theirKids = \App\Kinship::childIds($graph, $pid);
$theirSibs = \App\Kinship::siblingIds($graph, $pid);
?>
<div class="page-head">
<h1><?= e($p['name']) ?></h1>
<span class="sub">
<?php if ($labelToMe !== '我'): ?>
<span class="rel-label"><?= e($labelToMe) ?></span>
<?php else: ?>
这是你自己
<?php endif; ?>
<?php if (!empty($p['nickname'])): ?> · 小名 <?= e($p['nickname']) ?><?php endif; ?>
<?php if (!empty($p['is_placeholder'])): ?><span class="badge">虚拟人物</span><?php endif; ?>
</span>
<div class="actions">
<?php if ($canW): ?>
<a class="btn btn-ghost btn-sm" href="<?= e(url('people/edit', ['id' => $pid])) ?>">编辑资料</a>
<?php endif; ?>
<?php if ($admin): ?>
<form method="post" action="<?= e(url('people/destroy')) ?>" style="display:inline"
onsubmit="return confirm('确定要从名册里移除 <?= e($p['name']) ?> 吗?相关的账号与亲属关系也会一起删掉。')">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= $pid ?>">
<button class="btn btn-danger btn-sm" type="submit">移除</button>
</form>
<?php endif; ?>
</div>
</div>
<div class="detail">
<aside class="detail-photo">
<img src="<?= e(avatar_url($p['avatar'])) ?>" alt="">
<div class="name"><?= e($p['name']) ?></div>
<div class="sub">
<?php if ($role === 'admin' && $p['account_status'] === 'active'): ?>
家庭管理员
<?php elseif ($role === 'guest'): ?>
访客(只读)
<?php elseif (!empty($p['account_id'])): ?>
已开通登录<?= $p['account_status'] === 'disabled' ? '(已停用)' : '' ?>
<?php else: ?>
还没有开通登录
<?php endif; ?>
</div>
</aside>
<div>
<div class="card card-pad">
<dl class="kv">
<div class="kv-row"><dt>姓名</dt><dd><?= e($p['name']) ?></dd></div>
<?php if (!empty($p['nickname'])): ?>
<div class="kv-row"><dt>小名</dt><dd><?= e($p['nickname']) ?></dd></div>
<?php endif; ?>
<div class="kv-row">
<dt>性别</dt>
<dd><?= e(\App\Person::genderWord((string)($p['gender'] ?? '')) ?: '未填写') ?></dd>
</div>
<?php if (!empty($p['birth_rank'])): ?>
<div class="kv-row"><dt>家中排行</dt><dd>第 <?= (int)$p['birth_rank'] ?> 个孩子</dd></div>
<?php endif; ?>
<div class="kv-row">
<dt>生日</dt>
<dd>
<?php if (!empty($p['birthday'])): ?>
<?= e($p['birthday']) ?>
<?php if ($age !== null): ?> · <?= (int)$age ?> 岁<?php endif; ?>
<?php $d = days_until_birthday($p['birthday']); ?>
<?php if ($d === 0): ?> · <span style="color:var(--brand)">今天生日</span>
<?php elseif ($d !== null): ?> · 还有 <?= (int)$d ?> 天<?php endif; ?>
<?php else: ?>
<span class="muted">未填写</span>
<?php endif; ?>
</dd>
</div>
<div class="kv-row">
<dt>手机号</dt>
<dd class="mono"><?= e(\App\Crypto::maskPhone((string)($p['phone_encrypted'] ?? ''))) ?></dd>
</div>
<div class="kv-row">
<dt>身份证号</dt>
<dd class="mono"><?= e(\App\Crypto::maskIdCard((string)($p['id_card_encrypted'] ?? ''))) ?></dd>
</div>
<?php if (!empty($p['email'])): ?>
<div class="kv-row"><dt>邮箱</dt><dd><?= e($p['email']) ?></dd></div>
<?php endif; ?>
<?php if (!empty($p['address'])): ?>
<div class="kv-row"><dt>住址</dt><dd><?= e($p['address']) ?></dd></div>
<?php endif; ?>
<?php if (!empty($p['note'])): ?>
<div class="kv-row"><dt>备注</dt><dd><?= nl2br(e($p['note'])) ?></dd></div>
<?php endif; ?>
<div class="kv-row">
<dt>最近登录</dt>
<dd><?= !empty($p['last_login_at']) ? e($p['last_login_at']) : '从未登录' ?></dd>
</div>
</dl>
</div>
<!-- ============ 亲属关系 ============ -->
<div class="card card-pad" style="margin-top:16px">
<h2 class="section-title">
<?= e($p['name']) ?> 的亲属
<span class="hint">关系是双向的,填一次两边都生效</span>
</h2>
<?php
// 已有关系一览(从「当前这个人」的视角)
$blocks = [
['父母', $theirParents['father'] ? [[$theirParents['father'], '父亲']] : [],
$theirParents['mother'] ? [[$theirParents['mother'], '母亲']] : []],
];
?>
<div class="rel-summary">
<div class="rel-sum-item">
<span class="rel-sum-k">父亲</span>
<span class="rel-sum-v">
<?php if ($theirParents['father']): ?>
<a href="<?= e(url('people/show', ['id' => $theirParents['father']])) ?>"><?= e($nameOf($theirParents['father'])) ?></a>
<?php else: ?><span class="muted">未填写</span><?php endif; ?>
</span>
</div>
<div class="rel-sum-item">
<span class="rel-sum-k">母亲</span>
<span class="rel-sum-v">
<?php if ($theirParents['mother']): ?>
<a href="<?= e(url('people/show', ['id' => $theirParents['mother']])) ?>"><?= e($nameOf($theirParents['mother'])) ?></a>
<?php else: ?><span class="muted">未填写</span><?php endif; ?>
</span>
</div>
<div class="rel-sum-item">
<span class="rel-sum-k">配偶</span>
<span class="rel-sum-v">
<?php if ($theirSpouses): ?>
<?php foreach ($theirSpouses as $s): ?>
<a href="<?= e(url('people/show', ['id' => $s])) ?>"><?= e($nameOf($s)) ?></a>
<?php endforeach; ?>
<?php else: ?><span class="muted">未填写</span><?php endif; ?>
</span>
</div>
<div class="rel-sum-item">
<span class="rel-sum-k">子女</span>
<span class="rel-sum-v">
<?php if ($theirKids): ?>
<?php foreach ($theirKids as $c): ?>
<a href="<?= e(url('people/show', ['id' => $c])) ?>"><?= e($nameOf($c)) ?></a>
<?php $rk = $people[$c]['birth_rank'] ?? null; ?>
<?php if ($rk): ?><span class="muted small">(第<?= (int)$rk ?>)</span><?php endif; ?>
<?php endforeach; ?>
<?php else: ?><span class="muted">未填写</span><?php endif; ?>
</span>
</div>
<div class="rel-sum-item">
<span class="rel-sum-k">兄弟姐妹</span>
<span class="rel-sum-v">
<?php if ($theirSibs): ?>
<?php foreach ($theirSibs as $s): ?>
<a href="<?= e(url('people/show', ['id' => $s])) ?>"><?= e($nameOf($s)) ?></a>
<?php endforeach; ?>
<span class="muted small">(由共同父母自动认出)</span>
<?php else: ?><span class="muted">暂无</span><?php endif; ?>
</span>
</div>
</div>
<?php if ($canW): ?>
<div class="rel-add">
<h3 class="rel-add-title">添加/修改关系</h3>
<form method="post" action="<?= e(url('kin/relate')) ?>" class="rel-form">
<?= csrf_field() ?>
<input type="hidden" name="from_id" value="<?= $pid ?>">
<div class="field">
<label>关系</label>
<select class="select" name="kind" id="relKind">
<option value="parent">是我的父母</option>
<option value="child">是我的子女</option>
<option value="spouse">是我的配偶</option>
</select>
</div>
<div class="field" id="relRoleWrap">
<label id="relRoleLabel">是哪一位</label>
<select class="select" name="role">
<option value="father">父亲</option>
<option value="mother">母亲</option>
</select>
</div>
<div class="field">
<label>对方</label>
<select class="select" name="to_id" required>
<option value="">— 选择一个人 —</option>
<?php foreach ($options as $o): ?>
<option value="<?= (int)$o['id'] ?>">
<?= e($o['name']) ?><?= !empty($o['nickname']) ? '(' . e($o['nickname']) . ')' : '' ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="field" style="justify-content:flex-end">
<button class="btn btn-sm" type="submit">记下这条关系</button>
</div>
</form>
<p class="small muted" style="margin:12px 0 0">
找不到人?先到「添加成员」把人建出来 —— 不确定真实姓名时,
起个「我的父亲」这样的名字也可以,之后再改。
</p>
</div>
<?php if ($theirRels): ?>
<details class="rel-list">
<summary>已有关系明细(<?= count($theirRels) ?> 条)</summary>
<ul>
<?php foreach ($theirRels as $r): ?>
<li>
<?php if ($r['kind'] === 'parent'): ?>
<?= e($nameOf((int)$r['from_id'])) ?>
(<?= $r['from_role'] === 'mother' ? '母亲' : '父亲' ?>)
→
<?= e($nameOf((int)$r['to_id'])) ?>
<?php else: ?>
<?= e($nameOf((int)$r['from_id'])) ?> 与 <?= e($nameOf((int)$r['to_id'])) ?> 是配偶
<?php endif; ?>
<form method="post" action="<?= e(url('kin/unrelate')) ?>" style="display:inline">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int)$r['id'] ?>">
<input type="hidden" name="person_id" value="<?= $pid ?>">
<button class="btn btn-danger btn-sm" type="submit">解除</button>
</form>
</li>
<?php endforeach; ?>
</ul>
</details>
<?php endif; ?>
<?php else: ?>
<p class="small muted" style="margin:12px 0 0">你的身份是访客,只能查看亲属关系,不能修改。</p>
<?php endif; ?>
</div>
<!-- ============ 以这个人看出去的家谱 ============ -->
<?php if (!empty($theirTree['nodes']) && count($theirTree['nodes']) > 1): ?>
<div class="card card-pad" style="margin-top:16px">
<h2 class="section-title">从 <?= e($p['name']) ?> 的角度看 <span class="hint">上下各两代</span></h2>
<div class="mini-tree">
<?php foreach ($theirTree['generations'] as $level => $ids): ?>
<div class="mini-gen">
<?php foreach ($ids as $id): $n = $theirTree['nodes'][$id]; ?>
<a class="mini-node <?= (int)$id === $pid ? 'is-me' : '' ?>"
href="<?= e(url('people/show', ['id' => (int)$id])) ?>">
<img src="<?= e(avatar_url($n['avatar'])) ?>" alt="">
<span class="mn-name"><?= e($n['name']) ?></span>
<span class="mn-label"><?= e($n['label']) ?></span>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php if ($admin): ?>
<div class="card card-pad" style="margin-top:16px">
<h2 class="section-title">登录账号</h2>
<?php if (empty($p['account_id'])): ?>
<p class="small muted" style="margin:0 0 14px">
开通后,<?= e($p['name']) ?> 就能用手机号和密码登录家书了。
</p>
<form method="post" action="<?= e(url('accounts/create')) ?>" class="form-grid">
<?= csrf_field() ?>
<input type="hidden" name="person_id" value="<?= $pid ?>">
<div class="field">
<label>登录密码</label>
<input class="input" name="password" type="password" placeholder="至少 8 位,含字母和数字" required>
</div>
<div class="field">
<label>角色</label>
<select class="select" name="role">
<option value="member">家庭成员(可创建与编辑)</option>
<option value="admin">管理员(可改系统设置)</option>
<option value="guest">访客(只能查看)</option>
</select>
</div>
<div class="field full">
<button class="btn btn-sm" type="submit">开通账号</button>
</div>
</form>
<?php else: ?>
<p class="small muted" style="margin:0 0 14px">
账号已开通,登录名是 <?= e($p['name']) ?> 的手机号。
当前角色:<b><?= e(\App\Account::roleWord($role)) ?></b>
<?= $role === 'guest' ? '—— 只能查看,不能新增或修改任何内容。' : '' ?>
</p>
<div class="btn-row" style="margin-top:0;flex-wrap:wrap">
<?php if (!$isSelf): ?>
<form method="post" action="<?= e(url('accounts/role')) ?>" style="display:inline">
<?= csrf_field() ?>
<input type="hidden" name="person_id" value="<?= $pid ?>">
<select class="select" name="role" style="width:auto;display:inline-block">
<option value="member" <?= $role === 'member' ? 'selected' : '' ?>>家庭成员</option>
<option value="admin" <?= $role === 'admin' ? 'selected' : '' ?>>管理员</option>
<option value="guest" <?= $role === 'guest' ? 'selected' : '' ?>>访客</option>
</select>
<button class="btn btn-ghost btn-sm" type="submit">改角色</button>
</form>
<form method="post" action="<?= e(url('accounts/status')) ?>" style="display:inline">
<?= csrf_field() ?>
<input type="hidden" name="person_id" value="<?= $pid ?>">
<input type="hidden" name="status" value="<?= $p['account_status'] === 'active' ? 'disabled' : 'active' ?>">
<button class="btn btn-ghost btn-sm" type="submit">
<?= $p['account_status'] === 'active' ? '停用账号' : '启用账号' ?>
</button>
</form>
<?php else: ?>
<span class="small muted">这是你自己 —— 要改自己的权限,请让另一位管理员来操作。</span>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
<script>
// 关系表单的「是哪一位」下拉有**两种含义**:
// kind=parent → 对方是父亲/母亲(填的是对方)
// kind=child → 我是孩子的父亲/母亲(填的是自己)
// 所以标签必须跟着换,否则用户会以为在填对方的身份,把关系记反。
(function () {
var kind = document.getElementById('relKind');
var wrap = document.getElementById('relRoleWrap');
var lab = document.getElementById('relRoleLabel');
if (!kind || !wrap || !lab) return;
function sync() {
if (kind.value === 'spouse') {
wrap.style.display = 'none';
return;
}
wrap.style.display = '';
lab.textContent = kind.value === 'child' ? '我是孩子的' : '对方是我的';
}
kind.addEventListener('change', sync);
sync();
})();
</script>