v24 第 6 轮:名册导入 / 两种备注 / 多段工作经历 / 家谱卡片与首字母检索修正

用户 2026-10-03 第二批(8 条)。逐条落点:

1. **名册导入**(新):支持 **vCard 3.0/4.0(.vcf)** 与 **CSV/TSV**(中英文表头都认)。
   新增 `app/ContactImport.php`(纯解析)+ `app/ContactImporter.php`(判重+落库)。
   判重按 **手机号(哈希,精确)→ 姓名(大小写无关)**;命中已有的人**只补空字段,
   绝不覆盖**(批量覆盖没法回退)。
   解析上要处理的三件事都处理了:**折行接回**(长名字/地址会被按字节折断)、
   转义还原(`\,` `\;` `\n`)、多值收全(TEL/EMAIL/ADR 可能出现多次)。
   UTF-16/GBK 的 vcf 也认(手机导出的常见编码)。
2. 成员详情页:右上角「移除」→ **「返回」**(回名册);「删除」移到页面**最下方**
   (在「开通登录账号」下面),并改成 `<details>` 两步确认 + 写清会连带删掉什么。
3. 编辑资料里的「这个人的一句话」**去掉** —— 那句话只能**本人**改。
   ⚠️ 落法是「这一栏只在改自己时出现」:用户要的是"别人不能替他写",
   而不是"这句没人能填";服务端也只在 `$isSelf` 时读表单,否则管理员一编辑就把它清空。
4. **工作单位 / 职务 / 职称可以有多段**(新表 `person_jobs`):表单是一组"经历行"
   (单位/职务/职称/起止年月/备注,可 +/×),详情页按"现在 + 历史"列出。
   `persons.org/title` 保留,语义变成"当前那段的快照"(列表页等只要"现在在哪儿"),
   升级时把老的 org/title 回填成一段经历。
5/备注分两种:「自我介绍」(`persons.note`,他本人写的、别人看得到)与
   **「我对他的私人备注」**(新表 `person_notes`,每人每对象一条,**只有写的人自己看得到**)。
   详情页有独立的一块;表单里只在"编辑别人"时出现。
6. 成员表单:**头像放到最前**、去掉「选择文件」按钮(点那张默认头像即选文件);
   表单标签统一 15px/600(原来 13–14px,和 12px 的灰字提示分不出层级)。
7. 名册列表:⋯ 前面加**「编辑」**按钮(改资料是高频动作,藏进 ⋯ 要点两下)。
8. 家谱:
   · **首字母检索 bug**:输入 `yy` 会搜出「岳父/洪勇」。根因是前端把
     **姓名与称呼的首字母拼成一串**再判子序列 —— 洪勇的称呼是"岳父" → `hy`+`yf`=`hyyf`
     → `yy` 跨字段命中。改成**逐字段独立判**(服务端本来就是逐字段判的,两边现在同口径)。
   · **卡片仍比头像宽**:头像是 **1:1(正方形)** 槽位,卡片里却写成 96×72 的长方形 +
     `object-fit: contain` → 照片被缩到 72 宽、左右各留 12px 白 → 看着"框比头像宽"。
     改成**正方形头像区 + `object-fit: cover`**(90×128 卡片,88×88 头像区)。
   · 右上角加**「我的家谱」**按钮(只在看别人的家谱时出现 —— 看自己的时候点它没意义)。

**测试新增**(都能变红,且做过反向验证):
- `_fb_import_test.py` 21 条:新人新建 / 手机号命中不重复建人 / **已有字段一个都不被覆盖** /
  缺的字段被补上 / 反复导入幂等 / 只给姓名也能命中 / 认不出的文件被明确拒绝,
  外加 ⑱ **"有单位但没有经历行"的人**(最容易漏的覆盖路径)。
- `_fb_roster_filter_test.js`(jsdom,用真实渲染出的 HTML)8 条:直击
  「输 yy 不该命中 岳父/洪勇」;反向验证时把实现改回拼接写法 → ① 立刻红。
- 改动过程中被测试抓出**两个真 bug**并已修:
  ① 导入时给"已有单位的人"也插经历 → `PersonJob::syncCurrent()` 回写把这人的单位**覆盖**了;
  ② `syncCurrent()` 在经历表为空时会把 `persons.org/title` **清空** ——
     而它被"编辑资料"这类常规路径调用一次就触发,属于**随手一保存就丢数据**。
     现在空表直接 return("经历表不是唯一的一份")。

**战绩**:e2e **775/0** · 导入 21/0 · 第 5 轮专项 91/0 · 家谱 34/0 · 几何 428/0 ·
移动端 688/0 · 交互 97/0 · 升级 126/0。
线上:schema **v15**、指纹一致、`person_jobs`/`person_notes` 已建、
6 条老的"工作单位"全部回填成经历、13 个文件 md5 与本地一致、错误日志无新增。
This commit is contained in:
2026-10-03 19:27:14 +08:00
parent 218cdf74fc
commit b5725b449a
14 changed files with 1639 additions and 81 deletions
+162 -2
View File
@@ -16,6 +16,8 @@ use App\Auth;
use App\Birthday;
use App\Calendar;
use App\Cinema;
use App\ContactImport;
use App\ContactImporter;
use App\Crypto;
use App\Database;
use App\Douban;
@@ -34,6 +36,8 @@ use App\MovieStats;
use App\Nickname;
use App\Person;
use App\PersonGroup;
use App\PersonJob;
use App\PersonNote;
use App\PersonQuery;
use App\PersonRoster;
use App\Pinyin;
@@ -144,6 +148,41 @@ function read_birthday_input(array $existing = []): array
* @return string[][][] kind => 行列表(label/value,value 保留原样,空行不丢 ——
* 交给 Person::replaceContacts 丢弃;校验失败重画表单时还要用它回填)
*/
/**
* 从表单读「工作经历」那组行(用户 2026-10-03 第 4 条)。
*
* 表单长这样(与「补充联系方式」同一套平行数组做法):
* job_org[] / job_title[] / job_rank[] / job_start[] / job_end[] / job_note[]
* 浏览器会按同一下标自动补齐,zip 起来最直观;空行由 PersonJob::replace 丢掉。
*
* start/end 用 `<input type="month">`,值就是 'YYYY-MM' —— 正好是库里存的格式,
* 不需要再做一次"日期 → 年月"的换算(少一处能写错的地方)。
*
* @return array<int,array{org:string,title:string,rank_name:string,start_ym:string,end_ym:string,note:string}>
*/
function read_jobs_input(): array
{
$org = (array)($_POST['job_org'] ?? []);
$title = (array)($_POST['job_title'] ?? []);
$rank = (array)($_POST['job_rank'] ?? []);
$start = (array)($_POST['job_start'] ?? []);
$end = (array)($_POST['job_end'] ?? []);
$note = (array)($_POST['job_note'] ?? []);
$n = max(count($org), count($title), count($rank), count($start), count($end), count($note));
$out = [];
for ($i = 0; $i < $n; $i++) {
$out[] = [
'org' => (string)($org[$i] ?? ''),
'title' => (string)($title[$i] ?? ''),
'rank_name' => (string)($rank[$i] ?? ''),
'start_ym' => (string)($start[$i] ?? ''),
'end_ym' => (string)($end[$i] ?? ''),
'note' => (string)($note[$i] ?? ''),
];
}
return $out;
}
function read_contacts_input(): array
{
$out = [];
@@ -803,6 +842,64 @@ switch ($r) {
redirect('people');
break;
/**
* 名册导入(v24 用户 2026-10-03 第 1 条)。
*
* 用户原话:「需要增加一个导入功能,可以导入常见的联系人格式,例如 vcf 等。
* 导入时,如果名字或手机号在已有的名册中存在,则导入其他信息,
* 如果这些信息在系统中已经存在,则只导入其他信息。」
*
* 流程:上传文件 → ContactImport::parse()(纯解析)→ ContactImporter::run()(判重+落库)。
* 判重与"只补空不覆盖"的规则都在 ContactImporter 里,这里只负责收文件、报告结果。
*/
case 'people/import':
Auth::requireWrite();
$familyId = (int)Auth::familyId();
$me = (int)Auth::personId();
if ($method !== 'POST') {
View::render('people/import', [
'title' => '导入联系人',
'groups' => PersonGroup::orderFor($familyId, $me),
'report' => null,
], 'people');
break;
}
csrf_check();
$f = $_FILES['contacts'] ?? null;
if (!$f || (int)($f['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
flash('err', '没收到文件(或上传失败)。');
redirect('people/import');
}
// ⚠️ 不去校验扩展名/类型:手机导出的 vcf 常被改名成 .txt 或 .csv,
// 按扩展名拦会把正常文件挡在外面。判据用**内容**(见 ContactImport::parse)。
$raw = (string)file_get_contents((string)$f['tmp_name']);
if (trim($raw) === '') {
flash('err', '文件是空的。');
redirect('people/import');
}
$records = ContactImport::parse($raw, (string)($f['name'] ?? ''));
if (!$records) {
flash('err', '没从这份文件里认出任何联系人。支持 .vcf(vCard)与 .csv/.txt'
. '(表头含 姓名/手机/邮箱 即可)。');
redirect('people/import');
}
$groupId = (int)($_POST['group_id'] ?? 0);
$res = ContactImporter::run($familyId, $me, $records, $groupId);
$msg = '导入完成:新增 ' . $res['added'] . ' 人,补充信息 ' . $res['updated'] . ' 人';
if ($res['skipped'] > 0) {
$msg .= ',' . $res['skipped'] . ' 条无需改动';
}
flash('ok', $msg . '。');
View::render('people/import', [
'title' => '导入联系人',
'groups' => PersonGroup::orderFor($familyId, $me),
'report' => $res,
], 'people');
break;
/**
* 「从家里已有的人里挑」—— 添加成员的第二个入口。
*
@@ -1225,6 +1322,9 @@ switch ($r) {
'errors' => [],
'canEditAll' => true,
'isSelf' => false,
// 工作经历先给一行空白(第 4 条),省得还要点「再加一段」
'jobRows' => [[]],
'myNote' => '',
], 'people');
break;
@@ -1242,7 +1342,10 @@ switch ($r) {
'org' => trim((string)($_POST['org'] ?? '')),
'title' => trim((string)($_POST['title'] ?? '')),
'note' => trim((string)($_POST['note'] ?? '')),
'slogan' => trim((string)($_POST['slogan'] ?? '')),
// 新增的人还没有"我的这一句话"(那是他自己的东西,由他之后自己填)
'slogan' => '',
// 工作经历(第 4 条):一组行,整组替换;见 PersonJob::replace
'jobs' => read_jobs_input(),
'avatar' => '',
'kind' => 'member',
// ⚠️ 这两项必须显式从表单取回:表单里有,但漏了这一步就会被静默丢掉,
@@ -1274,6 +1377,10 @@ switch ($r) {
'canEditAll' => true,
'isSelf' => false,
'contacts' => $contacts,
// 校验失败要把用户刚填的工作经历**原样回显**(第 4 条),
// 私备注同理(第 5 条)—— 别让他重填一遍。
'jobRows' => (array)($d['jobs'] ?? []),
'myNote' => trim((string)($_POST['my_note'] ?? '')),
], 'people');
exit;
}
@@ -1297,6 +1404,10 @@ switch ($r) {
'canEditAll' => true,
'isSelf' => false,
'contacts' => $contacts,
// 校验失败要把用户刚填的工作经历**原样回显**(第 4 条),
// 私备注同理(第 5 条)—— 别让他重填一遍。
'jobRows' => (array)($d['jobs'] ?? []),
'myNote' => trim((string)($_POST['my_note'] ?? '')),
], 'people');
exit;
}
@@ -1312,6 +1423,8 @@ switch ($r) {
}
PersonRoster::ensure($familyId, (int)Auth::personId(), $id, $gid > 0 ? $gid : null, 'self');
Person::replaceContacts($id, $contacts);
// 工作经历:整组替换(第 4 条)。空的那组会把这段经历清空 —— 与联系人同一口径。
PersonJob::replace($id, (array)($d['jobs'] ?? []));
clear_old();
if ($avatarWarn !== '') {
flash('ok', '已把「' . $d['name'] . '」加进名册。头像没有保存:' . $avatarWarn);
@@ -1355,6 +1468,10 @@ switch ($r) {
'phoneValue' => Crypto::decrypt((string)$p['phone_encrypted']),
'idCardValue' => Crypto::decrypt((string)$p['id_card_encrypted']),
'contacts' => $editContacts,
// 工作经历(第 4 条):库里已有的多段
'jobRows' => PersonJob::list($id),
// 我对他的私备注(第 5 条):**只有我自己**那一份
'myNote' => $isSelf ? '' : PersonNote::get($id, (int)Auth::personId()),
], 'people');
break;
@@ -1374,6 +1491,8 @@ switch ($r) {
// 改别人(= 能写的人编名册):字段是完整的一套。
// 改自己(可能是访客):只允许动属于自己的那些 —— 与 Person::updateSelf 一一对应。
$canEditAll = Auth::canWrite();
// 「我的这一句话」只能本人改(用户第 3 条)→ 非本人提交时沿用库里现有值。
$keepSlogan = (string)($p['slogan'] ?? '');
$d = [
'name' => trim((string)($_POST['name'] ?? '')),
'gender' => (string)($_POST['gender'] ?? ''),
@@ -1384,7 +1503,13 @@ switch ($r) {
'org' => trim((string)($_POST['org'] ?? '')),
'title' => trim((string)($_POST['title'] ?? '')),
'note' => trim((string)($_POST['note'] ?? '')),
'slogan' => trim((string)($_POST['slogan'] ?? '')),
// ⚠️ 用户 2026-10-03 第 3 条:这一句话**只能本人改** ——
// 表单里这一栏也只在 $isSelf 时出现。别人提交时这里读不到字段,
// 若直接 $_POST 取值会拿到空串 → **把本人的那句话清空**。
// 所以非本人一律沿用库里已有的值($keepSlogan)。
'slogan' => $isSelf ? trim((string)($_POST['slogan'] ?? '')) : $keepSlogan,
// 工作经历(第 4 条):一组行,整组替换;见 PersonJob::replace
'jobs' => read_jobs_input(),
'avatar' => (string)$p['avatar'],
'birth_rank' => $canEditAll ? trim((string)($_POST['birth_rank'] ?? '')) : ($p['birth_rank'] ?? ''),
'is_placeholder' => $canEditAll
@@ -1445,6 +1570,15 @@ switch ($r) {
exit;
}
Person::replaceContacts($id, $contacts);
// 工作经历:整组替换(第 4 条)。空的那组会把这段经历清空 —— 与联系人同一口径。
PersonJob::replace($id, (array)($d['jobs'] ?? []));
// 「我对他的私人备注」(第 5 条):落在**写的人自己**名下(person_notes)。
// ⚠️ 两条守卫:
// ① 编辑自己时表单里没有这个字段(不需要"我对我的备注")→ 别把已有的清掉;
// ② 即使有,也不该对自己写(`$id !== 我`)。
if ((int)$id !== (int)Auth::personId() && array_key_exists('my_note', $_POST)) {
PersonNote::set($id, (int)Auth::personId(), (string)$_POST['my_note']);
}
if ((int)$p['id'] === (int)Auth::personId()) {
Auth::refreshAvatar((string)$d['avatar']);
// 姓名也可能改了 —— 不同步会话的话顶栏还是旧名字
@@ -1459,6 +1593,28 @@ switch ($r) {
redirect('people/show', ['id' => $id]);
break;
/**
* 保存「我对他的私人备注」(用户 2026-10-03 第 5 条)。
*
* 独立成一个路由而不是塞进 people/update:这两件事的**可见性**不同 ——
* people/update 改的是"这个人的资料"(有写权限的人都能改),
* 而这条备注是"**我**和他之间的事",只有写的人自己看得到。
* 混在一起的话,"有写权限"就等于"能改别人对某人的私评",那是另一回事。
*/
case 'people/note':
Auth::requireLogin();
csrf_check();
$familyId = (int)Auth::familyId();
$me = (int)Auth::personId();
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0 || $id === $me || !isset(Person::map($familyId)[$id])) {
flash('err', '找不到这个人。');
redirect('people');
}
PersonNote::set($id, $me, (string)($_POST['note'] ?? ''));
flash('ok', '备注已保存(只有你自己看得到)。');
redirect('people/show', ['id' => $id]);
case 'people/show':
$familyId = (int)Auth::familyId();
$me = (int)Auth::personId();
@@ -1490,6 +1646,10 @@ switch ($r) {
// 「我叫他什么」—— 只有我自己能改,改了也只影响我看到的
'myNickname' => $id === $me ? '' : Nickname::get($me, $id),
'shown' => $id === $me ? (string)$p['name'] : (Nickname::display($me, $p) ?: (string)$p['name']),
// 工作经历(第 4 条):可以有多段,详情页整列出来
'jobs' => PersonJob::list($id),
// 我对他的私人备注(第 5 条):**只有我自己**那一份;看自己时为空
'myNote' => $id === $me ? '' : PersonNote::get($id, $me),
'birthdays' => Birthday::describe($p),
'birthdayNext'=> Birthday::nextOccurrences($p, date('Y-m-d'), 400),
// 补充联系方式(vCard 对齐):kind=phone 的 value 是密文,模板里用 maskPhone 展示