feat: 增加群组用户列表获取功能

This commit is contained in:
dayjoy
2025-09-11 16:46:54 +08:00
parent c782dea322
commit 0d4a938c00
4 changed files with 103 additions and 26 deletions

View File

@@ -1,22 +1,25 @@
import { Request, Response, NextFunction } from "express";
import {Request, Response, NextFunction} from "express";
import axios from "axios";
export type UserInfo = {
userId: number;
nickname: string;
avatarUrl: string;
gender: 'MALE' |'FEMALE' | 'UNKNOWN';
gender: 'MALE' | 'FEMALE' | 'UNKNOWN';
nimToken: string; // NetEase Cloud Communication token
nimAccountId: string; // NetEase Cloud Communication account ID
createdAt: string;
updatedAt: string;
}
// Extend Express Request type to add user information
declare global {
namespace Express {
interface Request {
userInfo?: UserInfo;
userInfo?: UserInfo | null;
groupInfo?: {
groupId: number;
users: UserInfo[];
} | null;
}
}
}
@@ -90,8 +93,8 @@ export const userInfoMiddleware = async (
}
// Get user information
const userInfo = await getUserInfoByToken(token);
req.userInfo = userInfo;
const userInfoRes = await getUserInfoByToken(token);
req.userInfo = userInfoRes.code === 200 ? userInfoRes.data : null;
next();
} catch (error: any) {
@@ -102,11 +105,44 @@ export const userInfoMiddleware = async (
}
};
//////// group
/**
* Optional user information middleware
* If there is a token, get user information; if there is no token, the request will not be blocked
* Get group users
*/
export const optionalUserInfoMiddleware = async (
export const getGroupUsers = async (groupId: number, token: string): Promise<any> => {
try {
const response = await axios.post<UserInfo[]>(
"https://egret.byteawake.com/api/group/members",
{groupId},
{
headers: {
Authorization: `Bearer ${token}`,
},
timeout: 10000, // 10 second timeout
}
);
return response.data;
} catch (error: any) {
if (error.response) {
// API returned error response
throw new Error(`Failed to get user information: ${error.response.status} ${error.response.statusText}`);
} else if (error.request) {
// Request was sent but no response received
throw new Error("Failed to get user information: timeout or network error");
} else {
// Other errors
throw new Error(`Failed to get user information: ${error.message}`);
}
}
};
/**
* group information middleware
*/
export const groupInfoMiddleware = async (
req: Request,
res: Response,
next: NextFunction
@@ -114,21 +150,29 @@ export const optionalUserInfoMiddleware = async (
try {
const authHeader = req.headers.authorization;
const token = extractTokenFromHeader(authHeader);
const groupId = req.headers.groupid;
if (token) {
if (token && groupId) {
try {
const userInfo = await getUserInfoByToken(token);
req.userInfo = userInfo;
const usersRes = await getGroupUsers(Number(groupId), token);
const users: UserInfo[] = usersRes.code === 200 ? usersRes.data : [];
req.groupInfo = {
groupId: Number(groupId),
users,
}
} catch (error) {
// If getting user information fails, do not block the request from continuing, but do not set userInfo
console.warn("Failed to get user information:", error);
console.warn("Failed to get group user information:", error);
}
}
next();
} catch (error) {
// If the middleware itself encounters an error, do not block the request from continuing
console.error("User information middleware error:", error);
next();
} catch (error: any) {
res.status(400).json({
error: "Get Group Users failed",
message: error.message,
});
}
};