feat(nsfw): 新增账号成人内容偏好并默认开启

- src/lib/digital.ts: 补充账号 NSFW 偏好协议,兼容未返回偏好的旧响应
- src/components/digital-home.tsx: 我的页支持保存开关,收藏更新保留服务端偏好
- src/components/digital.css: 增加开关样式与键盘焦点提示
- README.md: 说明默认值、游客限制与前后端配套发布要求
This commit is contained in:
湛兮
2026-10-27 20:18:16 +08:00
parent 3530219123
commit cb5e004eb9
4 changed files with 87 additions and 7 deletions
+34 -7
View File
@@ -1,6 +1,6 @@
"use client";
/* eslint-disable @next/next/no-img-element -- 本地封面直接读取,避免在小型服务器上运行图片转换。 */
/** 星港数字人空间:探索、会话、世界书和身份资料的响应式入口。 */
/** 星港数字人空间:探索、会话、世界书、身份资料和账号内容偏好。 */
import Link from "next/link";
import {
useCallback,
@@ -502,8 +502,13 @@ export function DigitalHome({
: [...profile.favorites, card.id],
};
try {
await digital("profile", "PUT", next);
setProfile(next);
// 收藏不写内容开关,避免旧页面状态覆盖管理员刚保存的账号偏好。
const saved = await digital<Profile>("profile", "PUT", {
favorites: next.favorites,
persona: next.persona,
userName: next.userName,
});
setProfile(saved);
} catch (e) {
reportError(e);
} finally {
@@ -874,11 +879,15 @@ export function DigitalHome({
...profile,
userName: profileDraft.userName,
persona: profileDraft.persona,
nsfwEnabled: profileDraft.nsfwEnabled ?? true,
};
try {
await digital("profile", "PUT", next);
setProfile(next);
setNotice("身份资料已保存");
const saved = await digital<Profile>("profile", "PUT", next);
if (saved.nsfwEnabled !== next.nsfwEnabled)
throw new Error("内容偏好未保存,请稍后重试");
setProfile(saved);
setProfileDraft(saved);
setNotice("身份与内容偏好已保存");
} catch (e) {
reportError(e);
}
@@ -917,6 +926,7 @@ export function DigitalHome({
}
setConversation(null);
currentId.current = null;
if (value === "me" && tab !== "me" && profile) setProfileDraft(profile);
setTab(value);
setError("");
}
@@ -1763,9 +1773,26 @@ export function DigitalHome({
<p className="dh-form-note">
此资料可在新会话的「对话设定」中使用。每个会话的设定相互独立。
</p>
<label className="dh-content-preference">
<span>
<strong>NSFW 成人内容</strong>
<small>登录账号默认开启,可随时关闭;保存后用于后续回复。</small>
</span>
<input
type="checkbox"
role="switch"
checked={profileDraft.nsfwEnabled ?? true}
onChange={(event) =>
setProfileDraft({
...profileDraft,
nsfwEnabled: event.target.checked,
})
}
/>
</label>
<button className="dh-primary" type="submit">
<Check size={16} />
保存身份
保存身份与偏好
</button>
<div className="dh-profile-stats">
<div>