feat(client): 添加对groupId的处理

This commit is contained in:
dayjoy
2025-09-11 10:59:02 +08:00
parent 01294f58b1
commit 80ee3ca5a5
3 changed files with 44 additions and 6 deletions

View File

@@ -1,12 +1,12 @@
import "./App.css";
import React, { useEffect } from "react";
import { Routes } from "react-router-dom";
import { handleAuthTokenFromUrl } from "./utils/auth";
import {handleAuthTokenAndGroupIdFromUrl} from "./utils/auth";
const App: React.FC = () => {
useEffect(() => {
// 在组件挂载时处理URL中的authToken
handleAuthTokenFromUrl();
// 在组件挂载时处理URL中的authToken 和 groupId
handleAuthTokenAndGroupIdFromUrl();
}, []);
return (

View File

@@ -1,4 +1,4 @@
import { getAuthToken } from '../utils/auth';
import {getAuthToken, getGroupId} from '../utils/auth';
interface RequestOptions extends RequestInit {
headers?: Record<string, string>;
@@ -13,6 +13,7 @@ export const apiRequest = async <T = any>(
options: RequestOptions = {}
): Promise<T> => {
const authToken = getAuthToken();
const groupId = getGroupId();
const url = endpoint;
@@ -27,6 +28,11 @@ export const apiRequest = async <T = any>(
headers.authorization = `Bearer ${authToken}`;
}
// 如果有groupId添加groupId头
if (groupId) {
headers.groupId = groupId;
}
// 发起请求
const response = await fetch(url, {
...options,

View File

@@ -1,6 +1,7 @@
// 处理认证相关的工具函数
const AUTH_TOKEN_KEY = 'authToken';
const GROUP_ID_KEY = 'groupId';
/**
* 从URL搜索参数中获取authToken
@@ -9,6 +10,13 @@ export const getAuthTokenFromUrl = (): string | null => {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('authToken');
};
/**
* 从URL搜索参数中获取groupId
*/
export const getGroupIdFromUrl = (): string | null => {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('groupId');
};
/**
* 保存authToken到localStorage
@@ -17,6 +25,13 @@ export const saveAuthToken = (token: string): void => {
localStorage.setItem(AUTH_TOKEN_KEY, token);
};
/**
* 保存groupId到localStorage
*/
export const saveGroupId = (groupId: string): void => {
localStorage.setItem(GROUP_ID_KEY, groupId);
};
/**
* 从localStorage获取authToken
*/
@@ -24,6 +39,13 @@ export const getAuthToken = (): string | null => {
return localStorage.getItem(AUTH_TOKEN_KEY);
};
/**
* 从localStorage获取groupId
*/
export const getGroupId = (): string | null => {
return localStorage.getItem(GROUP_ID_KEY);
};
/**
* 清除authToken
*/
@@ -31,18 +53,28 @@ export const clearAuthToken = (): void => {
localStorage.removeItem(AUTH_TOKEN_KEY);
};
/**
* 清除groupId
*/
export const clearGroupId = (): void => {
localStorage.removeItem(GROUP_ID_KEY);
};
/**
* 检查并处理URL中的authToken
* 如果URL中有authToken参数则保存到localStorage并从URL中移除
*/
export const handleAuthTokenFromUrl = (): void => {
export const handleAuthTokenAndGroupIdFromUrl = (): void => {
const tokenFromUrl = getAuthTokenFromUrl();
if (tokenFromUrl) {
const groupIdFromUrl = getGroupIdFromUrl();
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());
}
};