Codex Assistant
1 天以前 58d9f460b2f8c34430285115e2557d18333c5cab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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();