response-handler-test.html 14.7 KB
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>响应处理器外部配置测试</title>
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      max-width: 1200px;
      margin: 0 auto;
      padding: 20px;
      background-color: #f5f5f5;
    }
    
    h1 {
      color: #333;
      text-align: center;
      margin-bottom: 30px;
    }
    
    .test-section {
      background: white;
      border-radius: 8px;
      padding: 20px;
      margin-bottom: 20px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    
    .test-section h3 {
      color: #2c3e50;
      margin-top: 0;
      border-bottom: 2px solid #3498db;
      padding-bottom: 10px;
    }
    
    .config-display {
      background: #f8f9fa;
      border: 1px solid #dee2e6;
      border-radius: 4px;
      padding: 15px;
      margin: 10px 0;
      font-family: 'Courier New', monospace;
      font-size: 14px;
    }
    
    button {
      background: #3498db;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 4px;
      cursor: pointer;
      font-size: 14px;
      margin: 5px;
    }
    
    button:hover {
      background: #2980b9;
    }
    
    button:disabled {
      background: #bdc3c7;
      cursor: not-allowed;
    }
    
    .result {
      margin-top: 15px;
      padding: 15px;
      border-radius: 4px;
      font-family: 'Courier New', monospace;
      font-size: 13px;
      white-space: pre-wrap;
    }
    
    .result.success {
      background: #d4edda;
      border: 1px solid #c3e6cb;
      color: #155724;
    }
    
    .result.error {
      background: #f8d7da;
      border: 1px solid #f5c6cb;
      color: #721c24;
    }
    
    .result.info {
      background: #d1ecf1;
      border: 1px solid #bee5eb;
      color: #0c5460;
    }
    
    .event-log {
      background: #f8f9fa;
      border: 1px solid #dee2e6;
      border-radius: 4px;
      padding: 10px;
      max-height: 200px;
      overflow-y: auto;
      font-family: 'Courier New', monospace;
      font-size: 12px;
    }
    
    .event-item {
      margin: 2px 0;
      padding: 2px 5px;
      border-radius: 2px;
    }
    
    .event-success { background: #d4edda; }
    .event-error { background: #f8d7da; }
    .event-logout { background: #fff3cd; }
    .event-response { background: #d1ecf1; }
  </style>
</head>
<body>
  <h1>响应处理器外部配置测试</h1>
  <p>测试 response-handler.ts 中的外部配置功能,验证自定义字段映射和事件触发机制</p>

  <!-- 事件日志 -->
  <div class="test-section">
    <h3>事件日志</h3>
    <button onclick="clearEventLog()">清空日志</button>
    <div id="eventLog" class="event-log"></div>
  </div>

  <!-- 测试1:自定义 code_key 配置 -->
  <div class="test-section">
    <h3>测试1:自定义 code_key 配置</h3>
    <p>测试使用自定义的状态码字段名</p>
    
    <div class="config-display">
配置:
{
  httpOptions: {
    customer: {
      response: {
        code_key: "status",           // 自定义状态码字段
        code_success_value: "200",    // 成功状态码
        data_key: "result"            // 数据字段
      }
    }
  }
}

模拟响应:
{
  "status": "200",
  "result": { "message": "操作成功", "id": 123 },
  "timestamp": "2024-01-01T00:00:00Z"
}
    </div>
    
    <button onclick="testCustomCodeKey()">测试自定义 code_key</button>
    <div id="result1" class="result" style="display: none;"></div>
  </div>

  <!-- 测试2:数组形式的 code_key 配置 -->
  <div class="test-section">
    <h3>测试2:数组形式的 code_key 配置</h3>
    <p>测试使用数组形式的状态码字段名,自动匹配存在的字段</p>
    
    <div class="config-display">
配置:
{
  httpOptions: {
    customer: {
      response: {
        code_key: ["code", "status", "errorCode"],  // 数组形式,自动匹配
        code_success_value: "0",
        data_key: "data"
      }
    }
  }
}

模拟响应:
{
  "errorCode": "0",
  "data": { "list": [1, 2, 3], "total": 3 },
  "message": "查询成功"
}
    </div>
    
    <button onclick="testArrayCodeKey()">测试数组 code_key</button>
    <div id="result2" class="result" style="display: none;"></div>
  </div>

  <!-- 测试3:登出状态码配置 -->
  <div class="test-section">
    <h3>测试3:登出状态码配置</h3>
    <p>测试登出状态码的识别和事件触发</p>
    
    <div class="config-display">
配置:
{
  httpOptions: {
    customer: {
      response: {
        code_key: "code",
        errId: "errorId",
        errId_logout_value: "401",    // 登出状态码
        data_key: "data"
      }
    }
  }
}

模拟响应:
{
  "code": "500",
  "errorId": "401",
  "data": null,
  "message": "登录已过期,请重新登录"
}
    </div>
    
    <button onclick="testLogoutConfig()">测试登出配置</button>
    <div id="result3" class="result" style="display: none;"></div>
  </div>

  <!-- 测试4:returnAll 模式 -->
  <div class="test-section">
    <h3>测试4:returnAll 模式</h3>
    <p>测试 returnAll 模式下返回完整响应数据</p>
    
    <div class="config-display">
配置:
{
  returnAll: true,                  // 返回完整响应
  httpOptions: {
    customer: {
      response: {
        code_key: "status",
        errId_logout_value: "401",
        data_key: "payload"
      }
    }
  }
}

模拟响应:
{
  "status": "500",
  "payload": { "error": "服务器错误" },
  "meta": { "timestamp": "2024-01-01", "version": "1.0" }
}
    </div>
    
    <button onclick="testReturnAllMode()">测试 returnAll 模式</button>
    <div id="result4" class="result" style="display: none;"></div>
  </div>

  <!-- 测试5:无配置情况 -->
  <div class="test-section">
    <h3>测试5:无配置情况</h3>
    <p>测试没有提供 httpOptions 配置时的默认行为</p>
    
    <div class="config-display">
配置:
{
  // 无 httpOptions 配置
}

模拟响应:
{
  "code": "200",
  "data": { "message": "默认处理" },
  "success": true
}
    </div>
    
    <button onclick="testNoConfig()">测试无配置</button>
    <div id="result5" class="result" style="display: none;"></div>
  </div>

  <script type="module">
    // 导入必要的模块
    import { processResponse, addEventListener } from '../src/adapter/handlers/response-handler.ts';
    
    // 将函数暴露到全局作用域
    window.processResponse = processResponse;
    window.addEventListener = addEventListener;
    
    // 事件日志
    let eventLog = [];
    
    // 注册事件监听器
    function setupEventListeners() {
      const eventTypes = ['success', 'error', 'logout', 'response'];
      
      eventTypes.forEach(eventType => {
        addEventListener(eventType, (data, config) => {
          const timestamp = new Date().toLocaleTimeString();
          eventLog.push({
            type: eventType,
            timestamp,
            data,
            config: config ? { url: config.url, method: config.method } : null
          });
          updateEventLogDisplay();
        });
      });
    }
    
    // 更新事件日志显示
    function updateEventLogDisplay() {
      const logElement = document.getElementById('eventLog');
      logElement.innerHTML = eventLog.map(log => 
        `<div class="event-item event-${log.type}">
          [${log.timestamp}] ${log.type.toUpperCase()}: ${JSON.stringify(log.data)}
        </div>`
      ).join('');
      logElement.scrollTop = logElement.scrollHeight;
    }
    
    // 清空事件日志
    window.clearEventLog = function() {
      eventLog = [];
      updateEventLogDisplay();
    };
    
    // 初始化事件监听器
    setupEventListeners();
    
    console.log('✅ 响应处理器测试页面初始化完成');
  </script>

  <script>
    // 测试函数需要在全局作用域中定义
    
    // 测试1:自定义 code_key 配置
    window.testCustomCodeKey = function() {
      const resultDiv = document.getElementById('result1');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result info';
      resultDiv.textContent = '测试中...';
      
      try {
        // 模拟响应数据
        const responseData = {
          data: {
            "status": "200",
            "result": { "message": "操作成功", "id": 123 },
            "timestamp": "2024-01-01T00:00:00Z"
          },
          status: 200,
          statusText: 'OK'
        };
        
        // 配置
        const config = {
          url: '/api/test',
          method: 'GET',
          httpOptions: {
            customer: {
              response: {
                code_key: "status",
                code_success_value: "200",
                data_key: "result"
              }
            }
          }
        };
        
        // 处理响应
        const result = window.processResponse(responseData, config);
        
        resultDiv.className = 'result success';
        resultDiv.textContent = `✓ 测试成功\n\n原始响应:\n${JSON.stringify(responseData.data, null, 2)}\n\n处理后结果:\n${JSON.stringify(result, null, 2)}`;
        
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.textContent = `✗ 测试失败\n错误:${error.message}\n堆栈:${error.stack}`;
      }
    };
    
    // 测试2:数组形式的 code_key 配置
    window.testArrayCodeKey = function() {
      const resultDiv = document.getElementById('result2');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result info';
      resultDiv.textContent = '测试中...';
      
      try {
        const responseData = {
          data: {
            "errorCode": "0",
            "data": { "list": [1, 2, 3], "total": 3 },
            "message": "查询成功"
          },
          status: 200,
          statusText: 'OK'
        };
        
        const config = {
          url: '/api/list',
          method: 'GET',
          httpOptions: {
            customer: {
              response: {
                code_key: ["code", "status", "errorCode"],
                code_success_value: "0",
                data_key: "data"
              }
            }
          }
        };
        
        const result = window.processResponse(responseData, config);
        
        resultDiv.className = 'result success';
        resultDiv.textContent = `✓ 测试成功\n\n原始响应:\n${JSON.stringify(responseData.data, null, 2)}\n\n处理后结果:\n${JSON.stringify(result, null, 2)}\n\n说明:自动匹配到 "errorCode" 字段`;
        
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.textContent = `✗ 测试失败\n错误:${error.message}`;
      }
    };
    
    // 测试3:登出状态码配置
    window.testLogoutConfig = function() {
      const resultDiv = document.getElementById('result3');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result info';
      resultDiv.textContent = '测试中...';
      
      try {
        const responseData = {
          data: {
            "code": "500",
            "errorId": "401",
            "data": null,
            "message": "登录已过期,请重新登录"
          },
          status: 200,
          statusText: 'OK'
        };
        
        const config = {
          url: '/api/protected',
          method: 'GET',
          httpOptions: {
            customer: {
              response: {
                code_key: "code",
                errId: "errorId",
                errId_logout_value: "401",
                data_key: "data"
              }
            }
          }
        };
        
        const result = window.processResponse(responseData, config);
        
        resultDiv.className = 'result success';
        resultDiv.textContent = `✓ 测试成功\n\n原始响应:\n${JSON.stringify(responseData.data, null, 2)}\n\n处理后结果:\n${JSON.stringify(result, null, 2)}\n\n说明:应该触发 logout 事件(检查事件日志)`;
        
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.textContent = `✗ 测试失败\n错误:${error.message}`;
      }
    };
    
    // 测试4:returnAll 模式
    window.testReturnAllMode = function() {
      const resultDiv = document.getElementById('result4');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result info';
      resultDiv.textContent = '测试中...';
      
      try {
        const responseData = {
          data: {
            "status": "500",
            "payload": { "error": "服务器错误" },
            "meta": { "timestamp": "2024-01-01", "version": "1.0" }
          },
          status: 200,
          statusText: 'OK'
        };
        
        const config = {
          url: '/api/error',
          method: 'GET',
          returnAll: true,
          httpOptions: {
            customer: {
              response: {
                code_key: "status",
                errId_logout_value: "401",
                data_key: "payload"
              }
            }
          }
        };
        
        const result = window.processResponse(responseData, config);
        
        resultDiv.className = 'result success';
        resultDiv.textContent = `✓ 测试成功\n\n原始响应:\n${JSON.stringify(responseData.data, null, 2)}\n\n处理后结果:\n${JSON.stringify(result, null, 2)}\n\n说明:returnAll 模式返回完整响应数据`;
        
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.textContent = `✗ 测试失败\n错误:${error.message}`;
      }
    };
    
    // 测试5:无配置情况
    window.testNoConfig = function() {
      const resultDiv = document.getElementById('result5');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result info';
      resultDiv.textContent = '测试中...';
      
      try {
        const responseData = {
          data: {
            "code": "200",
            "data": { "message": "默认处理" },
            "success": true
          },
          status: 200,
          statusText: 'OK'
        };
        
        const config = {
          url: '/api/default',
          method: 'GET'
          // 无 httpOptions 配置
        };
        
        const result = window.processResponse(responseData, config);
        
        resultDiv.className = 'result success';
        resultDiv.textContent = `✓ 测试成功\n\n原始响应:\n${JSON.stringify(responseData.data, null, 2)}\n\n处理后结果:\n${JSON.stringify(result, null, 2)}\n\n说明:无配置时直接返回原始响应数据`;
        
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.textContent = `✗ 测试失败\n错误:${error.message}`;
      }
    };
  </script>
</body>
</html>