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) {
|
if (tokenFromUrl && groupIdFromUrl) {
|
||||||
saveAuthToken(tokenFromUrl);
|
saveAuthToken(tokenFromUrl);
|
||||||
saveGroupId(groupIdFromUrl);
|
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 { Express } from "express";
|
||||||
import {userInfoMiddleware} from "@/middleware/auth";
|
import {userInfoMiddleware, groupInfoMiddleware} from "@/middleware/auth";
|
||||||
|
|
||||||
export const createApis = (app: Express) => {
|
export const createApis = (app: Express) => {
|
||||||
app.get("/api/test", userInfoMiddleware, async (req, res) => {
|
app.get("/api/test", async (req, res) => {
|
||||||
console.log(req.userInfo);
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: "success",
|
message: "success",
|
||||||
data: null,
|
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 */
|
/** add apis here */
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,11 +12,14 @@ export type UserInfo = {
|
|||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extend Express Request type to add user information
|
|
||||||
declare global {
|
declare global {
|
||||||
namespace Express {
|
namespace Express {
|
||||||
interface Request {
|
interface Request {
|
||||||
userInfo?: UserInfo;
|
userInfo?: UserInfo | null;
|
||||||
|
groupInfo?: {
|
||||||
|
groupId: number;
|
||||||
|
users: UserInfo[];
|
||||||
|
} | null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,8 +93,8 @@ export const userInfoMiddleware = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get user information
|
// Get user information
|
||||||
const userInfo = await getUserInfoByToken(token);
|
const userInfoRes = await getUserInfoByToken(token);
|
||||||
req.userInfo = userInfo;
|
req.userInfo = userInfoRes.code === 200 ? userInfoRes.data : null;
|
||||||
|
|
||||||
next();
|
next();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -102,11 +105,44 @@ export const userInfoMiddleware = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//////// group
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional user information middleware
|
* Get group users
|
||||||
* If there is a token, get user information; if there is no token, the request will not be blocked
|
|
||||||
*/
|
*/
|
||||||
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,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
@@ -114,21 +150,29 @@ export const optionalUserInfoMiddleware = async (
|
|||||||
try {
|
try {
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = req.headers.authorization;
|
||||||
const token = extractTokenFromHeader(authHeader);
|
const token = extractTokenFromHeader(authHeader);
|
||||||
|
const groupId = req.headers.groupid;
|
||||||
|
|
||||||
if (token) {
|
if (token && groupId) {
|
||||||
try {
|
try {
|
||||||
const userInfo = await getUserInfoByToken(token);
|
const usersRes = await getGroupUsers(Number(groupId), token);
|
||||||
req.userInfo = userInfo;
|
const users: UserInfo[] = usersRes.code === 200 ? usersRes.data : [];
|
||||||
|
req.groupInfo = {
|
||||||
|
groupId: Number(groupId),
|
||||||
|
users,
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// If getting user information fails, do not block the request from continuing, but do not set userInfo
|
// 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();
|
next();
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
// If the middleware itself encounters an error, do not block the request from continuing
|
res.status(400).json({
|
||||||
console.error("User information middleware error:", error);
|
error: "Get Group Users failed",
|
||||||
next();
|
message: error.message,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user