feat: 聊天界面增加认证

This commit is contained in:
liuyonghe0111 2025-12-19 14:58:19 +08:00
parent 5ee481adaa
commit 35d63bdd64
3 changed files with 30 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import { Button } from '@/components/ui/button';
import type React from 'react'; import type React from 'react';
import { useAsyncFn } from '@/hooks/tools'; import { useAsyncFn } from '@/hooks/tools';
import { useStreamChatStore } from '@/app/(main)/chat/[id]/stream-chat'; import { useStreamChatStore } from '@/app/(main)/chat/[id]/stream-chat';
import { toast } from 'sonner';
export default function ChatButton({ export default function ChatButton({
id, id,
@ -13,9 +14,16 @@ export default function ChatButton({
const createChannel = useStreamChatStore((s) => s.createChannel); const createChannel = useStreamChatStore((s) => s.createChannel);
const { loading, run: createChannelAndPush } = useAsyncFn(async () => { const { loading, run: createChannelAndPush } = useAsyncFn(async () => {
const channelId = await createChannel(id); const res = await createChannel(id);
if (!channelId) return; if (res.result === 'error') {
router.push(`/chat/${channelId}`); if (res.error === 'need_auth') {
router.push(`/login?redirect=${encodeURIComponent(`/character/${id}`)}`);
} else {
toast.error(res.error);
}
return;
}
router.push(`/chat/${res.channelId}`);
}); });
return ( return (

View File

@ -15,6 +15,7 @@ import { useStreamChatStore } from '@/app/(main)/chat/[id]/stream-chat';
import { useAsyncFn } from '@/hooks/tools'; import { useAsyncFn } from '@/hooks/tools';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import { useModels } from '@/hooks/services/chat'; import { useModels } from '@/hooks/services/chat';
import { toast } from 'sonner';
const genderMap = { const genderMap = {
0: '/icons/male.svg', 0: '/icons/male.svg',
@ -90,8 +91,11 @@ export default function Profile({ onActiveTab }: ProfileProps) {
const deleteChannel = useStreamChatStore((s) => s.deleteChannel); const deleteChannel = useStreamChatStore((s) => s.deleteChannel);
const { loading: creating, run: createChannelAndPush } = useAsyncFn(async () => { const { loading: creating, run: createChannelAndPush } = useAsyncFn(async () => {
const channelId = await createChannel(characterId); const { result, channelId, error } = await createChannel(characterId);
if (!channelId) return; if (result === 'error') {
toast.error(error);
return;
}
router.push(`/chat/${channelId}`); router.push(`/chat/${channelId}`);
}); });

View File

@ -47,7 +47,9 @@ interface StreamChatStore {
updateUserChatSetting: () => Promise<void>; updateUserChatSetting: () => Promise<void>;
// 创建某个角色的聊天频道, 返回channelId // 创建某个角色的聊天频道, 返回channelId
createChannel: (characterId: string) => Promise<string | false>; createChannel: (
characterId: string
) => Promise<{ result: 'ok' | 'error'; channelId?: string; error?: 'need_auth' | string }>;
switchToChannel: (id: string) => Promise<void>; switchToChannel: (id: string) => Promise<void>;
queryChannels: (filter: any) => Promise<Channel[]>; queryChannels: (filter: any) => Promise<Channel[]>;
deleteChannel: ( deleteChannel: (
@ -98,19 +100,21 @@ export const useStreamChatStore = create<StreamChatStore>((set, get) => ({
const { user, client } = get(); const { user, client } = get();
const { switchToChannel, queryChannels } = get(); const { switchToChannel, queryChannels } = get();
if (!client) { if (!client) {
return false; return { result: 'error', error: 'need_auth' };
} }
const { data } = await createChannel({ const { data } = await protect(() =>
userId: user.userId, createChannel({
userName: user.userName, userId: user.userId,
characterId, userName: user.userName,
}); characterId,
})
);
if (!data?.channelId) { if (!data?.channelId) {
return false; return { result: 'error', error: 'create_channel_failed' };
} }
await queryChannels({}); await queryChannels({});
switchToChannel(data.channelId); switchToChannel(data.channelId);
return data.channelId; return { result: 'ok', channelId: data.channelId };
}, },
setChatSetting: (setting: any) => { setChatSetting: (setting: any) => {