Files
lixi a4a97c03c2 新增:资料页真实化 + 编辑页——/me 读写、PATCH 三态、获赞与作品统计(T3.5-08)
资料页头部三项(展示名 / 头像 / 数字)自此全部来自服务端,176 行硬编码
demo(「萌宠新手(豆豆家长)」/ 24 / 1.8k / 2)退役。

- `PatchField<T>` 承载契约 v1.4.0 的 PATCH 三态(absent 不落键 / clear 落
  显式 null / value 落值)。三态必须由类型承载而非 `T?` 加约定:把「不改」
  也编码成 null,用户只改昵称就会连头像一起被服务端清掉。
- `UserProfile` 补 nickname / avatarUrl,展示回退 `nickname ?? username`
  **做在客户端展示层**——服务端 /me 刻意返回 DB 原值,编辑页因此只用原值
  预填,避免把展示约定固化成真实昵称。
- `ProfileController`:`/me` 主链路四态 + 两块统计独立三态(统计失败只降级
  这一块,不为两个数字丢掉整页);登出 reset 防跨账号泄漏。
- 编辑页维护三态意图而非「当前值整体提交」:昵称 / 头像各有显式清除入口,
  未改字段的键根本不进 JSON;空 patch 直接短路(服务端对空 patch 答 400)。
- 昵称校验按**码点**计 1~32 且先 btrim,与 `ck_users_nickname` 对齐;
  纯空白是校验失败而非隐式清空。
- `MediaUploader` 的 purpose 参数化 + 复用其六态编排的 `AvatarUploadSheet`
  (单图、预览确认后才交付 ready assetId,孤儿防护不变)。
- 修复 `/api/v1/me` 端口线路:该端点由 user 服务(:8082)提供,`ApiAuthRepository`
  此前只挂 auth(:8081)会得到 404。此前无人消费 me(),故这条错线一直没被
  触发;本单是第一个真实消费者,桌面实测即暴露。

测试 526 → 588(+62)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-11 15:37:00 +08:00

66 lines
2.4 KiB
Dart
Raw Permalink 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.
import 'package:flutter/foundation.dart';
/// PATCH 请求的**三态字段**:契约 v1.4.0 对「昵称 / 头像」两类天生可选的
/// 字段定型为三态语义(`UpdateMeRequest` 与 `UpdatePetRequest.avatarAssetId`):
///
/// | 态 | JSON 表现 | 服务端语义 |
/// | --- | --- | --- |
/// | [PatchField.absent] | **键不出现** | 不改 |
/// | [PatchField.clear] | 键出现且值为 `null` | 清空 |
/// | [PatchField.value] | 键出现且有值 | 设置 |
///
/// Dart 的 `String?` 只有两态(有值 / null),无法区分「不改」与「清空」——
/// 若把「不改」也编码为 `null`,未改动的字段会被服务端当成清空指令执行
/// (用户只改昵称,头像就被顺手删了)。故三态必须由类型承载,
/// 不能靠 `T?` 加约定。
///
/// 序列化一律经 [writeTo]:它是「absent 不落键」这条纪律的唯一实现处,
/// 各请求 DTO 不自行拼 map,避免某处漏写 `isPresent` 判断。
@immutable
class PatchField<T extends Object> {
/// 不改:键不出现在 JSON 里。
const PatchField.absent() : _present = false, _value = null;
/// 清空:键出现且值为 `null`。
const PatchField.clear() : _present = true, _value = null;
/// 设置为 [value]。
const PatchField.value(T value) : _present = true, _value = value;
final bool _present;
final T? _value;
/// 本次 PATCH 是否触及该字段(决定键是否落进 JSON)。
bool get isPresent => _present;
/// 是否为「显式清空」(present 且值为 null)。
bool get isClear => _present && _value == null;
/// present 且有值时的值;absent 与 clear 均为 null(两者不可由此区分)。
T? get valueOrNull => _value;
/// 按三态把自己写进 [json]absent 不落键;clear 落 `null`
/// 有值时落 [encode] 的产物(缺省原样写入)。
void writeTo(
Map<String, Object?> json,
String key, {
Object? Function(T value)? encode,
}) {
if (!_present) return;
final value = _value;
json[key] = value == null ? null : (encode?.call(value) ?? value);
}
@override
bool operator ==(Object other) =>
other is PatchField<T> &&
other._present == _present &&
other._value == _value;
@override
int get hashCode => Object.hash(_present, _value);
@override
String toString() => _present ? 'PatchField($_value)' : 'PatchField.absent()';
}