Codex Assistant
21 小时以前 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
// 测试 wxs 格式化函数的问题
 
// 模拟 wxs 中的 formatDateYYYYMMDD 函数
function formatDateYYYYMMDD_wxs(val) {
  // 强制转换为字符串,兼容 null, undefined, number 等类型
  var s = '' + val;
 
  if (s.length < 10) {
    return '—';
  }
 
  var out = '';
 
  // 优先处理 '2024-07-31...' 格式
  if (s.charAt(4) === '-' && s.charAt(7) === '-') {
    out = s.slice(0, 10); // 使用 slice 替换 substr
    return out;
  }
 
  // 备用处理 '2024/07/31...' 格式
  if (s.charAt(4) === '/' && s.charAt(7) === '/') {
    var year = s.slice(0, 4);
    var month = s.slice(5, 7);
    var day = s.slice(8, 10);
    out = year + '-' + month + '-' + day;
    return out;
  }
 
  // 对于无法识别的字符串格式,返回占位符
  return '—';
}
 
// 模拟 JavaScript 中的 formatDateYYYYMMDD 函数
function formatDateYYYYMMDD_js(date) {
  if (!date && date !== 0) return '—'
  // 字符串:优先匹配 YYYY-MM-DD 直接返回,避免解析
  if (typeof date === 'string') {
    const m = date.match(/^(\d{4}-\d{2}-\d{2})/)
    if (m) return m[1]
  }
  // 数值:时间戳(秒/毫秒)兜底
  if (typeof date === 'number') {
    const ts = date > 1e12 ? date : date * 1000
    const d = new Date(ts)
    if (!isNaN(d.getTime())) {
      const y = d.getFullYear()
      const m = String(d.getMonth() + 1).padStart(2, '0')
      const day = String(d.getDate()).padStart(2, '0')
      return `${y}-${m}-${day}`
    }
  }
  // 其他情况走工具函数兜底
  return '—'
}
 
console.log('🔍 测试 wxs 和 JavaScript 格式化函数的差异\n');
 
// 测试数据 - 来自后端的真实数据
const testDates = [
  "2025-10-11T00:00",
  "2025-10-06T00:00", 
  "2025-10-07T00:00",
  "2025-10-04T00:00",
  "2025-10-12T00:00"
];
 
console.log('📊 测试结果对比:');
console.log('─'.repeat(80));
console.log('原始数据'.padEnd(20) + 'wxs函数结果'.padEnd(20) + 'JS函数结果'.padEnd(20) + '是否一致');
console.log('─'.repeat(80));
 
testDates.forEach(date => {
  const wxsResult = formatDateYYYYMMDD_wxs(date);
  const jsResult = formatDateYYYYMMDD_js(date);
  const isMatch = wxsResult === jsResult;
  
  console.log(
    date.padEnd(20) + 
    wxsResult.padEnd(20) + 
    jsResult.padEnd(20) + 
    (isMatch ? '✅' : '❌')
  );
});
 
console.log('─'.repeat(80));
 
// 测试边界情况
console.log('\n🔍 测试边界情况:');
const edgeCases = [
  null,
  undefined,
  "",
  "2025-10-04",
  "2025/10/04",
  "invalid-date",
  "2025-10-04T00:00:00.000Z"
];
 
edgeCases.forEach(testCase => {
  const wxsResult = formatDateYYYYMMDD_wxs(testCase);
  const jsResult = formatDateYYYYMMDD_js(testCase);
  console.log(`输入: ${testCase} -> wxs: "${wxsResult}", js: "${jsResult}"`);
});
 
// 检查是否有固定返回值的问题
console.log('\n🚨 检查是否存在固定返回值问题:');
const uniqueWxsResults = [...new Set(testDates.map(formatDateYYYYMMDD_wxs))];
const uniqueJsResults = [...new Set(testDates.map(formatDateYYYYMMDD_js))];
 
console.log(`wxs函数返回的唯一值: ${uniqueWxsResults.join(', ')}`);
console.log(`JS函数返回的唯一值: ${uniqueJsResults.join(', ')}`);
 
if (uniqueWxsResults.length === 1 && testDates.length > 1) {
  console.log('❌ 发现问题:wxs函数对所有不同输入返回相同值!');
} else {
  console.log('✅ wxs函数正常处理不同输入');
}
 
if (uniqueJsResults.length === 1 && testDates.length > 1) {
  console.log('❌ 发现问题:JS函数对所有不同输入返回相同值!');
} else {
  console.log('✅ JS函数正常处理不同输入');
}