whitelist-simple-test.html 5.17 KB
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>白名单URL简单测试</title>
  <script src="../dist/index.umd.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
    }

    .container {
      display: flex;
      flex-direction: column;
      gap: 20px;
    }

    .card {
      border: 1px solid #ddd;
      border-radius: 8px;
      padding: 15px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    pre {
      background-color: #f5f5f5;
      padding: 10px;
      border-radius: 4px;
      overflow-x: auto;
    }

    button {
      padding: 8px 16px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      margin-right: 10px;
      margin-bottom: 10px;
    }

    button:hover {
      background-color: #45a049;
    }
  </style>
</head>

<body>
  <h1>白名单URL简单测试</h1>

  <div class="container">
    <div class="card">
      <h2>测试说明</h2>
      <p>本页面测试在请求前检查method参数是否在白名单中,如果在白名单中,则在URL中添加sdctag=true参数并参与签名。</p>
      <button id="testWhitelist">测试GET请求</button>
      <button id="testNonWhitelist">测试POST请求</button>
      <h3>结果:</h3>
      <pre id="result">点击按钮开始测试...</pre>
    </div>
  </div>

  <script>
    // 等待页面加载完成
    document.addEventListener('DOMContentLoaded', function () {
      // 获取DOM元素
      const resultElement = document.getElementById('result');
      const testWhitelistButton = document.getElementById('testWhitelist');
      const testNonWhitelistButton = document.getElementById('testNonWhitelist');

      // 检查GlskFetch对象
      console.log('GlskFetch:', window.GlskFetch);

      // 获取Request构造函数
      let RequestConstructor = null;
      if (typeof window.GlskFetch === 'function') {
        // 如果GlskFetch本身是构造函数
        RequestConstructor = window.GlskFetch;
      } else if (window.GlskFetch && typeof window.GlskFetch.default === 'function') {
        // 如果GlskFetch.default是构造函数
        RequestConstructor = window.GlskFetch.default;
      }

      if (!RequestConstructor) {
        console.error('Request构造函数不存在');
        resultElement.textContent = 'SDK加载失败:Request构造函数不存在';
        return;
      }

      // 注意:Trace对象在UMD模块中可能不可直接访问
      // 但Request类内部已经正确引入并使用了Trace对象
      // 所以白名单功能应该能正常工作
      console.log('SDK内部已集成Trace模块,将从服务器获取白名单URL');

      // 创建请求实例
      const request = new RequestConstructor({
        baseURL: 'http://192.168.1.117:9898/facade',
        appId: 'dj_zhixin',
        appSecret: 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b',
        sentryDsn: 'https://be4fb79a912b4c34a5d1c539a1618978@sentry.glsx.com.cn/1',
      });

      // 测试白名单内请求
      testWhitelistButton.addEventListener('click', async function () {
        resultElement.textContent = '请求中...';
        try {
          // 白名单URL列表由SDK内部处理
          // 我们只需要在请求参数中添加method参数
          console.log('发送请求,SDK将自动检查白名单并添加sdctag参数');

          // 使用params包裹GET请求参数
          const result = await request.get('/dict/sign/test', {
            params:{
            "id":6,
            "objs": ["hehe", "haah"],
            "dogs":[{"name":"haogou","age":1},{"name":"拉布拉多","age":2}],
             "dogs":{"name":"haogou","age":1}
        }
          });

          resultElement.textContent =
            `请求成功:\n请求参数: ${JSON.stringify(result.config.data, null, 2)}\n\n响应数据: ${JSON.stringify(result.data, null, 2)}`;
        } catch (error) {
          resultElement.textContent = `请求失败: ${error.message}`;
        }
      });

      // 测试白名单外请求
      testNonWhitelistButton.addEventListener('click', async function () {
        resultElement.textContent = '请求中...';
        try {
          // 白名单URL列表由SDK内部处理
          // 我们只需要在请求参数中添加method参数
          console.log('发送请求,SDK将自动检查白名单并添加sdctag参数');
          
          // POST请求使用data传递参数(放在请求体中)
          const result = await request.post('/dict/sign/test1', {
            "id": 6,
            "objs": ["hehe", "haah"],
            "dogs": [{"name": "haogou", "age": 1}, {"name": "拉布拉多", "age": 2}],
            "dog": {"name": "haogou", "age": 1}
          });
          
          resultElement.textContent =
            `请求成功:\n请求参数: ${JSON.stringify(result.config.params, null, 2)}\n\n响应数据: ${JSON.stringify(result.data, null, 2)}`;
        } catch (error) {
          resultElement.textContent = `请求失败: ${error.message}`;
        }
      });
    });
  </script>
</body>

</html>