更新小程序配置,切换API基础地址至真实后端。实现@用户提及功能,允许在内容中插入用户信息并高亮显示,优化用户体验。调整后端逻辑以支持免费章节的判断,确保章节信息的准确性与一致性。

This commit is contained in:
Alex-larget
2026-03-08 11:56:27 +08:00
parent 73ecead336
commit 90d32a5121
3 changed files with 181 additions and 14 deletions

View File

@@ -36,6 +36,7 @@ import {
Plus,
Image as ImageIcon,
Search,
AtSign,
} from 'lucide-react'
import { get, put, del } from '@/api/client'
import { ChapterTree } from './ChapterTree'
@@ -156,6 +157,12 @@ export function ContentPage() {
const [editingPart, setEditingPart] = useState<{ id: string; title: string } | null>(null)
const [isSavingPartTitle, setIsSavingPartTitle] = useState(false)
const [showMentionUserDialog, setShowMentionUserDialog] = useState(false)
const [mentionTarget, setMentionTarget] = useState<'new' | 'edit'>('new')
const [mentionUserList, setMentionUserList] = useState<{ id: string; nickname: string }[]>([])
const [mentionUserSearch, setMentionUserSearch] = useState('')
const [mentionUserLoading, setMentionUserLoading] = useState(false)
const tree = buildTree(sectionsList)
const totalSections = sectionsList.length
@@ -178,6 +185,43 @@ export function ContentPage() {
loadList()
}, [])
const loadMentionUsers = useCallback(async () => {
setMentionUserLoading(true)
try {
const params = new URLSearchParams({ pageSize: '200' })
if (mentionUserSearch.trim()) params.set('search', mentionUserSearch.trim())
const data = await get<{ success?: boolean; users?: { id: string; nickname?: string }[] }>(
`/api/db/users?${params}`,
)
const users = Array.isArray(data?.users) ? data.users : []
setMentionUserList(users.map((u) => ({ id: String(u.id), nickname: u.nickname || '用户' })))
} catch (e) {
console.error(e)
setMentionUserList([])
} finally {
setMentionUserLoading(false)
}
}, [mentionUserSearch])
useEffect(() => {
if (showMentionUserDialog) loadMentionUsers()
}, [showMentionUserDialog, loadMentionUsers])
const openMentionUserDialog = (target: 'new' | 'edit') => {
setMentionTarget(target)
setMentionUserSearch('')
setShowMentionUserDialog(true)
}
const insertMentionUser = (user: { id: string; nickname: string }) => {
const snippet = `{{@${user.id}:${user.nickname}}}`
if (mentionTarget === 'new') {
setNewSection((prev) => ({ ...prev, content: (prev.content || '') + snippet }))
} else if (editingSection) {
setEditingSection({ ...editingSection, content: (editingSection.content || '') + snippet })
}
setShowMentionUserDialog(false)
}
const togglePart = (partId: string) => {
setExpandedParts((prev) =>
@@ -563,10 +607,22 @@ export function ContentPage() {
</div>
</div>
<div className="space-y-2">
<Label className="text-gray-300"> (Markdown格式)</Label>
<div className="flex items-center justify-between">
<Label className="text-gray-300"> (Markdown格式)</Label>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => openMentionUserDialog('new')}
className="border-gray-600 text-gray-300 hover:bg-gray-700/50 bg-transparent"
>
<AtSign className="w-4 h-4 mr-1" />
@用户
</Button>
</div>
<Textarea
className="bg-[#0a1628] border-gray-700 text-white min-h-[300px] font-mono text-sm placeholder:text-gray-500"
placeholder="输入章节内容..."
placeholder="输入章节内容... 可点击「插入 @用户」插入 {{@用户ID:昵称}},小程序内将高亮可点"
value={newSection.content}
onChange={(e) => setNewSection({ ...newSection, content: e.target.value })}
/>
@@ -746,6 +802,16 @@ export function ContentPage() {
<div className="flex items-center justify-between">
<Label className="text-gray-300"> (Markdown格式)</Label>
<div className="flex gap-2">
<Button
type="button"
variant="outline"
size="sm"
onClick={() => openMentionUserDialog('edit')}
className="border-gray-600 text-gray-300 hover:bg-gray-700/50 bg-transparent"
>
<AtSign className="w-4 h-4 mr-1" />
@用户
</Button>
<input
ref={imageInputRef}
type="file"
@@ -807,6 +873,55 @@ export function ContentPage() {
</DialogContent>
</Dialog>
{/* 插入 @用户 - 选择用户弹窗 */}
<Dialog open={showMentionUserDialog} onOpenChange={setShowMentionUserDialog}>
<DialogContent className="bg-[#0f2137] border-gray-700 text-white max-w-md max-h-[80vh] overflow-hidden flex flex-col" showCloseButton>
<DialogHeader>
<DialogTitle className="text-white flex items-center gap-2">
<AtSign className="w-5 h-5 text-[#38bdac]" />
@用户
</DialogTitle>
</DialogHeader>
<p className="text-gray-400 text-sm"> {'{{@用户ID:昵称}}'}</p>
<div className="space-y-2">
<Input
className="bg-[#0a1628] border-gray-700 text-white placeholder:text-gray-500"
placeholder="搜索昵称或 ID..."
value={mentionUserSearch}
onChange={(e) => setMentionUserSearch(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && loadMentionUsers()}
/>
<Button
type="button"
variant="outline"
size="sm"
onClick={loadMentionUsers}
disabled={mentionUserLoading}
className="border-gray-600 text-gray-300 hover:bg-gray-700/50 bg-transparent w-full"
>
{mentionUserLoading ? <RefreshCw className="w-4 h-4 animate-spin" /> : <Search className="w-4 h-4" />}
{mentionUserLoading ? '加载中...' : '搜索'}
</Button>
</div>
<div className="flex-1 overflow-y-auto min-h-[200px] border border-gray-700 rounded-md bg-[#0a1628]">
{mentionUserList.length === 0 && !mentionUserLoading && (
<div className="p-4 text-center text-gray-500 text-sm"></div>
)}
{mentionUserList.map((user) => (
<button
key={user.id}
type="button"
onClick={() => insertMentionUser(user)}
className="w-full px-4 py-2 text-left hover:bg-[#38bdac]/20 border-b border-gray-700/50 last:border-0 flex justify-between items-center"
>
<span className="text-white truncate">{user.nickname}</span>
<span className="text-gray-500 text-xs font-mono shrink-0 ml-2">{user.id}</span>
</button>
))}
</div>
</DialogContent>
</Dialog>
<Tabs defaultValue="chapters" className="space-y-6">
<TabsList className="bg-[#0f2137] border border-gray-700/50 p-1">
<TabsTrigger value="chapters" className="data-[state=active]:bg-[#38bdac]/20 data-[state=active]:text-[#38bdac] text-gray-400">