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

View File

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

View File

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