lrj
昨天 9f8395fab13ca4b230a0f7d62636e209745c91d4
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
/**
 * 测试真实微信登录流程
 * 验证修复后的wxLogin接口是否能正确处理真实微信code
 */
async function testRealWechatLogin() {
    // 动态导入 node-fetch
    const { default: fetch } = await import('node-fetch');
    console.log('=== 测试真实微信登录流程 ===');
    console.log('测试时间:', new Date().toLocaleString());
    
    const graphqlUrl = 'http://localhost:8080/api/graphql';
    
    // 模拟一个真实的微信code(实际使用时应该从小程序端获取)
    const testCode = 'real_wechat_code_from_miniprogram';
    
    const mutation = `
        mutation {
            wxLogin(input: {
                code: "${testCode}"
                loginIp: "192.168.1.100"
                deviceInfo: "iPhone 13 Pro"
                phoneAuthorized: false
            }) {
                token
                userInfo {
                    userId
                    name
                    userType
                }
                isNewUser
                loginRecordId
                sessionKey
            }
        }
    `;
    
    try {
        console.log('步骤1: 发送wxLogin请求');
        console.log('- GraphQL端点:', graphqlUrl);
        console.log('- 使用code:', testCode);
        console.log('- 测试模式: false (应该调用真实微信API)');
        
        const response = await fetch(graphqlUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                query: mutation
            })
        });
        
        const result = await response.json();
        
        console.log('\n步骤2: 分析响应结果');
        console.log('HTTP状态码:', response.status);
        
        if (result.errors) {
            console.log('❌ GraphQL错误:');
            result.errors.forEach((error, index) => {
                console.log(`  ${index + 1}. ${error.message}`);
                if (error.extensions) {
                    console.log(`     分类: ${error.extensions.classification}`);
                }
            });
            
            // 检查是否是微信API相关的错误
            const wechatApiError = result.errors.find(error => 
                error.message.includes('微信API') || 
                error.message.includes('AppSecret') ||
                error.message.includes('code2session')
            );
            
            if (wechatApiError) {
                console.log('\n🔍 检测到微信API相关错误:');
                console.log('这表明系统正在尝试调用真实的微信API,而不是返回测试数据');
                console.log('错误原因可能是:');
                console.log('1. 使用的code不是真实的微信登录code');
                console.log('2. 微信小程序配置(AppID/AppSecret)需要验证');
                console.log('3. 网络连接问题');
                
                console.log('\n✅ 修复验证: test-mode已成功设置为false');
                console.log('系统现在会调用真实的微信API而不是返回测试数据');
            }
        } else {
            console.log('✅ 请求成功');
            console.log('响应数据:', JSON.stringify(result.data, null, 2));
            
            const wxLoginData = result.data.wxLogin;
            if (wxLoginData) {
                console.log('\n📊 登录结果分析:');
                console.log('- Token存在:', !!wxLoginData.token);
                console.log('- 用户ID:', wxLoginData.userInfo?.userId);
                console.log('- 是否新用户:', wxLoginData.isNewUser);
                console.log('- 登录记录ID:', wxLoginData.loginRecordId);
                console.log('- SessionKey存在:', !!wxLoginData.sessionKey);
                console.log('- SessionKey长度:', wxLoginData.sessionKey?.length || 0);
                
                // 检查sessionKey是否是测试数据
                if (wxLoginData.sessionKey && wxLoginData.sessionKey.startsWith('test_session_key_')) {
                    console.log('⚠️  警告: SessionKey仍然是测试数据');
                    console.log('这表明test-mode可能仍然为true,或者配置未生效');
                } else if (wxLoginData.sessionKey) {
                    console.log('✅ SessionKey看起来是真实数据(不是test_开头)');
                }
            }
        }
        
    } catch (error) {
        console.log('❌ 请求异常:', error.message);
        console.log('错误详情:', error);
    }
    
    console.log('\n=== 测试完成 ===');
    console.log('下一步: 如果看到微信API相关错误,说明修复成功');
    console.log('需要使用真实的微信小程序登录code进行最终测试');
}
 
// 运行测试
testRealWechatLogin().catch(console.error);