68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import {Controller, Get, Route, Response, Tags, Request, Security} from 'tsoa';
|
|
import {ApiError, ApiResponse} from '../types/api';
|
|
import type {Request as ExpressRequest} from 'express';
|
|
import axios from "axios";
|
|
import type {UserInfo} from "../types/user";
|
|
|
|
export const getGroupUsers = async (groupId: number, token: string): Promise<UserInfo[]> => {
|
|
try {
|
|
const response = await axios.post<ApiResponse<UserInfo[]>>(
|
|
"https://egret.byteawake.com/api/group/members",
|
|
{groupId},
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
timeout: 10000, // 10 second timeout
|
|
}
|
|
);
|
|
|
|
if (response.data.code !== 200) {
|
|
throw new Error(`Failed to get group users: ${response.data.message}`);
|
|
}
|
|
|
|
return response.data.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}`);
|
|
}
|
|
}
|
|
};
|
|
|
|
@Route('api/group')
|
|
@Tags('Chat Group')
|
|
export class GroupController extends Controller {
|
|
/**
|
|
* 获取当前聊天群组的全部用户信息
|
|
* @summary 获取当前聊天群组的全部用户信息
|
|
* @description 获取当前聊天群组的全部用户信息
|
|
*/
|
|
@Get('/members')
|
|
@Security("jwt")
|
|
@Response<ApiResponse<UserInfo[]>>(200, 'Success')
|
|
@Response<ApiResponse<null>>(400, 'Bad Request')
|
|
@Response<ApiResponse<null>>(401, 'Unauthorized')
|
|
public async getGroupUsers(@Request() req: ExpressRequest): Promise<ApiResponse<UserInfo[]>> {
|
|
const user = req.user;
|
|
const groupId = req.headers.groupid;
|
|
|
|
if (!groupId) {
|
|
throw new ApiError(400, 'groupId is required in headers');
|
|
}
|
|
|
|
const users = await getGroupUsers(Number(groupId), user.token);
|
|
|
|
return {
|
|
code: 200,
|
|
message: 'success',
|
|
data: users,
|
|
};
|
|
}
|
|
} |