feat:添加日志
This commit is contained in:
@ -57,42 +57,70 @@ async function request<T>(path: string, options: RequestOptions): Promise<T> {
|
|||||||
timeout: options.timeout || timeout
|
timeout: options.timeout || timeout
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log('[HTTP] 请求详情:', {
|
||||||
|
url: url.toString(),
|
||||||
|
method: options.method,
|
||||||
|
headers: requestOptions.headers,
|
||||||
|
hasToken: !!token,
|
||||||
|
body: options.body
|
||||||
|
});
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = httpModule.request(requestOptions, (res) => {
|
const req = httpModule.request(requestOptions, (res) => {
|
||||||
let data = '';
|
let data = '';
|
||||||
|
|
||||||
|
console.log('[HTTP] 响应状态码:', res.statusCode);
|
||||||
|
console.log('[HTTP] 响应头:', res.headers);
|
||||||
|
|
||||||
res.on('data', (chunk) => {
|
res.on('data', (chunk) => {
|
||||||
data += chunk;
|
data += chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
res.on('end', () => {
|
res.on('end', () => {
|
||||||
|
console.log('[HTTP] 响应体:', data);
|
||||||
try {
|
try {
|
||||||
const json = JSON.parse(data);
|
const json = JSON.parse(data);
|
||||||
|
console.log('[HTTP] 解析后的响应:', JSON.stringify(json, null, 2));
|
||||||
|
|
||||||
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
|
console.log('[HTTP] 请求成功');
|
||||||
resolve(json as T);
|
resolve(json as T);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(json.error || json.message || `HTTP ${res.statusCode}`));
|
console.error('[HTTP] 请求失败:', {
|
||||||
|
statusCode: res.statusCode,
|
||||||
|
error: json.error,
|
||||||
|
message: json.message,
|
||||||
|
msg: json.msg
|
||||||
|
});
|
||||||
|
reject(new Error(json.error || json.message || json.msg || `HTTP ${res.statusCode}`));
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error('[HTTP] 解析响应失败:', e);
|
||||||
|
console.error('[HTTP] 原始响应:', data);
|
||||||
reject(new Error(`解析响应失败: ${data}`));
|
reject(new Error(`解析响应失败: ${data}`));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', (error) => {
|
req.on('error', (error) => {
|
||||||
|
console.error('[HTTP] 请求错误:', error);
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('timeout', () => {
|
req.on('timeout', () => {
|
||||||
|
console.error('[HTTP] 请求超时');
|
||||||
req.destroy();
|
req.destroy();
|
||||||
reject(new Error('请求超时'));
|
reject(new Error('请求超时'));
|
||||||
});
|
});
|
||||||
|
|
||||||
if (options.body) {
|
if (options.body) {
|
||||||
req.write(JSON.stringify(options.body));
|
const bodyStr = JSON.stringify(options.body);
|
||||||
|
console.log('[HTTP] 发送请求体:', bodyStr);
|
||||||
|
req.write(bodyStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
req.end();
|
req.end();
|
||||||
|
console.log('[HTTP] 请求已发送');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,12 +294,22 @@ export async function getCreditBalance(userId: string): Promise<CreditBalanceRes
|
|||||||
* POST /api/invitation/verify
|
* POST /api/invitation/verify
|
||||||
*/
|
*/
|
||||||
export async function verifyInvitationCode(code: string): Promise<InvitationVerifyResponse> {
|
export async function verifyInvitationCode(code: string): Promise<InvitationVerifyResponse> {
|
||||||
console.log('[API] 验证邀请码');
|
console.log('[API] 验证邀请码 - 开始');
|
||||||
|
console.log('[API] 邀请码:', code);
|
||||||
const body: InvitationVerifyRequest = { code };
|
const body: InvitationVerifyRequest = { code };
|
||||||
return request<InvitationVerifyResponse>('/api/invitation/verify', {
|
console.log('[API] 请求体:', JSON.stringify(body));
|
||||||
method: 'POST',
|
|
||||||
body
|
try {
|
||||||
});
|
const response = await request<InvitationVerifyResponse>('/api/invitation/verify', {
|
||||||
|
method: 'POST',
|
||||||
|
body
|
||||||
|
});
|
||||||
|
console.log('[API] 验证邀请码 - 响应:', JSON.stringify(response));
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[API] 验证邀请码 - 错误:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -22,22 +22,35 @@ export class InvitationService {
|
|||||||
*/
|
*/
|
||||||
static async verifyCode(code: string): Promise<{ success: boolean; message: string }> {
|
static async verifyCode(code: string): Promise<{ success: boolean; message: string }> {
|
||||||
try {
|
try {
|
||||||
console.log('[InvitationService] 验证邀请码:', code);
|
console.log('[InvitationService] ========== 开始验证邀请码 ==========');
|
||||||
|
console.log('[InvitationService] 邀请码:', code);
|
||||||
|
console.log('[InvitationService] 邀请码长度:', code.length);
|
||||||
|
|
||||||
const response = await verifyInvitationCode(code);
|
const response = await verifyInvitationCode(code);
|
||||||
|
|
||||||
|
console.log('[InvitationService] 收到响应:', JSON.stringify(response, null, 2));
|
||||||
|
console.log('[InvitationService] 响应代码:', response.code);
|
||||||
|
console.log('[InvitationService] 响应消息:', response.msg);
|
||||||
|
console.log('[InvitationService] 验证结果:', response.data?.verified);
|
||||||
|
|
||||||
if (response.code === 200 && response.data?.verified) {
|
if (response.code === 200 && response.data?.verified) {
|
||||||
|
console.log('[InvitationService] ✓ 验证成功');
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: response.msg || '验证成功'
|
message: response.msg || '验证成功'
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
console.log('[InvitationService] ✗ 验证失败');
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: response.msg || '验证失败'
|
message: response.msg || '验证失败'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[InvitationService] 验证邀请码失败:', error);
|
console.error('[InvitationService] ========== 验证邀请码异常 ==========');
|
||||||
|
console.error('[InvitationService] 错误类型:', error.constructor.name);
|
||||||
|
console.error('[InvitationService] 错误消息:', error.message);
|
||||||
|
console.error('[InvitationService] 错误堆栈:', error.stack);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: error.message || '网络连接失败,请检查网络后重试'
|
message: error.message || '网络连接失败,请检查网络后重试'
|
||||||
|
|||||||
Reference in New Issue
Block a user