diff --git a/client/shared/model/group.ts b/client/shared/model/group.ts index d9a30932..118251a2 100644 --- a/client/shared/model/group.ts +++ b/client/shared/model/group.ts @@ -172,6 +172,18 @@ export async function quitGroup(groupId: string) { }); } +/** + * 检查当前用户是否是群组成员 + * @param groupId 群组ID + */ +export async function isMember(groupId: string): Promise { + const { data } = await request.post('/api/group/isMember', { + groupId, + }); + + return data; +} + /** * 更新用户所在的权限组 * @param groupId 群组ID diff --git a/client/web/src/routes/Invite/JoinBtn.tsx b/client/web/src/routes/Invite/JoinBtn.tsx index f021b1c2..daf63786 100644 --- a/client/web/src/routes/Invite/JoinBtn.tsx +++ b/client/web/src/routes/Invite/JoinBtn.tsx @@ -7,6 +7,8 @@ import { applyGroupInvite, checkTokenValid, getCachedGroupInviteInfo, + model, + showErrorToasts, t, useAsync, useAsyncRequest, @@ -36,16 +38,50 @@ export const JoinBtn: React.FC = React.memo((props) => { ); }, []); + const { value: invite } = useAsync(() => { + return getCachedGroupInviteInfo(props.inviteCode); + }, [props.inviteCode]); + + useAsync(async () => { + // 检查用户是否已经加入 + if (!isTokenValid) { + return; + } + + if (!invite) { + return; + } + + try { + const isMember = await model.group.isMember(invite.groupId); + if (isMember) { + setIsJoined(true); + } + } catch (err) {} + }, [isTokenValid, invite]); + const [{ loading: joinLoading }, handleJoinGroup] = useAsyncRequest(async () => { await applyGroupInvite(props.inviteCode); - const invite = await getCachedGroupInviteInfo(props.inviteCode); - openModal(, { + if (!invite) { + showErrorToasts(t('未找到邀请码信息')); + return; + } + openModal(, { maskClosable: false, }); setIsJoined(true); - }, [props.inviteCode]); + }, [props.inviteCode, invite]); + + const [, handleJumpToGroup] = useAsyncRequest(async () => { + if (!invite) { + showErrorToasts(t('未找到邀请码信息')); + return; + } + + navigate(`/main/group/${invite.groupId}`); + }, [navigate, invite]); if (loading) { return null; @@ -53,8 +89,13 @@ export const JoinBtn: React.FC = React.memo((props) => { if (isJoined) { return ( - ); } diff --git a/server/services/core/group/group.service.ts b/server/services/core/group/group.service.ts index de854dc7..10179af2 100644 --- a/server/services/core/group/group.service.ts +++ b/server/services/core/group/group.service.ts @@ -89,6 +89,11 @@ class GroupService extends TcService { groupId: 'string', }, }); + this.registerAction('isMember', this.isMember, { + params: { + groupId: 'string', + }, + }); this.registerAction('appendGroupMemberRoles', this.appendGroupMemberRoles, { params: { groupId: 'string', @@ -508,6 +513,24 @@ class GroupService extends TcService { } } + /** + * 检查是否为群组成员 + */ + async isMember(ctx: TcContext<{ groupId: string }>) { + const groupId = ctx.params.groupId; + const userId = ctx.meta.userId; + + const groupInfo = await call(ctx).getGroupInfo(groupId); + if (!groupInfo) { + // 没有找到群组信息 + return false; + } + + const members = groupInfo.members; + + return members.some((m) => String(m.userId) === userId); + } + /** * 追加群组成员的角色 */