const axios = require('axios');
|
|
const BASE_URL = 'http://localhost:8080';
|
|
async function testSaveUserInfoFixed() {
|
console.log('=== 测试修复后的saveUserInfo功能 ===\n');
|
|
try {
|
// 1. 先进行微信登录获取匿名用户token
|
console.log('1. 进行微信登录获取匿名用户token');
|
const loginResponse = await axios.post(`${BASE_URL}/api/auth/wx-login`, {
|
code: '0b3ycd0w32tGL53puK1w3ho1Hv2ycd0R',
|
loginIp: '127.0.0.1',
|
deviceInfo: 'test-device-fixed',
|
phoneAuthorized: false
|
}, {
|
headers: {
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('微信登录响应状态:', loginResponse.status);
|
|
if (!loginResponse.data || !loginResponse.data.token) {
|
console.error('❌ 微信登录失败,无法获取token');
|
return;
|
}
|
|
const anonymousToken = loginResponse.data.token;
|
console.log('✅ 成功获取匿名用户token:', anonymousToken.substring(0, 50) + '...\n');
|
|
// 2. 使用匿名用户token保存用户信息(包含电话号码)
|
console.log('2. 使用匿名用户token保存用户信息');
|
const saveUserInfoMutation = `
|
mutation SaveUserInfo($input: UserInput!) {
|
saveUserInfo(input: $input) {
|
id
|
name
|
phone
|
gender
|
birthday
|
wxOpenId
|
unionId
|
avatar
|
}
|
}
|
`;
|
|
const userInput = {
|
name: "测试用户修复版",
|
phone: "13981970816",
|
gender: "MALE",
|
birthday: "1990-01-01",
|
avatar: "/uploads/test-avatar-fixed.jpg"
|
};
|
|
console.log('保存的用户信息:', JSON.stringify(userInput, null, 2));
|
|
const saveResponse = await axios.post(`${BASE_URL}/api/graphql`, {
|
query: saveUserInfoMutation,
|
variables: { input: userInput }
|
}, {
|
headers: {
|
'Authorization': `Bearer ${anonymousToken}`,
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('保存用户信息响应状态:', saveResponse.status);
|
console.log('保存用户信息响应:', JSON.stringify(saveResponse.data, null, 2));
|
|
if (saveResponse.data.errors) {
|
console.error('❌ 保存用户信息失败:', saveResponse.data.errors);
|
return;
|
}
|
|
const savedUser = saveResponse.data.data.saveUserInfo;
|
console.log('✅ 成功保存用户信息');
|
console.log('- 用户ID:', savedUser.id);
|
console.log('- 姓名:', savedUser.name);
|
console.log('- 电话:', savedUser.phone);
|
console.log('- 微信OpenID:', savedUser.wxOpenId);
|
console.log('- 头像:', savedUser.avatar);
|
|
// 3. 验证数据库中的用户信息
|
console.log('\n3. 验证数据库中的用户信息');
|
const userProfileQuery = `
|
query {
|
userProfile {
|
id
|
name
|
phone
|
userType
|
wxOpenId
|
avatar
|
}
|
}
|
`;
|
|
const profileResponse = await axios.post(`${BASE_URL}/api/graphql`, {
|
query: userProfileQuery
|
}, {
|
headers: {
|
'Authorization': `Bearer ${anonymousToken}`,
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('用户档案响应:', JSON.stringify(profileResponse.data, null, 2));
|
|
// 4. 测试重复保存(应该更新现有用户)
|
console.log('\n4. 测试重复保存(应该更新现有用户)');
|
const updatedUserInput = {
|
name: "测试用户修复版-更新",
|
phone: "13981970816", // 相同电话号码
|
gender: "FEMALE",
|
birthday: "1991-02-02",
|
avatar: "/uploads/test-avatar-updated.jpg"
|
};
|
|
const updateResponse = await axios.post(`${BASE_URL}/api/graphql`, {
|
query: saveUserInfoMutation,
|
variables: { input: updatedUserInput }
|
}, {
|
headers: {
|
'Authorization': `Bearer ${anonymousToken}`,
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('更新用户信息响应:', JSON.stringify(updateResponse.data, null, 2));
|
|
if (updateResponse.data.data && updateResponse.data.data.saveUserInfo) {
|
const updatedUser = updateResponse.data.data.saveUserInfo;
|
console.log('✅ 成功更新用户信息');
|
console.log('- 用户ID:', updatedUser.id);
|
console.log('- 更新后姓名:', updatedUser.name);
|
console.log('- 更新后性别:', updatedUser.gender);
|
console.log('- 微信OpenID:', updatedUser.wxOpenId);
|
|
// 验证是否正确记录了wxopenid
|
if (updatedUser.wxOpenId) {
|
console.log('✅ wxopenid已正确记录到数据库');
|
} else {
|
console.log('❌ wxopenid未记录到数据库');
|
}
|
}
|
|
console.log('\n=== 测试完成 ===');
|
|
} catch (error) {
|
console.error('测试过程中发生错误:', error.message);
|
if (error.response) {
|
console.error('错误响应状态:', error.response.status);
|
console.error('错误响应数据:', error.response.data);
|
}
|
}
|
}
|
|
// 运行测试
|
testSaveUserInfoFixed();
|