const http = require('http');
|
|
// 使用之前获取的有效token
|
const validToken = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiItODMzNDg4IiwicGhvbmUiOiJvZ3h4QTEtS3JTVlRkcUk5VDF1YUIxQlF3UEdVIiwiaWF0IjoxNzU5ODM5NDQ1LCJleHAiOjE3NTk5MjU4NDV9.Xoq2S-zQzI3GMWxaSS2A5GGlPsR3z2BRkzg4HK3tHhE';
|
|
// 使用有效token测试saveUserInfo
|
function testSaveUserInfo(token) {
|
console.log('=== 使用有效token测试saveUserInfo ===');
|
|
const mutation = `
|
mutation SaveUserInfo($input: UserInput!) {
|
saveUserInfo(input: $input) {
|
id
|
name
|
phone
|
gender
|
birthday
|
wxOpenId
|
unionId
|
}
|
}
|
`;
|
|
const variables = {
|
input: {
|
name: "测试用户JWT修复",
|
phone: "13981970816",
|
gender: "MALE",
|
birthday: "2025-10-07"
|
}
|
};
|
|
const postData = JSON.stringify({
|
query: mutation,
|
variables: variables
|
});
|
|
const options = {
|
hostname: 'localhost',
|
port: 8080,
|
path: '/api/graphql',
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
'Content-Length': Buffer.byteLength(postData),
|
'Authorization': `Bearer ${token}`
|
}
|
};
|
|
console.log('发送GraphQL请求,携带有效token...');
|
console.log('Token:', token.substring(0, 30) + '...');
|
|
const req = http.request(options, (res) => {
|
console.log('响应状态码:', res.statusCode);
|
|
let data = '';
|
res.on('data', (chunk) => {
|
data += chunk;
|
});
|
|
res.on('end', () => {
|
console.log('响应内容:', data);
|
try {
|
const response = JSON.parse(data);
|
if (response.errors) {
|
console.log('❌ GraphQL错误:', response.errors);
|
response.errors.forEach((error, index) => {
|
console.log(`错误 ${index + 1}:`, error.message);
|
if (error.extensions) {
|
console.log('错误详情:', error.extensions);
|
}
|
});
|
} else if (response.data && response.data.saveUserInfo) {
|
console.log('✅ 保存用户信息成功!');
|
console.log('用户信息:', response.data.saveUserInfo);
|
} else {
|
console.log('⚠️ 未知响应格式:', response);
|
}
|
} catch (e) {
|
console.log('❌ 解析响应失败:', e.message);
|
console.log('原始响应:', data);
|
}
|
});
|
});
|
|
req.on('error', (e) => {
|
console.error('❌ 请求错误:', e.message);
|
});
|
|
req.write(postData);
|
req.end();
|
}
|
|
// 检查token是否过期
|
function checkTokenExpiry(token) {
|
try {
|
const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
|
const now = Math.floor(Date.now() / 1000);
|
const exp = payload.exp;
|
|
console.log('Token信息:');
|
console.log('- 用户ID:', payload.sub);
|
console.log('- 签发时间:', new Date(payload.iat * 1000).toLocaleString());
|
console.log('- 过期时间:', new Date(exp * 1000).toLocaleString());
|
console.log('- 当前时间:', new Date(now * 1000).toLocaleString());
|
console.log('- 是否过期:', now > exp ? '是' : '否');
|
console.log('- 剩余时间:', Math.max(0, exp - now), '秒');
|
|
return now <= exp;
|
} catch (e) {
|
console.log('❌ Token解析失败:', e.message);
|
return false;
|
}
|
}
|
|
// 开始测试
|
console.log('=== JWT Token 验证 ===');
|
if (checkTokenExpiry(validToken)) {
|
console.log('\n✅ Token有效,开始测试saveUserInfo...\n');
|
testSaveUserInfo(validToken);
|
} else {
|
console.log('\n❌ Token已过期,请重新获取token');
|
}
|