SOURCE

console 命令行工具 X clear

                    
>
console
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Base62 编码 / 解码工具</title>
  <style>
    :root {
      color-scheme: light dark;
    }

    body {
      font-family: 'Segoe UI', sans-serif;
      max-width: 600px;
      margin: 50px auto;
      padding: 20px;
      background: var(--bg, #fff);
      color: var(--text, #333);
      border-radius: 12px;
      box-shadow: 0 10px 30px rgba(0,0,0,0.1);
    }

    h1 {
      text-align: center;
      margin-bottom: 30px;
    }

    label {
      font-weight: bold;
      margin-top: 20px;
      display: block;
    }

    input {
      width: 100%;
      padding: 10px;
      font-size: 16px;
      margin-top: 8px;
      border: 1px solid #ccc;
      border-radius: 6px;
      box-sizing: border-box;
    }

    button {
      margin-top: 12px;
      padding: 10px 20px;
      font-size: 16px;
      border: none;
      background-color: #4f46e5;
      color: white;
      border-radius: 6px;
      cursor: pointer;
      transition: background-color 0.2s;
    }

    button:hover {
      background-color: #4338ca;
    }

    .output {
      margin-top: 12px;
      background: #f4f4f5;
      padding: 10px;
      border-radius: 6px;
      font-family: monospace;
      white-space: pre-wrap;
    }

    .error {
      color: red;
      font-size: 14px;
    }

    @media (prefers-color-scheme: dark) {
      body {
        --bg: #1f1f1f;
        --text: #e0e0e0;
      }
      .output {
        background: #2b2b2b;
      }
      input, button {
        background: #2a2a2a;
        color: #fff;
        border: 1px solid #444;
      }
    }
  </style>
</head>
<body>
  <h1>Base62 编码 / 解码</h1>

  <label for="inputNumber">原始数字</label>
  <input id="inputNumber" placeholder="输入 Snowflake ID 或大整数,如 1940249849942573057" />
  <button onclick="handleEncode()">编码为 Base62</button>
  <div class="output" id="encodedOutput">编码结果将在此显示</div>

  <label for="inputBase62">Base62 字符串</label>
  <input id="inputBase62" placeholder="输入 Base62,如 0hTZ3xnv" />
  <button onclick="handleDecode()">解码为数字</button>
  <div class="output" id="decodedOutput">解码结果将在此显示</div>

  <script>
    const base62Chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    function encodeBase62(input) {
      let num = typeof input === 'bigint' ? input : BigInt(input.toString());
      let res = '';
      while (num > 0n) {
        const remainder = num % 62n;
        res = base62Chars[Number(remainder)] + res;
        num = num / 62n;
      }
      return res.padStart(8, '0');
    }

    function decodeBase62(str) {
      let num = 0n;
      for (const char of str) {
        const index = BigInt(base62Chars.indexOf(char));
        if (index < 0n) throw new Error(`字符 "${char}" 不是有效的 Base62 字符`);
        num = num * 62n + index;
      }
      return num.toString();
    }

    function handleEncode() {
      const input = document.getElementById('inputNumber').value.trim();
      const output = document.getElementById('encodedOutput');
      try {
        const result = encodeBase62(input);
        output.textContent = `✅ 编码结果:${result}`;
        output.classList.remove('error');
      } catch (e) {
        output.textContent = `❌ 编码失败:${e.message}`;
        output.classList.add('error');
      }
    }

    function handleDecode() {
      const input = document.getElementById('inputBase62').value.trim();
      const output = document.getElementById('decodedOutput');
      try {
        const result = decodeBase62(input);
        output.textContent = `✅ 解码结果:${result}`;
        output.classList.remove('error');
      } catch (e) {
        output.textContent = `❌ 解码失败:${e.message}`;
        output.classList.add('error');
      }
    }
  </script>
</body>
</html>