Compare commits
9 Commits
8c60050ff1
...
eb39f41f0c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb39f41f0c | ||
|
|
a4ccdada1c | ||
|
|
d8a921b50a | ||
|
|
a5048bafb3 | ||
|
|
999a8e2520 | ||
|
|
b78a07f192 | ||
|
|
5688bb4242 | ||
|
|
b1530ce06e | ||
|
|
f53aed1b82 |
@@ -6,9 +6,9 @@
|
||||
],
|
||||
"scripts": {
|
||||
"start": "yarn start:all",
|
||||
"start:all": "concurrently -n 'client,server' 'yarn workspace egret-app-client dev' 'yarn workspace egret-app-server start'",
|
||||
"start:all": "concurrently -n 'client,server' 'yarn workspace egret-app-client dev' 'yarn workspace egret-app-server serve'",
|
||||
"start:client": "yarn workspace egret-app-client dev",
|
||||
"start:server": "yarn workspace egret-app-server start",
|
||||
"start:server": "yarn workspace egret-app-server serve",
|
||||
"build": "lerna run build && node ./scripts/post-build.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -54,32 +54,38 @@ export const apiRequest = async <T = any>(
|
||||
|
||||
// 便捷的HTTP方法封装
|
||||
export const api = {
|
||||
get: <T = any>(endpoint: string, options?: RequestOptions): Promise<T> =>
|
||||
apiRequest<T>(endpoint, { ...options, method: 'GET' }),
|
||||
get: <T = any>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>> =>
|
||||
apiRequest<ApiResponse<T>>(endpoint, { ...options, method: 'GET' }),
|
||||
|
||||
post: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> =>
|
||||
apiRequest<T>(endpoint, {
|
||||
post: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
|
||||
apiRequest<ApiResponse<T>>(endpoint, {
|
||||
...options,
|
||||
method: 'POST',
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
}),
|
||||
|
||||
put: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> =>
|
||||
apiRequest<T>(endpoint, {
|
||||
put: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
|
||||
apiRequest<ApiResponse<T>>(endpoint, {
|
||||
...options,
|
||||
method: 'PUT',
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
}),
|
||||
|
||||
patch: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> =>
|
||||
apiRequest<T>(endpoint, {
|
||||
patch: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
|
||||
apiRequest<ApiResponse<T>>(endpoint, {
|
||||
...options,
|
||||
method: 'PATCH',
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
}),
|
||||
|
||||
delete: <T = any>(endpoint: string, options?: RequestOptions): Promise<T> =>
|
||||
apiRequest<T>(endpoint, { ...options, method: 'DELETE' }),
|
||||
delete: <T = any>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>> =>
|
||||
apiRequest<ApiResponse<T>>(endpoint, { ...options, method: 'DELETE' }),
|
||||
};
|
||||
|
||||
export type ApiResponse<T> = {
|
||||
code: number;
|
||||
message: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
export default api;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import api from "@/api/index.ts";
|
||||
import {getGroupId} from "@/utils/auth.ts";
|
||||
|
||||
export type UserInfo = {
|
||||
userId: number;
|
||||
@@ -15,15 +14,15 @@ export type UserInfo = {
|
||||
/**
|
||||
* 获取当前用户的信息
|
||||
*/
|
||||
export const getUserInfo = async (): Promise<UserInfo> => {
|
||||
return await api.post<UserInfo>('https://egret.byteawake.com/api/user/info');
|
||||
export const getUserInfo = async (): Promise<UserInfo | null> => {
|
||||
const res = await api.get<UserInfo>('/api/user/info');
|
||||
return res.code === 200 ? res.data : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取群组内所有用户的信息
|
||||
*/
|
||||
export const getGroupUsers = async (): Promise<UserInfo[]> => {
|
||||
return await api.post<UserInfo[]>('https://egret.byteawake.com/api/group/members', {
|
||||
groupId: Number(getGroupId()),
|
||||
});
|
||||
const res = await api.get<UserInfo[]>('/api/group/members');
|
||||
return res.code === 200 ? res.data || [] : [];
|
||||
};
|
||||
|
||||
@@ -36,14 +36,16 @@ export const saveGroupId = (groupId: string): void => {
|
||||
* 从localStorage获取authToken
|
||||
*/
|
||||
export const getAuthToken = (): string | null => {
|
||||
return localStorage.getItem(AUTH_TOKEN_KEY);
|
||||
const tokenFromUrl = getAuthTokenFromUrl();
|
||||
return tokenFromUrl || localStorage.getItem(AUTH_TOKEN_KEY);
|
||||
};
|
||||
|
||||
/**
|
||||
* 从localStorage获取groupId
|
||||
*/
|
||||
export const getGroupId = (): string | null => {
|
||||
return localStorage.getItem(GROUP_ID_KEY);
|
||||
const groupIdFromUrl = getGroupIdFromUrl();
|
||||
return groupIdFromUrl || localStorage.getItem(GROUP_ID_KEY);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "tsoa -c tsoaConfig.json spec-and-routes && tsc && tsc-alias",
|
||||
"serve": "node ./build/index.js",
|
||||
"serve": "yarn build && node ./build/index.js",
|
||||
"start": "nodemon"
|
||||
},
|
||||
"keywords": [],
|
||||
|
||||
@@ -4,7 +4,7 @@ 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[]> => {
|
||||
export const getGroupMembers = async (groupId: number, token: string): Promise<UserInfo[]> => {
|
||||
try {
|
||||
const response = await axios.post<ApiResponse<UserInfo[]>>(
|
||||
"https://egret.byteawake.com/api/group/members",
|
||||
@@ -18,20 +18,20 @@ export const getGroupUsers = async (groupId: number, token: string): Promise<Use
|
||||
);
|
||||
|
||||
if (response.data.code !== 200) {
|
||||
throw new Error(`Failed to get group users: ${response.data.message}`);
|
||||
throw new Error(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}`);
|
||||
throw new ApiError(400, `Failed to get group members 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");
|
||||
throw new ApiError(400, "Failed to get group members information: timeout or network error");
|
||||
} else {
|
||||
// Other errors
|
||||
throw new Error(`Failed to get user information: ${error.message}`);
|
||||
throw new ApiError(400, `Failed to get group members information: ${error.message}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -44,12 +44,12 @@ export class GroupController extends Controller {
|
||||
* @summary 获取当前聊天群组的全部用户信息
|
||||
* @description 获取当前聊天群组的全部用户信息
|
||||
*/
|
||||
@Get('/users')
|
||||
@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[]>> {
|
||||
public async getGroupMembers(@Request() req: ExpressRequest): Promise<ApiResponse<UserInfo[]>> {
|
||||
const user = req.user;
|
||||
const groupId = req.headers.groupid;
|
||||
|
||||
@@ -57,7 +57,7 @@ export class GroupController extends Controller {
|
||||
throw new ApiError(400, 'groupId is required in headers');
|
||||
}
|
||||
|
||||
const users = await getGroupUsers(Number(groupId), user.token);
|
||||
const users = await getGroupMembers(Number(groupId), user.token);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
|
||||
@@ -16,11 +16,12 @@ export class UserController extends Controller {
|
||||
@Security('jwt')
|
||||
@Response<ApiResponse<UserInfo>>(200, 'Success')
|
||||
@Response(401, 'Unauthorized')
|
||||
public async getUserGroupInfo(@Request() req: ExpressRequest): Promise<ApiResponse<UserInfo>> {
|
||||
public async getUserGroupInfo(@Request() req: ExpressRequest): Promise<ApiResponse<Omit<UserInfo, 'token'>>> {
|
||||
const { token, ...rest } = req.user;
|
||||
return {
|
||||
code: 200,
|
||||
message: 'success',
|
||||
data: req.user
|
||||
data: rest
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -55,20 +55,20 @@ export const getUserInfoByToken = async (token: string): Promise<UserInfo> => {
|
||||
);
|
||||
|
||||
if (response.data.code !== 200 || !response.data.data) {
|
||||
throw new Error(`Failed to get user information: ${response.data.message}`);
|
||||
throw new Error(response.data.message);
|
||||
}
|
||||
|
||||
return {...response.data.data, token};
|
||||
} catch (error: any) {
|
||||
if (error.response) {
|
||||
// API returned error response
|
||||
throw new Error(`Failed to get user information: ${error.response.status} ${error.response.statusText}`);
|
||||
throw new ApiError(401, `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");
|
||||
throw new ApiError(401, "Failed to get user information: timeout or network error");
|
||||
} else {
|
||||
// Other errors
|
||||
throw new Error(`Failed to get user information: ${error.message}`);
|
||||
throw new ApiError(401, `Failed to get user information: ${error.message}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user