Files
FamilyBook/install.php
T
laoyang 1c460a0e83 图片可裁剪 · 名册按关系分组 · 个人设置独立 · 顶栏下拉菜单
① 上传图片一律可裁剪,比例由图位决定
   新增 app/ImageSlot.php 统一配置(头像 1:1/512×512;题图 16:9 只留注释,
   将来启用只加一行)。前端只挑区域、写回归一化矩形 "x,y,w,h",真正裁剪在服务端
   Upload::fitRegion():夹回图内 → 在框内居中收缩到目标比例(不外扩)→ 钉住 sx+sw ≤ w。
   比例解析单独写 parseRatioAttr():parseFloat('16:9') 得 16,会把题图裁成细长条且不报错。

② 名册按关系分组,顺序每人可自定
   分类全家共用(person_groups,另一个人可以自己加分类);「自己的」只体现在顺序上
   (user_settings.group_order,每人一份)。persons.group_id 故意不加外键(老库只能
   ADD COLUMN),完整性由 PersonGroup 保证:删分类把人置回未分类,人不会从名册消失。

③ 设置分两级:系统设置 / 个人设置
   个人设置拆成 templates/settings/me.php + _profile.php + _nicknames.php,
   取代 templates/people/me.php(旧路由与旧表单仍兼容)。

④ 右上角「姓名/头像 + 下拉菜单」
   原来是两个裸链接,窄屏会被 CSS 藏掉导致改密码/退出不可达;改成 <details> 零 JS 菜单
   (app.js 只补「点外面 / Esc」)。菜单项:个人设置 / 系统设置(管理员)/ 修改密码 / 退出登录。

Schema 版本 3 → 4;安装与老库升级共用 PersonGroup::backfillMissing() 一条回填路径。

验证:php -l 全量;e2e 202/0(新增「关系分组」与「图片裁剪」两节);老库升级 49/0;
桌面几何 108/0;移动端几何 192/0(360/375/390/414);交互 76/0;
反向验证 31/31(逐条把修复改回旧写法,指定的目标断言必须变红);
部署后本地↔线上 72 个文件 sha256 逐字节一致。
2026-09-30 11:06:22 +08:00

108 lines
3.9 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
/**
* 安装向导:建表 → 建家庭 → 建第一个人(同时是他/她的管理员账号)。
*
* 「是否已安装」以**数据库里的四张表**为准,不以某个文件是否存在为准 ——
* 文件可能被删、可能被恢复,表在就是装过了。
*/
declare(strict_types=1);
require __DIR__ . '/app/bootstrap.php';
use App\Account;
use App\Crypto;
use App\Database;
use App\Family;
use App\Person;
use App\Schema;
use App\View;
if (Database::hasTables()) {
// 装过之后这个入口就该消失:给 404,别让它在公网一直挂着、也别让爬虫收录
http_response_code(404);
View::renderBare('error', [
'title' => '已经装好了',
'message' => '「家书」已经安装过。如果要重新安装,请先清空数据库里的 families / persons / accounts / settings 四张表。',
]);
exit;
}
$dbName = $GLOBALS['config']['db']['name'];
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
csrf_check();
$familyName = trim((string)($_POST['family_name'] ?? ''));
$motto = trim((string)($_POST['motto'] ?? ''));
$name = trim((string)($_POST['name'] ?? ''));
$phone = trim((string)($_POST['phone'] ?? ''));
$password = (string)($_POST['password'] ?? '');
$errors = [];
if ($familyName === '') { $errors['_'] = '家庭名称不能为空'; }
if ($name === '') { $errors['name'] = '请填写你的姓名'; }
if (!preg_match('/^\+?[0-9\-]{6,20}$/', $phone)) {
$errors['phone'] = '手机号格式看起来不对';
}
$pwErr = Account::validatePassword($password);
if ($pwErr) { $errors['password'] = implode(';', $pwErr); }
if (!$errors) {
try {
// ensure() 而不是逐条 exec(statements()):它还会补齐后加的列与枚举值,
// 所以老库重新跑一次就能自动升级,不必手工改表。
Schema::ensure();
$familyId = Family::create($familyName, $motto);
// 关系分组的预置值:在这里建好,免得「装完第一眼进名册发现一个分组都没有」
// (ensure() 里也会补,但那要等下一次请求)
\App\PersonGroup::seedDefaults((int)$familyId);
// 装完就把结构版本盖上:不盖的话,第一次进首页发现"版本对不上",
// 会再跑一次 ensure()(幂等,不出错,但白跑一遍 ALTER 查询)。
\App\Setting::set($familyId, 'schema_version', Schema::VERSION);
$personId = Person::create($familyId, [
'name' => $name,
'gender' => '',
'avatar' => '',
'phone' => $phone,
'id_card' => '',
'email' => '',
'address' => '',
'note' => '',
'slogan' => '',
'sort_order'=> 1,
'kind' => 'member',
]);
Account::create($personId, $password, 'admin');
// 创始人(=第一个管理员)默认归到「家人」。
// 用 backfillMissing 而不是自己算组 id:规则「member → 家人」只写在一处,
// 安装与老库升级才不会有第二种说法。
\App\PersonGroup::backfillMissing(Database::get());
// 装完就删掉自己 —— 避免把安装入口长期暴露在公网
@unlink(__FILE__);
header('Location: index.php?r=auth/login');
exit;
} catch (\Throwable $ex) {
$errors['_'] = '安装失败:' . $ex->getMessage();
}
}
set_old($_POST);
View::renderBare('install', [
'title' => '安装家书',
'dbName' => $dbName,
'errors' => $errors,
]);
exit;
}
clear_old();
View::renderBare('install', [
'title' => '安装家书',
'dbName' => $dbName,
'errors' => [],
]);