feat: 1. 将大部分的请求从fetch改成axios方便管理。
2. 给文档类型添加入口模块和相关数据的渲染。并且给文档类型进行功能上的角色权限区分 3. 新增角色权限管理页面
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
* 2. 如果需要新的网络请求,在 `OAuthClient` 中添加
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
|
||||
interface OAuthConfig {
|
||||
serverUrl: string;
|
||||
clientId: string;
|
||||
@@ -114,46 +116,38 @@ export class OAuthClient {
|
||||
});
|
||||
|
||||
try {
|
||||
// 创建 AbortController 用于超时控制
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 15000); // 15秒超时
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
const response = await axios.post(url, data, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: data,
|
||||
signal: controller.signal
|
||||
timeout: 60000 // 60秒超时
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
console.log('🔧 Token响应状态:', response.status, response.statusText);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.error('❌ 获取访问令牌失败:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
errorData: errorData
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
const tokenResponse = await response.json() as TokenResponse;
|
||||
const tokenResponse = response.data as TokenResponse;
|
||||
console.log('✅ 获取访问令牌成功:', {
|
||||
token_type: tokenResponse.token_type,
|
||||
expires_in: tokenResponse.expires_in,
|
||||
scope: tokenResponse.scope
|
||||
});
|
||||
|
||||
|
||||
return tokenResponse;
|
||||
} catch (error) {
|
||||
// 判断是否为超时错误
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
console.error('❌ 获取访问令牌超时(15秒):', error.message);
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (error.code === 'ECONNABORTED') {
|
||||
console.error('❌ 获取访问令牌超时(60秒):', error.message);
|
||||
} else if (error.response) {
|
||||
console.error('❌ 获取访问令牌失败:', {
|
||||
status: error.response.status,
|
||||
statusText: error.response.statusText,
|
||||
errorData: error.response.data
|
||||
});
|
||||
} else {
|
||||
console.error('❌ 获取访问令牌网络错误:', error.message);
|
||||
}
|
||||
} else {
|
||||
console.error('❌ 获取访问令牌网络错误:', error);
|
||||
console.error('❌ 获取访问令牌错误:', error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -168,31 +162,25 @@ export class OAuthClient {
|
||||
const url = `${this.config.serverUrl}/api/bff/v1.2/oauth2/userinfo`;
|
||||
|
||||
try {
|
||||
// 创建 AbortController 用于超时控制
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 15000); // 15秒超时
|
||||
|
||||
const response = await fetch(url, {
|
||||
const response = await axios.get(url, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`
|
||||
},
|
||||
signal: controller.signal
|
||||
timeout: 60000 // 60秒超时
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('获取用户信息失败:', response.status, response.statusText);
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.json() as UserInfoResponse;
|
||||
return response.data as UserInfoResponse;
|
||||
} catch (error) {
|
||||
// 判断是否为超时错误
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
console.error('❌ 获取用户信息超时(15秒):', error.message);
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (error.code === 'ECONNABORTED') {
|
||||
console.error('❌ 获取用户信息超时(60秒):', error.message);
|
||||
} else if (error.response) {
|
||||
console.error('获取用户信息失败:', error.response.status, error.response.statusText);
|
||||
} else {
|
||||
console.error('❌ 获取用户信息网络错误:', error.message);
|
||||
}
|
||||
} else {
|
||||
console.error('❌ 获取用户信息网络错误:', error);
|
||||
console.error('❌ 获取用户信息错误:', error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -219,34 +207,25 @@ export class OAuthClient {
|
||||
});
|
||||
|
||||
try {
|
||||
// 创建 AbortController 用于超时控制
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 15000); // 15秒超时
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
const response = await axios.post(url, data, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: data,
|
||||
signal: controller.signal
|
||||
timeout: 60000 // 60秒超时
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.error('刷新访问令牌失败:', errorData);
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.json() as TokenResponse;
|
||||
return response.data as TokenResponse;
|
||||
} catch (error) {
|
||||
// 判断是否为超时错误
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
console.error('❌ 刷新访问令牌超时(15秒):', error.message);
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (error.code === 'ECONNABORTED') {
|
||||
console.error('❌ 刷新访问令牌超时(60秒):', error.message);
|
||||
} else if (error.response) {
|
||||
console.error('刷新访问令牌失败:', error.response.data);
|
||||
} else {
|
||||
console.error('❌ 刷新访问令牌网络错误:', error.message);
|
||||
}
|
||||
} else {
|
||||
console.error('❌ 刷新访问令牌网络错误:', error);
|
||||
console.error('❌ 刷新访问令牌错误:', error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -266,25 +245,21 @@ export class OAuthClient {
|
||||
});
|
||||
|
||||
try {
|
||||
// 创建 AbortController 用于超时控制
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 15000); // 15秒超时
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
const response = await axios.post(url, data, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: data,
|
||||
signal: controller.signal
|
||||
timeout: 60000 // 60秒超时
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
return response.ok;
|
||||
return response.status >= 200 && response.status < 300;
|
||||
} catch (error) {
|
||||
// 判断是否为超时错误
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
console.error('❌ 登出超时(15秒):', error.message);
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (error.code === 'ECONNABORTED') {
|
||||
console.error('❌ 登出超时(60秒):', error.message);
|
||||
} else {
|
||||
console.error('❌ 登出失败:', error);
|
||||
}
|
||||
} else {
|
||||
console.error('❌ 登出失败:', error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user