chore(comment): 注释改为英文
This commit is contained in:
@@ -8,17 +8,16 @@ const App: React.FC = () => {
|
||||
const { setTheme } = useTheme();
|
||||
|
||||
useEffect(() => {
|
||||
// 在组件挂载时先处理URL中的authToken 和 groupId
|
||||
// Consume authToken & groupId from URL and persist
|
||||
handleAuthTokenAndGroupIdFromUrl();
|
||||
|
||||
// 读取并处理URL中的 theme('light'|'dark')参数
|
||||
// Read optional theme ('light' | 'dark') from URL and persist via ThemeProvider
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const themeParam = params.get('theme');
|
||||
if (themeParam === 'light' || themeParam === 'dark') {
|
||||
// 使用 ThemeProvider 的 setTheme 应用主题,并存入本地(storageKey: egret-ui-theme)
|
||||
setTheme(themeParam);
|
||||
|
||||
// 从URL中移除 theme 参数,保持地址整洁
|
||||
// Clean up URL param after applying
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete('theme');
|
||||
window.history.replaceState(null, '', url.toString());
|
||||
|
||||
@@ -5,8 +5,7 @@ interface RequestOptions extends RequestInit {
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一的fetch请求封装
|
||||
* 自动添加Authorization头部
|
||||
* Unified fetch wrapper that adds auth and group headers
|
||||
*/
|
||||
export const apiRequest = async <T = any>(
|
||||
endpoint: string,
|
||||
@@ -17,34 +16,34 @@ export const apiRequest = async <T = any>(
|
||||
|
||||
const url = endpoint;
|
||||
|
||||
// 准备请求头
|
||||
// Prepare request headers
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
// 如果有authToken,添加Authorization头
|
||||
// Attach Authorization header if token exists
|
||||
if (authToken) {
|
||||
headers.authorization = `Bearer ${authToken}`;
|
||||
}
|
||||
|
||||
// 如果有groupId,添加groupId头
|
||||
// Attach groupId header if present
|
||||
if (groupId) {
|
||||
headers.groupId = groupId;
|
||||
}
|
||||
|
||||
// 发起请求
|
||||
// Fire request
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
// 处理响应
|
||||
// Handle non-2xx
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
// 尝试解析JSON,如果失败则返回响应对象
|
||||
// Try to parse JSON; fallback to raw response
|
||||
try {
|
||||
return await response.json();
|
||||
} catch {
|
||||
@@ -52,7 +51,7 @@ export const apiRequest = async <T = any>(
|
||||
}
|
||||
};
|
||||
|
||||
// 便捷的HTTP方法封装
|
||||
// Convenience HTTP helpers
|
||||
export const api = {
|
||||
get: <T = any>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>> =>
|
||||
apiRequest<ApiResponse<T>>(endpoint, { ...options, method: 'GET' }),
|
||||
|
||||
@@ -12,7 +12,7 @@ export type UserInfo = {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的信息
|
||||
* Get current user information
|
||||
*/
|
||||
export const getUserInfo = async (): Promise<UserInfo | null> => {
|
||||
const res = await api.get<UserInfo>('/api/user/info');
|
||||
@@ -20,7 +20,7 @@ export const getUserInfo = async (): Promise<UserInfo | null> => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取群组内所有用户的信息
|
||||
* Get all users in the current group
|
||||
*/
|
||||
export const getGroupMembers = async (): Promise<UserInfo[]> => {
|
||||
const res = await api.get<UserInfo[]>('/api/group/members');
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
// 处理认证相关的工具函数
|
||||
// Auth utilities
|
||||
|
||||
const AUTH_TOKEN_KEY = 'authToken';
|
||||
const GROUP_ID_KEY = 'groupId';
|
||||
|
||||
/**
|
||||
* 从URL搜索参数中获取authToken
|
||||
* Get authToken from URL search params
|
||||
*/
|
||||
export const getAuthTokenFromUrl = (): string | null => {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
return urlParams.get('authToken');
|
||||
};
|
||||
/**
|
||||
* 从URL搜索参数中获取groupId
|
||||
* Get groupId from URL search params
|
||||
*/
|
||||
export const getGroupIdFromUrl = (): string | null => {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
@@ -19,21 +19,21 @@ export const getGroupIdFromUrl = (): string | null => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 保存authToken到localStorage
|
||||
* Save authToken to localStorage
|
||||
*/
|
||||
export const saveAuthToken = (token: string): void => {
|
||||
localStorage.setItem(AUTH_TOKEN_KEY, token);
|
||||
};
|
||||
|
||||
/**
|
||||
* 保存groupId到localStorage
|
||||
* Save groupId to localStorage
|
||||
*/
|
||||
export const saveGroupId = (groupId: string): void => {
|
||||
localStorage.setItem(GROUP_ID_KEY, groupId);
|
||||
};
|
||||
|
||||
/**
|
||||
* 从localStorage获取authToken
|
||||
* Get authToken with URL fallback
|
||||
*/
|
||||
export const getAuthToken = (): string | null => {
|
||||
const tokenFromUrl = getAuthTokenFromUrl();
|
||||
@@ -41,7 +41,7 @@ export const getAuthToken = (): string | null => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 从localStorage获取groupId
|
||||
* Get groupId with URL fallback
|
||||
*/
|
||||
export const getGroupId = (): string | null => {
|
||||
const groupIdFromUrl = getGroupIdFromUrl();
|
||||
@@ -49,22 +49,21 @@ export const getGroupId = (): string | null => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 清除authToken
|
||||
* Clear authToken
|
||||
*/
|
||||
export const clearAuthToken = (): void => {
|
||||
localStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
};
|
||||
|
||||
/**
|
||||
* 清除groupId
|
||||
* Clear groupId
|
||||
*/
|
||||
export const clearGroupId = (): void => {
|
||||
localStorage.removeItem(GROUP_ID_KEY);
|
||||
};
|
||||
|
||||
/**
|
||||
* 检查并处理URL中的authToken
|
||||
* 如果URL中有authToken参数,则保存到localStorage并从URL中移除
|
||||
* Consume authToken/groupId from URL and persist, then clean URL
|
||||
*/
|
||||
export const handleAuthTokenAndGroupIdFromUrl = (): void => {
|
||||
const tokenFromUrl = getAuthTokenFromUrl();
|
||||
@@ -80,7 +79,7 @@ export const handleAuthTokenAndGroupIdFromUrl = (): void => {
|
||||
updated = true;
|
||||
}
|
||||
|
||||
// Clean URL if we consumed any param
|
||||
// Clean URL if any param was consumed
|
||||
if (updated) {
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete('authToken');
|
||||
|
||||
Reference in New Issue
Block a user