const http = require('http');
|
|
// 模拟微信登录获取有效token
|
function wxLogin() {
|
const postData = JSON.stringify({
|
code: '0d3mWR000zzp6V1526100pOK7a2mWR0W', // 替换为实际的微信code
|
encryptedData: '',
|
iv: ''
|
});
|
|
const options = {
|
hostname: 'localhost',
|
port: 8080,
|
path: '/api/auth/wx-login',
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
'Content-Length': Buffer.byteLength(postData)
|
}
|
};
|
|
console.log('发送微信登录请求...');
|
|
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.token) {
|
console.log('\n获取到token:', response.token);
|
console.log('用户信息:', response.userInfo);
|
|
// 使用获取到的token测试saveUserInfo
|
setTimeout(() => {
|
testSaveUserInfo(response.token);
|
}, 1000);
|
} else {
|
console.log('登录失败:', response);
|
}
|
} catch (e) {
|
console.log('解析响应失败:', e.message);
|
}
|
});
|
});
|
|
req.on('error', (e) => {
|
console.error('请求错误:', e.message);
|
});
|
|
req.write(postData);
|
req.end();
|
}
|
|
// 使用有效token测试saveUserInfo
|
function testSaveUserInfo(token) {
|
console.log('\n=== 使用有效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...');
|
|
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);
|
} else {
|
console.log('保存用户信息成功:', response.data);
|
}
|
} catch (e) {
|
console.log('解析响应失败:', e.message);
|
}
|
});
|
});
|
|
req.on('error', (e) => {
|
console.error('请求错误:', e.message);
|
});
|
|
req.write(postData);
|
req.end();
|
}
|
|
// 开始测试
|
wxLogin();
|