Codex Assistant
17 小时以前 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// 调试小程序用户信息显示问题 - 修复后版本
console.log('=== 调试小程序用户信息显示问题 - 修复后版本 ===');
 
// 模拟小程序环境 - 使用正确的用户信息结构
const mockApp = {
  globalData: {
    userInfo: {
      userId: 'test123',
      name: '张三',
      phone: '13800138000',
      email: 'test@example.com',
      avatar: 'https://example.com/avatar.jpg', // 直接在userInfo下
      gender: 'MALE', // 字符串格式或数字格式
      birthday: '1990-05-15' // 直接在userInfo下
    }
  }
};
 
// 测试数字格式的性别
const mockAppWithNumericGender = {
  globalData: {
    userInfo: {
      userId: 'test456',
      name: '李四',
      phone: '13900139000',
      email: 'test2@example.com',
      avatarUrl: 'https://example.com/avatar2.jpg', // 兼容旧字段名
      gender: 1, // 数字格式:0为男,1为女
      birthDate: '1992-08-20' // 兼容旧字段名
    }
  }
};
 
// 模拟页面数据
const pageData = {
  formData: {
    name: '',
    phone: '',
    email: '',
    avatarUrl: '',
    gender: null,
    education: '',
    birthDate: ''
  },
  displayUserInfo: {},
  genderOptions: ['男', '女'],
  genderIndex: -1,
  educationOptions: ['高中及以下', '大专', '本科', '硕士', '博士'],
  educationIndex: -1,
  localAvatarPath: ''
};
 
// 模拟修复后的 loadUserInfo 方法
function loadUserInfo(app = mockApp) {
  console.log('\n--- 执行修复后的 loadUserInfo ---');
  const userInfo = app.globalData.userInfo;
  console.log('全局用户信息:', JSON.stringify(userInfo, null, 2));
  
  if (userInfo) {
    let displayUserInfo = {
      name: userInfo.name || '',
      phone: userInfo.phone || '',
      avatarUrl: userInfo.avatar || userInfo.avatarUrl || '',
      gender: null,
      education: '',
      birthDate: ''
    };
    
    // 处理性别信息 - 直接从userInfo获取
    if (userInfo.gender !== undefined && userInfo.gender !== null) {
      // 如果是字符串格式,转换为数字
      if (typeof userInfo.gender === 'string') {
        displayUserInfo.gender = userInfo.gender === 'MALE' ? 0 : 1;
      } else {
        displayUserInfo.gender = parseInt(userInfo.gender);
      }
    }
    
    // 处理生日信息 - 直接从userInfo获取
    if (userInfo.birthday || userInfo.birthDate) {
      displayUserInfo.birthDate = userInfo.birthday || userInfo.birthDate;
    }
    
    pageData.displayUserInfo = displayUserInfo;
    console.log('设置 displayUserInfo:', JSON.stringify(pageData.displayUserInfo, null, 2));
  }
}
 
// 模拟修复后的 prefillUserInfo 方法
function prefillUserInfo(app = mockApp) {
  console.log('\n--- 执行修复后的 prefillUserInfo ---');
  const userInfo = app.globalData.userInfo;
  
  if (userInfo) {
    console.log('用户信息:', JSON.stringify(userInfo, null, 2));
    
    // 更新 formData
    pageData.formData.name = userInfo.name || '';
    pageData.formData.phone = userInfo.phone || '';
    pageData.formData.email = userInfo.email || '';
    pageData.formData.avatarUrl = userInfo.avatar || userInfo.avatarUrl || '';
    
    // 处理性别信息 - 直接从userInfo获取
    if (userInfo.gender !== undefined && userInfo.gender !== null) {
      let genderIndex;
      // 如果是字符串格式,转换为数字
      if (typeof userInfo.gender === 'string') {
        genderIndex = userInfo.gender === 'MALE' ? 0 : 1;
      } else {
        genderIndex = parseInt(userInfo.gender);
      }
      pageData.formData.gender = genderIndex;
      pageData.genderIndex = genderIndex;
      console.log('设置性别:', genderIndex === 0 ? '男' : '女', '索引:', genderIndex);
    }
    
    // 处理生日信息 - 直接从userInfo获取
    if (userInfo.birthday || userInfo.birthDate) {
      const birthDate = userInfo.birthday || userInfo.birthDate;
      pageData.formData.birthDate = birthDate;
      console.log('设置生日:', birthDate);
    }
    
    console.log('最终 formData:', JSON.stringify(pageData.formData, null, 2));
  }
}
 
// 模拟模板显示逻辑
function simulateTemplateDisplay() {
  console.log('\n--- 模拟模板显示 ---');
  
  // 头像显示逻辑
  const avatarSrc = pageData.localAvatarPath || pageData.formData.avatarUrl || '../../images/default-avatar.svg';
  console.log('头像显示路径:', avatarSrc);
  
  // 性别显示逻辑
  const genderDisplay = pageData.formData.gender !== null ? 
    pageData.genderOptions[pageData.formData.gender] : '请选择性别';
  console.log('性别显示文本:', genderDisplay);
  
  // 生日显示逻辑
  const birthDateDisplay = pageData.formData.birthDate || '请选择生日';
  console.log('生日显示文本:', birthDateDisplay);
  
  // 检查问题
  console.log('\n--- 问题诊断 ---');
  
  if (!pageData.formData.avatarUrl) {
    console.log('❌ 头像URL为空');
  } else {
    console.log('✅ 头像URL正常:', pageData.formData.avatarUrl);
  }
  
  if (pageData.formData.gender === null || pageData.formData.gender === undefined) {
    console.log('❌ 性别未设置');
  } else {
    console.log('✅ 性别正常:', pageData.formData.gender, '显示为:', genderDisplay);
  }
  
  if (!pageData.formData.birthDate) {
    console.log('❌ 生日未设置');
  } else {
    console.log('✅ 生日正常:', pageData.formData.birthDate);
  }
}
 
// 执行测试
console.log('\n=== 开始测试 - 字符串格式性别 ===');
 
// 1. 测试字符串格式的用户信息
loadUserInfo(mockApp);
prefillUserInfo(mockApp);
 
// 模拟模板显示逻辑
console.log('\n--- 模拟模板显示逻辑 (字符串格式) ---');
let avatarSrc = pageData.localAvatarPath || pageData.formData.avatarUrl || '/images/default-avatar.png';
console.log('头像显示:', avatarSrc);
 
let genderDisplay = pageData.genderIndex !== -1 ? pageData.genderOptions[pageData.genderIndex] : '请选择性别';
console.log('性别显示:', genderDisplay);
 
let birthDateDisplay = pageData.formData.birthDate || '请选择生日';
console.log('生日显示:', birthDateDisplay);
 
console.log('\n=== 开始测试 - 数字格式性别 ===');
 
// 2. 重置页面数据
pageData.formData = {
  name: '',
  phone: '',
  email: '',
  avatarUrl: '',
  gender: -1,
  education: '',
  birthDate: ''
};
pageData.genderIndex = -1;
pageData.educationIndex = -1;
 
// 测试数字格式的用户信息
loadUserInfo(mockAppWithNumericGender);
prefillUserInfo(mockAppWithNumericGender);
 
// 模拟模板显示逻辑
console.log('\n--- 模拟模板显示逻辑 (数字格式) ---');
avatarSrc = pageData.localAvatarPath || pageData.formData.avatarUrl || '/images/default-avatar.png';
console.log('头像显示:', avatarSrc);
 
genderDisplay = pageData.genderIndex !== -1 ? pageData.genderOptions[pageData.genderIndex] : '请选择性别';
console.log('性别显示:', genderDisplay);
 
birthDateDisplay = pageData.formData.birthDate || '请选择生日';
console.log('生日显示:', birthDateDisplay);
 
// 4. 诊断结果
console.log('\n=== 诊断结果 ===');
console.log('头像URL:', pageData.formData.avatarUrl ? '✓ 正常' : '✗ 缺失');
console.log('性别信息:', pageData.genderIndex !== -1 ? '✓ 正常' : '✗ 缺失');
console.log('生日信息:', pageData.formData.birthDate ? '✓ 正常' : '✗ 缺失');
console.log('\n修复完成!用户信息结构已统一,支持多种格式的性别和生日字段。');
 
console.log('\n=== 调试完成 ===');