<?php
// 简单的前端加密页：加载腾讯 ptlogin.js，计算 p 并通过 postMessage 回传
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>QQ密码加密</title>
  <style>
    body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial;color:#374151;margin:0;padding:20px;background:#f8fafc}
    .box{max-width:520px;margin:40px auto;background:#fff;border:1px solid #e5e7eb;border-radius:12px;box-shadow:0 6px 16px rgba(0,0,0,.06);padding:20px}
    h3{margin:0 0 8px 0;font-size:16px}
    .muted{font-size:12px;color:#6b7280;margin-bottom:10px}
    .status{margin-top:8px;font-size:13px;color:#6b7280}
    .ok{color:#059669}.err{color:#b91c1c}
  </style>
  <script src="assets/js/ptlogin_local.js"></script>
</head>
<body>
  <div class="box">
    <h3>正在准备加密…</h3>
    <div id="info" class="muted">请保持此窗口打开，完成后将自动返回</div>
    <div id="status" class="status"></div>
  </div>
  <script>
    function setStatus(text, ok){
      var s=document.getElementById('status');
      s.textContent=text;
      s.className='status ' + (ok?'ok':'err');
    }
    // 兼容不同版本的ptlogin.js：找出加密函数
    function computeP(pwd, uinHex, vcode){
      if (typeof getEncryption === 'function'){
        return getEncryption(pwd, uinHex, vcode);
      }
      if (typeof RSAData === 'function' && typeof md5 === 'function'){
        // 旧版兜底（不保证可用）
        try {
          return RSAData(pwd, vcode, uinHex);
        } catch(e){}
      }
      return null;
    }
    // 接收父页面数据：{qq,pwd,vcode,uin_hex}
    window.addEventListener('message', function(ev){
      var d = ev.data || {};
      if (!d || d.type !== 'qzone_pwd_params') return;
      try{
        document.getElementById('info').textContent = '收到参数，正在加密…';
        var p = computeP(String(d.pwd||''), String(d.uin_hex||''), String(d.vcode||''));
        if (!p){
          setStatus('加密函数不可用，请刷新重试或更换环境', false);
          ev.source && ev.source.postMessage({type:'pwd_encrypted', ok:false, msg:'encrypt_unavailable'}, ev.origin || '*');
          return;
        }
        setStatus('加密完成，正在返回…', true);
        ev.source && ev.source.postMessage({type:'pwd_encrypted', ok:true, p:p, vcode:d.vcode, qq:d.qq}, ev.origin || '*');
        if (window.opener) {
          setTimeout(function(){ window.close(); }, 500);
        }
      }catch(e){
        setStatus('加密异常：'+ e.message, false);
        ev.source && ev.source.postMessage({type:'pwd_encrypted', ok:false, msg:e.message}, ev.origin || '*');
      }
    });
    // 通知父页面就绪
    if (window.opener) {
      window.opener.postMessage({type:'qzone_pwd_ready'}, '*');
    } else if (window.parent && window.parent !== window) {
      window.parent.postMessage({type:'qzone_pwd_ready'}, '*');
    }
  </script>
</body>
</html>
