feat: 增强邀请页面的已加入检查,如果已登录则会发起请求查询是否为群组成员

pull/64/head
moonrailgun 2 years ago
parent 2027c1ee17
commit f5b71b076e

@ -172,6 +172,18 @@ export async function quitGroup(groupId: string) {
});
}
/**
*
* @param groupId ID
*/
export async function isMember(groupId: string): Promise<boolean> {
const { data } = await request.post('/api/group/isMember', {
groupId,
});
return data;
}
/**
*
* @param groupId ID

@ -7,6 +7,8 @@ import {
applyGroupInvite,
checkTokenValid,
getCachedGroupInviteInfo,
model,
showErrorToasts,
t,
useAsync,
useAsyncRequest,
@ -36,16 +38,50 @@ export const JoinBtn: React.FC<Props> = 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(<SuccessModal groupId={invite?.groupId ?? ''} />, {
if (!invite) {
showErrorToasts(t('未找到邀请码信息'));
return;
}
openModal(<SuccessModal groupId={invite.groupId} />, {
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<Props> = React.memo((props) => {
if (isJoined) {
return (
<Button block={true} type="primary" size="large" disabled={true}>
{t('已加入')}
<Button
block={true}
type="primary"
size="large"
onClick={handleJumpToGroup}
>
{t('已加入,跳转到群组')}
</Button>
);
}

@ -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);
}
/**
*
*/

Loading…
Cancel
Save