feat: 增加群组用户列表获取功能
This commit is contained in:
29
packages/client/src/api/user.ts
Normal file
29
packages/client/src/api/user.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import api from "@/api/index.ts";
|
||||
import {getGroupId} from "@/utils/auth.ts";
|
||||
|
||||
export type UserInfo = {
|
||||
userId: number;
|
||||
nickname: string;
|
||||
avatarUrl: string;
|
||||
gender: 'MALE' | 'FEMALE' | 'UNKNOWN';
|
||||
nimToken: string; // NetEase Cloud Communication token
|
||||
nimAccountId: string; // NetEase Cloud Communication account ID
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的信息
|
||||
*/
|
||||
export const getUserInfo = async (): Promise<UserInfo> => {
|
||||
return await api.post<UserInfo>('https://egret.byteawake.com/api/user/info');
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取群组内所有用户的信息
|
||||
*/
|
||||
export const getGroupUsers = async (): Promise<UserInfo[]> => {
|
||||
return await api.post<UserInfo[]>('https://egret.byteawake.com/api/group/members', {
|
||||
groupId: Number(getGroupId()),
|
||||
});
|
||||
};
|
||||
@@ -70,11 +70,5 @@ export const handleAuthTokenAndGroupIdFromUrl = (): void => {
|
||||
if (tokenFromUrl && groupIdFromUrl) {
|
||||
saveAuthToken(tokenFromUrl);
|
||||
saveGroupId(groupIdFromUrl);
|
||||
|
||||
// 从URL中移除authToken参数
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete('authToken');
|
||||
url.searchParams.delete('groupId');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import { Express } from "express";
|
||||
import {userInfoMiddleware} from "@/middleware/auth";
|
||||
import {userInfoMiddleware, groupInfoMiddleware} from "@/middleware/auth";
|
||||
|
||||
export const createApis = (app: Express) => {
|
||||
app.get("/api/test", userInfoMiddleware, async (req, res) => {
|
||||
console.log(req.userInfo);
|
||||
app.get("/api/test", async (req, res) => {
|
||||
res.status(200).json({
|
||||
message: "success",
|
||||
data: null,
|
||||
});
|
||||
});
|
||||
|
||||
// 同时使用用户信息和群组信息中间件
|
||||
app.get("/api/user-group/info", userInfoMiddleware, groupInfoMiddleware, async (req, res) => {
|
||||
res.status(200).json({
|
||||
message: "success",
|
||||
data: {
|
||||
user: req.userInfo,
|
||||
group: req.groupInfo
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/** add apis here */
|
||||
};
|
||||
|
||||
@@ -12,11 +12,14 @@ export type UserInfo = {
|
||||
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,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user