Files
FamilyBook/app/Attachment.php
T
laoyang 71a7ec35a0 v14: 观影/家谱/日程/旅程/资源/任务六模块统一,首页改 dashboard
用户 14 条需求一次交付(1-13 功能 + 14 工作方式)。

观影(1-4)
- 返回文案全模块统一为「返回观影」(不再有「返回列表」「返回观影记录」三种叫法)
- 观影人一律用彩色人物标签 person_chip:底色由头像主色算出来(上传时存库),
  瀑布流卡片也按人显示;五处观影模板全部改用它
- 人员显示统一走 person_name()(shown → 我给他起的称呼 → 姓名)
- 影院管理页与年度报告页的上下两块用 .card + .card 统一给间距(原来紧贴成一整块)

家谱(5)
- 树根从「我」换成**辈分最高的祖先**:库画的是"树根的祖先链+后代+树根的兄弟姐妹",
  根=我 时伯父/堂兄/嫂子/侄子一个都不画(12 人夹具只画出 8 人)
- focusMap 按**代数**圈定三代(不再按步数),normalizeLinks 把子女对称挂到父母双方
  —— 修掉"母亲登录看不到孩子"
- 新增「看全部 / 回到我」两个按钮,放在画布外(画布整块被库接管了手势)

统一事件原语(6-9)
- 旅程:TripLog 全量迁入(3 趟旅程 / 21 段行程 / 72 张照片 / 5 个人)。
  复用 events(kind='trip') + trip_details + trip_nodes(计划/执行双轨保留)
- 日程:每人可建日历本(自定义色)+ 共享只读/可写;观影与旅行是每人默认两本,
  内容是**从各模块实时派生**的(不复制,所以改了观影日期日历立刻跟着变);
  没有具体时分 = 全天;月视图参照群晖 Calendar
- 资源:挂在某人名下的附属信息(标题+正文+多附件),可共享给多人并设只读/读写,
  默认折叠、可搜标题与正文
- 任务:看板三列 + 两级子任务 + 评论@人 + 共享 + 循环(完成时按原截止日推下一期)
- 统一附件表:四个模块的附件共用一张,下载地址不落库、一律由 media.php 现算

模块化(10-11)
- App\Module 是模块的唯一来源(加一行 = 导航条/主题色/个人设置三处自动出现)
- 个人设置里可勾选要显示哪些模块、拖动排序(关掉的模块**不会**被兜底补回来)
- 每个模块一套主题色(家庭级),只覆盖 --brand 三兄弟,不碰纸墨底色

其他(12-13)
- 新增成员表单可选所属分组(编辑表单没有:分组是"我和他的关系",只归我自己)
- 首页改 dashboard:接下来的日子 / 未来七天 / 手头在做 / 往年今日。
  家人卡片从首页撤走(看人去名册)

验证(全部通过)
- e2e 542 条(新增 4 模块端到端 60+ 条)
- 桌面几何 368 / 移动端几何 520 / 家谱 12 / 交互 97 / 老库升级 123
- SQL 列名静态校验 0 处可疑(对着真实表结构)
- 反向验证 18 条新注入全部抓住

顺带修掉的既有问题
- 老库升级回填 SQL 按实际存在的列动态拼(缺列不再 1054 炸断升级)
- 全新安装时 Schema::ensure 跑在"人还没建"之前 → 补建日历本三处齐备
- 窄屏导航横向溢出(模块从 4 个加到 8 个后顶栏撑破视口)
2026-10-02 09:28:07 +08:00

172 lines
6.6 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
/**
* 统一附件(v12):旅程照片 / 资源附件 / 任务附件 / 评论附件,全都走这一张表。
*
* 为什么要统一:这几个模块对附件的要求其实是同一件事 ——
* "一个宿主挂多个文件、能下载、图片能预览、删宿主时别留下孤儿"。
* 各写一张表就要把"下载/预览/权限"写四遍,将来再加模块还得写第五遍。
*
* 下载地址**不落库**:一律由 media.php?k=<key> 现算(地址形态换过一次,
* 历史数据自动生效、零迁移 —— 这是 FeedLite 那边学到的最省事的一条)。
*/
declare(strict_types=1);
namespace App;
use App\Storage\Storage;
final class Attachment
{
/** 图片扩展名 → MIME(服务端自己判,不信浏览器给的类型) */
private const IMG_MIME = [
'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png',
'gif' => 'image/gif', 'webp' => 'image/webp', 'heic' => 'image/heic',
];
public const MAX_PER_HOST = 60;
/** 某个宿主的全部附件 */
public static function list(string $type, int $targetId): array
{
$st = Database::get()->prepare(
'SELECT * FROM attachments WHERE target_type = ? AND target_id = ? ORDER BY position, id'
);
$st->execute([$type, $targetId]);
return $st->fetchAll();
}
/**
* 存一个上传来的文件。
*
* @return array{ok:bool,error?:string,id?:int}
*/
public static function save(int $familyId, string $type, int $targetId, array $file, int $uploaderId = 0): array
{
if (!array_key_exists('error', $file)) {
return ['ok' => false, 'error' => ''];
}
if (is_array($file['error'])) {
return ['ok' => false, 'error' => '上传参数不对'];
}
if ($file['error'] === UPLOAD_ERR_NO_FILE) {
return ['ok' => false, 'error' => ''];
}
if ($file['error'] !== UPLOAD_ERR_OK) {
return ['ok' => false, 'error' => '上传失败(错误码 ' . (int)$file['error'] . ')'];
}
$stc = Database::get()->prepare('SELECT COUNT(*) FROM attachments WHERE target_type = ? AND target_id = ?');
$stc->execute([$type, $targetId]);
if ((int)$stc->fetchColumn() >= self::MAX_PER_HOST) {
return ['ok' => false, 'error' => '单个条目最多 ' . self::MAX_PER_HOST . ' 个附件'];
}
$tmp = (string)$file['tmp_name'];
$size = (int)@filesize($tmp);
$fino = new \finfo(FILEINFO_MIME_TYPE);
$mime = (string)$fino->file($tmp);
$name = self::safeName((string)($file['name'] ?? ''));
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if ($ext === '' && isset(self::IMG_MIME[$mime])) {
$ext = explode('/', $mime)[1] ?? '';
}
// 图片走 photo 槽位(ratio='free':不裁剪,等比缩到最长边 1600);
// 非图片(PDF/文档)原样存 —— 压缩反而会把它弄坏。
// ⚠️ 槽位键必须是 ImageSlot::SLOTS 里真实存在的那个:曾经这里写 'free'
// (把"不裁剪"的 ratio 当成槽位名用了),于是 Upload::image 直接返回
// 「上传位置配置不正确」,所有附件上传静默失败。
$isImage = strpos($mime, 'image/') === 0;
if ($isImage) {
$up = Upload::image($familyId, $file, 'photo');
if (empty($up['ok'])) {
return ['ok' => false, 'error' => (string)($up['error'] ?? '存图失败')];
}
$key = (string)$up['file'];
} else {
$key = 'files/' . bin2hex(random_bytes(12)) . ($ext !== '' ? '.' . preg_replace('/[^a-z0-9]/', '', $ext) : '');
$res = Storage::putFile($familyId, $tmp, $key, $mime !== '' ? $mime : 'application/octet-stream');
if (empty($res['ok'])) {
return ['ok' => false, 'error' => '保存失败:' . (string)($res['error'] ?? '')];
}
}
$db = Database::get();
$db->prepare(
'INSERT INTO attachments
(family_id, target_type, target_id, `key`, name, mime, size, is_image, position, uploader_id)
VALUES (?,?,?,?,?,?,?,?,?,?)'
)->execute([
$familyId, $type, $targetId, $key,
$name !== '' ? $name : null,
$mime !== '' ? $mime : null,
$size > 0 ? $size : null,
$isImage ? 1 : 0,
self::nextPosition($type, $targetId),
$uploaderId > 0 ? $uploaderId : null,
]);
return ['ok' => true, 'id' => (int)$db->lastInsertId()];
}
/** 删一个附件(连存储对象一起) */
public static function remove(int $id): void
{
$st = Database::get()->prepare('SELECT family_id, `key` FROM attachments WHERE id = ?');
$st->execute([$id]);
$r = $st->fetch();
if (!$r) {
return;
}
Database::get()->prepare('DELETE FROM attachments WHERE id = ?')->execute([$id]);
Storage::delete((int)$r['family_id'], (string)$r['key']);
}
/** 删某个宿主的全部附件(删旅程 / 删资源 / 删任务时调) */
public static function removeAll(string $type, int $targetId): void
{
foreach (self::list($type, $targetId) as $a) {
self::remove((int)$a['id']);
}
}
/** 能在浏览器里直接预览吗(图片 / PDF) */
public static function previewable(array $a): bool
{
if ((int)($a['is_image'] ?? 0) === 1) {
return true;
}
return strpos((string)($a['mime'] ?? ''), 'pdf') !== false;
}
/** 人话的大小 */
public static function sizeWord($bytes): string
{
$b = (int)$bytes;
if ($b <= 0) {
return '';
}
if ($b < 1024) {
return $b . ' B';
}
if ($b < 1024 * 1024) {
return round($b / 1024, 1) . ' KB';
}
return round($b / 1024 / 1024, 1) . ' MB';
}
private static function safeName(string $n): string
{
$n = str_replace(['\\', '/', ':', '*', '?', '"', '<', '>', '|'], '', $n);
$n = trim(preg_replace('/\s+/u', ' ', $n));
return mb_substr($n, 0, 180, 'UTF-8');
}
private static function nextPosition(string $type, int $targetId): int
{
$st = Database::get()->prepare(
'SELECT COALESCE(MAX(position),0)+10 FROM attachments WHERE target_type = ? AND target_id = ?'
);
$st->execute([$type, $targetId]);
return (int)$st->fetchColumn();
}
}