feat: 支持登录、个人信息存储
This commit is contained in:
@@ -1,8 +1,27 @@
|
||||
/**
|
||||
* HTTP 请求工具类
|
||||
* 封装 XMLHttpRequest,支持 GET/POST 请求
|
||||
* 封装 XMLHttpRequest,支持 GET/POST 请求,支持 JWT 认证
|
||||
*/
|
||||
export class HttpUtil {
|
||||
/** 认证 token */
|
||||
private static _authToken: string | null = null;
|
||||
|
||||
/**
|
||||
* 设置认证 token
|
||||
* @param token JWT token
|
||||
*/
|
||||
static setAuthToken(token: string | null): void {
|
||||
HttpUtil._authToken = token;
|
||||
console.log(`[HttpUtil] Auth token ${token ? '已设置' : '已清除'}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取认证 token
|
||||
*/
|
||||
static getAuthToken(): string | null {
|
||||
return HttpUtil._authToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 GET 请求
|
||||
* @param url 请求 URL
|
||||
@@ -17,6 +36,11 @@ export class HttpUtil {
|
||||
xhr.timeout = timeout;
|
||||
xhr.responseType = 'json';
|
||||
|
||||
// 设置认证头
|
||||
if (HttpUtil._authToken) {
|
||||
xhr.setRequestHeader('Authorization', `Bearer ${HttpUtil._authToken}`);
|
||||
}
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(xhr.response as T);
|
||||
@@ -53,6 +77,11 @@ export class HttpUtil {
|
||||
xhr.responseType = 'json';
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
// 设置认证头
|
||||
if (HttpUtil._authToken) {
|
||||
xhr.setRequestHeader('Authorization', `Bearer ${HttpUtil._authToken}`);
|
||||
}
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(xhr.response as T);
|
||||
|
||||
Reference in New Issue
Block a user