2026-03-09 21:29:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 群组服务
|
|
|
|
|
|
* 处理群组管理、成员管理、群设置、群公告等功能
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* 使用 RESTful API: /api/v1/groups
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { api } from './api';
|
|
|
|
|
|
import {
|
|
|
|
|
|
GroupResponse,
|
|
|
|
|
|
GroupMemberResponse,
|
|
|
|
|
|
GroupAnnouncementResponse,
|
|
|
|
|
|
GroupListResponse,
|
|
|
|
|
|
GroupMemberListResponse,
|
|
|
|
|
|
GroupAnnouncementListResponse,
|
|
|
|
|
|
CreateGroupRequest,
|
|
|
|
|
|
InviteMembersRequest,
|
|
|
|
|
|
TransferOwnerRequest,
|
|
|
|
|
|
SetRoleRequest,
|
|
|
|
|
|
SetNicknameRequest,
|
|
|
|
|
|
SetMuteAllRequest,
|
|
|
|
|
|
SetJoinTypeRequest,
|
|
|
|
|
|
CreateAnnouncementRequest,
|
|
|
|
|
|
MyMemberInfoResponse,
|
|
|
|
|
|
SetGroupAvatarRequest,
|
|
|
|
|
|
HandleGroupRequestAction,
|
|
|
|
|
|
} from '../types/dto';
|
|
|
|
|
|
|
|
|
|
|
|
// 群组服务类(纯 API 层)
|
|
|
|
|
|
// SQLite/内存缓存编排由 groupManager 统一处理
|
|
|
|
|
|
class GroupService {
|
|
|
|
|
|
async fetchGroupsFromApi(page = 1, pageSize = 20): Promise<GroupListResponse> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
const response = await api.get<GroupListResponse>('/groups', {
|
2026-03-09 21:29:03 +08:00
|
|
|
|
page,
|
|
|
|
|
|
page_size: pageSize,
|
|
|
|
|
|
});
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fetchGroupFromApi(id: number | string): Promise<GroupResponse> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
const response = await api.get<GroupResponse>(`/groups/${encodeURIComponent(String(id))}`);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fetchMembersFromApi(
|
|
|
|
|
|
groupId: number | string,
|
|
|
|
|
|
page = 1,
|
|
|
|
|
|
pageSize = 50
|
|
|
|
|
|
): Promise<GroupMemberListResponse> {
|
|
|
|
|
|
const response = await api.get<GroupMemberListResponse>(
|
2026-03-10 20:52:50 +08:00
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/members`,
|
2026-03-09 21:29:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
page,
|
|
|
|
|
|
page_size: pageSize,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 群组管理 ====================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建群组
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async createGroup(data: CreateGroupRequest): Promise<GroupResponse> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
const response = await api.post<GroupResponse>('/groups', data);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取群组列表
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* GET /api/v1/groups
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async getGroups(page = 1, pageSize = 20): Promise<GroupListResponse> {
|
|
|
|
|
|
return this.fetchGroupsFromApi(page, pageSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取群组详情
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* GET /api/v1/groups/:id
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async getGroup(id: number | string): Promise<GroupResponse> {
|
|
|
|
|
|
return this.fetchGroupFromApi(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前用户在群组中的成员信息
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* GET /api/v1/groups/:id/me
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async getMyMemberInfo(groupId: number | string): Promise<MyMemberInfoResponse> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
const response = await api.get<MyMemberInfoResponse>(
|
|
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/me`
|
|
|
|
|
|
);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解散群组
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* DELETE /api/v1/groups/:id
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async dissolveGroup(id: number | string): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.delete(`/groups/${encodeURIComponent(String(id))}`);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 转让群主
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/transfer
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async transferOwner(id: number | string, data: TransferOwnerRequest): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.post(`/groups/${encodeURIComponent(String(id))}/transfer`, data);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 成员管理 ====================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 邀请成员加入群组
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/invitations
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async inviteMembers(groupId: number | string, data: InviteMembersRequest): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.post(`/groups/${encodeURIComponent(String(groupId))}/invitations`, data);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 申请加入群组
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/join-requests
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async joinGroup(groupId: number | string): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.post(`/groups/${encodeURIComponent(String(groupId))}/join-requests`);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* 响应群邀请/申请
|
|
|
|
|
|
* POST /api/v1/groups/:id/join-requests/respond
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
2026-03-10 20:52:50 +08:00
|
|
|
|
async respondInvite(groupId: number | string, data: HandleGroupRequestAction): Promise<void> {
|
|
|
|
|
|
await api.post(
|
|
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/join-requests/respond`,
|
|
|
|
|
|
data
|
|
|
|
|
|
);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 审批主动加群申请
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/join-requests/handle
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
2026-03-10 20:52:50 +08:00
|
|
|
|
async reviewJoinRequest(groupId: number | string, data: HandleGroupRequestAction): Promise<void> {
|
|
|
|
|
|
await api.post(
|
|
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/join-requests/handle`,
|
|
|
|
|
|
data
|
|
|
|
|
|
);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 退出群组
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/leave
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async leaveGroup(groupId: number | string): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.post(`/groups/${encodeURIComponent(String(groupId))}/leave`);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取群组成员列表
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* GET /api/v1/groups/:id/members
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async getMembers(groupId: number | string, page = 1, pageSize = 50): Promise<GroupMemberListResponse> {
|
|
|
|
|
|
return this.fetchMembersFromApi(groupId, page, pageSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 移除群成员(踢人)
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/members/kick
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async removeMember(groupId: number | string, userId: string): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.post(`/groups/${encodeURIComponent(String(groupId))}/members/kick`, {
|
2026-03-09 21:29:03 +08:00
|
|
|
|
user_id: userId,
|
|
|
|
|
|
reject_add_request: false,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置成员角色(设置/取消管理员)
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* PUT /api/v1/groups/:id/members/:user_id/admin
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async setMemberRole(
|
|
|
|
|
|
groupId: number | string,
|
|
|
|
|
|
userId: string,
|
|
|
|
|
|
data: SetRoleRequest
|
|
|
|
|
|
): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.put(
|
|
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/members/${encodeURIComponent(userId)}/admin`,
|
|
|
|
|
|
{
|
|
|
|
|
|
enable: data.role === 'admin',
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置群昵称
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* PUT /api/v1/groups/:id/members/me/nickname
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async setNickname(groupId: number | string, data: SetNicknameRequest): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.put(`/groups/${encodeURIComponent(String(groupId))}/members/me/nickname`, data);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 禁言/解禁成员
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/members/ban
|
2026-03-09 21:29:03 +08:00
|
|
|
|
* @param groupId 群组ID
|
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
|
* @param duration 禁言时长(秒),0表示解除禁言
|
|
|
|
|
|
*/
|
|
|
|
|
|
async muteMember(
|
|
|
|
|
|
groupId: number | string,
|
|
|
|
|
|
userId: string,
|
|
|
|
|
|
duration: number = 0
|
|
|
|
|
|
): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.post(`/groups/${encodeURIComponent(String(groupId))}/members/ban`, {
|
2026-03-09 21:29:03 +08:00
|
|
|
|
user_id: userId,
|
|
|
|
|
|
duration: duration,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 群设置 ====================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置全员禁言
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* PUT /api/v1/groups/:id/ban
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async setMuteAll(groupId: number | string, data: SetMuteAllRequest): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.put(`/groups/${encodeURIComponent(String(groupId))}/ban`, {
|
2026-03-09 21:29:03 +08:00
|
|
|
|
enable: data.mute_all,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置加群方式
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* PUT /api/v1/groups/:id/join-type
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async setJoinType(groupId: number | string, data: SetJoinTypeRequest): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.put(`/groups/${encodeURIComponent(String(groupId))}/join-type`, data);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置群名称
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* PUT /api/v1/groups/:id/name
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async setGroupName(groupId: number | string, groupName: string): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.put(`/groups/${encodeURIComponent(String(groupId))}/name`, {
|
2026-03-09 21:29:03 +08:00
|
|
|
|
group_name: groupName,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置群头像
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* PUT /api/v1/groups/:id/avatar
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async setGroupAvatar(groupId: number | string, avatarUrl: string): Promise<GroupResponse> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
const response = await api.put<GroupResponse>(
|
|
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/avatar`,
|
|
|
|
|
|
{
|
|
|
|
|
|
avatar: avatarUrl,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 群公告 ====================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建群公告
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* POST /api/v1/groups/:id/announcements
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async createAnnouncement(
|
|
|
|
|
|
groupId: number | string,
|
|
|
|
|
|
data: CreateAnnouncementRequest
|
|
|
|
|
|
): Promise<GroupAnnouncementResponse> {
|
|
|
|
|
|
const response = await api.post<GroupAnnouncementResponse>(
|
2026-03-10 20:52:50 +08:00
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/announcements`,
|
|
|
|
|
|
data
|
2026-03-09 21:29:03 +08:00
|
|
|
|
);
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取群公告列表
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* GET /api/v1/groups/:id/announcements
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async getAnnouncements(
|
|
|
|
|
|
groupId: number | string,
|
|
|
|
|
|
page = 1,
|
|
|
|
|
|
pageSize = 20
|
|
|
|
|
|
): Promise<GroupAnnouncementListResponse> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await api.get<GroupAnnouncementListResponse>(
|
2026-03-10 20:52:50 +08:00
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/announcements`,
|
2026-03-09 21:29:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
page,
|
|
|
|
|
|
page_size: pageSize,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取群公告列表失败:', error);
|
|
|
|
|
|
return {
|
|
|
|
|
|
list: [],
|
|
|
|
|
|
total: 0,
|
|
|
|
|
|
page: 1,
|
|
|
|
|
|
page_size: pageSize,
|
|
|
|
|
|
total_pages: 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 删除群公告
|
2026-03-10 20:52:50 +08:00
|
|
|
|
* DELETE /api/v1/groups/:id/announcements/:announcement_id
|
2026-03-09 21:29:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async deleteAnnouncement(groupId: number | string, announcementId: number): Promise<void> {
|
2026-03-10 20:52:50 +08:00
|
|
|
|
await api.delete(
|
|
|
|
|
|
`/groups/${encodeURIComponent(String(groupId))}/announcements/${encodeURIComponent(String(announcementId))}`
|
|
|
|
|
|
);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 兼容旧API的方法(将逐步废弃) ====================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @deprecated 使用 setGroupName 代替
|
|
|
|
|
|
*/
|
|
|
|
|
|
async updateGroup(id: number, data: { name: string }): Promise<GroupResponse> {
|
|
|
|
|
|
await this.setGroupName(id, data.name);
|
|
|
|
|
|
return this.getGroup(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 导出群组服务实例
|
|
|
|
|
|
export const groupService = new GroupService();
|
|
|
|
|
|
|
|
|
|
|
|
// 导出类型以方便使用
|
|
|
|
|
|
export type {
|
|
|
|
|
|
GroupResponse,
|
|
|
|
|
|
GroupMemberResponse,
|
|
|
|
|
|
GroupAnnouncementResponse,
|
|
|
|
|
|
GroupListResponse,
|
|
|
|
|
|
GroupMemberListResponse,
|
|
|
|
|
|
GroupAnnouncementListResponse,
|
|
|
|
|
|
CreateGroupRequest,
|
|
|
|
|
|
InviteMembersRequest,
|
|
|
|
|
|
TransferOwnerRequest,
|
|
|
|
|
|
SetRoleRequest,
|
|
|
|
|
|
SetNicknameRequest,
|
|
|
|
|
|
SetMuteAllRequest,
|
|
|
|
|
|
SetJoinTypeRequest,
|
|
|
|
|
|
CreateAnnouncementRequest,
|
|
|
|
|
|
SetGroupAvatarRequest,
|
|
|
|
|
|
};
|