<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>大麦网 · 表单平台后台</title>
<link rel="icon" href="data:,">
<style>
*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{font-family:-apple-system,BlinkMacSystemFont,"PingFang SC","Microsoft YaHei","Helvetica Neue",Arial,sans-serif;background:#fff;color:#303133}
/* ===== 1:1 源站布局：左 400px 表单 + 右 570x550 banner，垂直居中 ===== */
.login-container{min-height:100vh;width:100%;display:flex;align-items:center;justify-content:center;padding:24px 12px;background:#fff}
.logo-banner{width:50%;text-align:right;flex:none}
.logo-banner img{width:560px;height:auto;max-width:100%;display:inline-block;margin-top:-40px;filter:drop-shadow(0 26px 40px rgba(20,40,80,.14))}
.logo-content{width:400px;max-width:90vw;margin-top:-50px;padding:0 8px}
.logo-content .tips{display:block;font-size:21px;font-weight:700;color:#10141c;line-height:60px;text-align:center}
/* tab */
.tab-bar{display:flex;border-bottom:1px solid #ebeef5;width:300px;max-width:100%;margin:0 auto 22px}
.tab-bar .tab{padding:10px 0;position:relative;color:#606266;font-size:15px;cursor:pointer;margin-right:28px;font-weight:400;user-select:none}
.tab-bar .tab.on{color:#303133;font-weight:600}
.tab-bar .tab.on::after{content:'';position:absolute;left:50%;transform:translateX(-50%);bottom:-1px;width:59px;height:7px;border-radius:4px;background:#d8d8d8}
/* element-style input */
.el-input{display:flex;align-items:center;border:1px solid #dcdfe6;border-radius:4px;height:40px;margin:0 auto 18px;width:300px;max-width:100%;padding:0 11px;background:#fff;transition:border-color .2s}
.el-input:focus-within{border-color:#409eff}
.el-input input{border:0;outline:0;flex:1;font-size:14px;color:#303133;background:transparent;height:100%;min-width:0}
.el-input input::placeholder{color:#c0c4cc}
.el-input .pre{margin-right:9px;color:#c0c4cc;display:flex;flex:none}
.el-input .pre svg{width:15px;height:15px}
.el-input .suf{cursor:pointer;color:#c0c4cc;display:flex;flex:none;margin-left:8px}
.el-input .suf svg{width:16px;height:16px}
/* element-style button */
.el-btn{display:inline-flex;align-items:center;justify-content:center;height:40px;border-radius:4px;font-size:14px;cursor:pointer;border:1px solid #dcdfe6;background:#fff;color:#606266;padding:0 16px;transition:.15s;outline:0;user-select:none}
.el-btn.primary{background:#409eff;border-color:#409eff;color:#fff}
.el-btn.primary:hover{background:#66b1ff;border-color:#66b1ff}
.el-btn.primary:active{background:#3a8ee6}
.el-btn.full{width:300px;max-width:100%;display:flex;margin:0 auto 6px}
.el-btn:disabled{opacity:.6;cursor:not-allowed}
/* QQ 登录按钮 */
.el-btn.qq{margin-top:10px;color:#303133}
.el-btn.qq svg{width:16px;height:16px;margin-right:6px;flex:none}
/* 微信扫码 */
.wx-login{text-align:center}
.wx-login .flex-center{display:flex;justify-content:center}
.wx-login-qrcode{width:192px;height:192px;border-radius:50px;margin:0 auto;overflow:hidden;border:1px solid #ebeef5;background:#fff;display:flex;align-items:center;justify-content:center}
.wx-login-qrcode svg,.wx-login-qrcode img{width:100%;height:100%;display:block}
.wx-status{margin-top:16px;color:#909399;font-size:13px;display:flex;align-items:center;justify-content:center;gap:8px}
.wx-status .dot{width:8px;height:8px;border-radius:50%;background:#e6a23c;animation:wxp 1.2s infinite}
.wx-status.ok .dot{background:#67c23a;animation:none}
@keyframes wxp{0%,100%{opacity:1}50%{opacity:.3}}
.text-center{text-align:center;margin-top:12px}
.el-link{color:#409eff;font-size:13px;cursor:pointer;text-decoration:none;display:inline-flex;align-items:center;gap:5px}
.el-link:hover{color:#66b1ff}
/* 注册 / 找回 */
.view{display:block}
.view.hide{display:none}
.reg-tips{font-size:15px;font-weight:300;color:#10141c;line-height:20px;margin:0 0 16px}
.code-row{display:flex;gap:12px;margin:0 auto 18px;width:300px;max-width:100%}
.code-row .code-input{flex:1;margin:0;min-width:0}
.code-row .code-btn{width:120px;flex:none;height:40px}
/* footer */
.login-foot{margin-top:22px;font-size:14px;color:#606266;text-align:center}
.login-foot a{color:#409eff;cursor:pointer;margin:0 2px;text-decoration:none}
.login-foot a:hover{color:#66b1ff}
.login-copy{margin-top:10px;font-size:12px;color:#909399;text-align:center;line-height:18px}
/* toast */
#lwToast{position:fixed;left:50%;top:64px;transform:translateX(-50%);background:rgba(0,0,0,.8);color:#fff;padding:10px 16px;border-radius:6px;font-size:13px;z-index:9999;display:none;max-width:80vw;white-space:pre-wrap;text-align:center}
/* 移动端 / 响应式 */
@media(max-width:720px){
  .login-container{padding:18px 10px}
  .logo-banner{display:none}
  .logo-content{width:92vw;padding:0 4px;margin-top:0}
  .logo-content .tips{line-height:44px}
  .el-input,.el-btn.full,.tab-bar,.code-row{width:100%}
}
@media(max-width:480px){
  .login-container{padding:14px 10px}
  .logo-content{width:96vw}
  .logo-content .tips{font-size:18px;line-height:36px}
  .el-input{height:42px}
  .el-btn,.el-btn.full{height:42px}
}
</style>
</head>
<body>
<div class="login-container">
  <!-- 右：banner（高仿低代码表单编辑器）-->
  <div class="logo-banner" aria-hidden="true">
    <img src="/assets/banner2.png" alt="大麦网·表单平台" class="banner-img">
  </div>

  <!-- 左：表单 -->
  <div class="logo-content">
    <span class="tips">欢迎使用大麦网！</span>

    <!-- 登录视图 -->
    <div id="view-login" class="view">
      <div class="tab-bar" id="loginTabs">
        <div class="tab on" data-t="account">账号密码登录</div>
      </div>

      <!-- 账号密码 -->
      <div class="view" data-pane="account">
        <div class="el-input">
          <span class="pre"><svg viewBox="0 0 16 16" fill="none"><circle cx="8" cy="5" r="3" stroke="currentColor" stroke-width="1.4"/><path d="M2.5 13a5.5 5.5 0 0 1 11 0" stroke="currentColor" stroke-width="1.4"/></svg></span>
          <input type="text" id="accInput" placeholder="请输入手机号/邮箱" autocomplete="off" onkeydown="if(event.key==='Enter')doLogin()"/>
        </div>
        <div class="el-input">
          <span class="pre"><svg viewBox="0 0 16 16" fill="none"><rect x="3" y="7" width="10" height="7" rx="1.5" stroke="currentColor" stroke-width="1.4"/><path d="M5 7V5a3 3 0 0 1 6 0v2" stroke="currentColor" stroke-width="1.4"/></svg></span>
          <input type="password" id="pwdInput" placeholder="请输入密码" autocomplete="current-password">
          <span class="suf" id="pwdEye" title="显示/隐藏"><svg viewBox="0 0 16 16" fill="none"><path d="M1.5 8S3.5 4.5 8 4.5 14.5 8 14.5 8 12.5 11.5 8 11.5 1.5 8 1.5 8Z" stroke="currentColor" stroke-width="1.3"/><circle cx="8" cy="8" r="2" stroke="currentColor" stroke-width="1.3"/></svg></span>
        </div>
        <button class="el-btn primary full" id="loginBtn" type="button" onclick="doLogin()">登 录</button>
        <button class="el-btn full qq" id="qqBtn" type="button" onclick="qqLogin()">
          <svg viewBox="0 0 24 24" width="16" height="16"><circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" stroke-width="1.6"/><text x="12" y="15.5" font-size="8" text-anchor="middle" fill="currentColor" font-family="Arial">QQ</text></svg>
          <span>QQ 登录</span>
        </button>
        <div class="text-center"><a class="el-link" onclick="goForget()">忘记密码？</a></div>
      </div>

    </div>

    <!-- 注册视图 -->
    <div id="view-register" class="view hide">
      <p class="reg-tips">注册成为大麦网用户~</p>
      <div class="tab-bar" id="regTabs">
        <div class="tab on" data-r="email">邮箱注册</div>
        <div class="tab" data-r="phone">手机注册</div>
      </div>
      <div class="el-input">
        <span class="pre" id="regAccIcon"></span>
        <input type="text" id="regAccount" placeholder="请输入邮箱" autocomplete="off">
      </div>
      <div class="el-input">
        <span class="pre"><svg viewBox="0 0 16 16" fill="none"><rect x="3" y="7" width="10" height="7" rx="1.5" stroke="currentColor" stroke-width="1.4"/><path d="M5 7V5a3 3 0 0 1 6 0v2" stroke="currentColor" stroke-width="1.4"/></svg></span>
        <input type="password" id="regPwd" placeholder="请输入密码" autocomplete="new-password">
      </div>
      <div class="code-row">
        <div class="el-input code-input">
          <span class="pre"><svg viewBox="0 0 16 16" fill="none"><path d="M8 1.5 13 3.5v4c0 3-2 5.5-5 7-3-1.5-5-4-5-7v-4L8 1.5Z" stroke="currentColor" stroke-width="1.3"/></svg></span>
          <input type="text" id="regCode" placeholder="请输入验证码" maxlength="4" oninput="this.value=this.value.replace(/[^0-9]/g,'')">
        </div>
        <button class="el-btn primary code-btn" id="sendCode" type="button" onclick="sendCode()">发送验证码</button>
      </div>
      <button class="el-btn primary full" id="regBtn" type="button" onclick="submitReg()">确 定</button>
    </div>

    <!-- 找回密码视图 -->
    <div id="view-forget" class="view hide">
      <p class="reg-tips">找回密码</p>
      <div class="tab-bar" id="fgTabs">
        <div class="tab on" data-f="email">邮箱找回</div>
        <div class="tab" data-f="phone">手机找回</div>
      </div>

      <!-- 步骤 1：账号 + 发送验证 -->
      <div class="view" data-fstep="1">
        <div class="el-input">
          <span class="pre" id="fgAccIcon"></span>
          <input type="text" id="fgAccount" placeholder="请输入邮箱" autocomplete="off">
        </div>
        <button class="el-btn primary full" id="fgSendBtn" type="button" onclick="fgSend()">发送验证</button>
      </div>

      <!-- 步骤 2：验证码 + 新密码 -->
      <div class="view hide" data-fstep="2">
        <div class="el-input">
          <span class="pre"><svg viewBox="0 0 16 16" fill="none"><path d="M8 1.5 13 3.5v4c0 3-2 5.5-5 7-3-1.5-5-4-5-7v-4L8 1.5Z" stroke="currentColor" stroke-width="1.3"/></svg></span>
          <input type="text" id="fgCode" placeholder="请输入验证码" maxlength="6" oninput="this.value=this.value.replace(/[^0-9]/g,'')">
        </div>
        <div class="el-input">
          <span class="pre"><svg viewBox="0 0 16 16" fill="none"><rect x="3" y="7" width="10" height="7" rx="1.5" stroke="currentColor" stroke-width="1.4"/><path d="M5 7V5a3 3 0 0 1 6 0v2" stroke="currentColor" stroke-width="1.4"/></svg></span>
          <input type="password" id="fgPwd" placeholder="请输入新密码" autocomplete="new-password">
        </div>
        <div class="el-input">
          <span class="pre"><svg viewBox="0 0 16 16" fill="none"><rect x="3" y="7" width="10" height="7" rx="1.5" stroke="currentColor" stroke-width="1.4"/><path d="M5 7V5a3 3 0 0 1 6 0v2" stroke="currentColor" stroke-width="1.4"/></svg></span>
          <input type="password" id="fgPwd2" placeholder="请再次输入新密码" autocomplete="new-password">
        </div>
        <button class="el-btn primary full" id="fgSubmitBtn" type="button" onclick="fgSubmit()">重置密码</button>
      </div>

      <div class="text-center"><a class="el-link" onclick="goLogin()">返回登录</a></div>
    </div>

    <div class="login-foot" id="loginFoot">没有账号？<a onclick="goRegister()">注册</a></div>
    <div class="login-copy" id="loginCopy"></div>
  </div>
</div>

<script>
(function(){
'use strict';
function el(id){return document.getElementById(id);}
function esc(s){return String(s==null?'':s).replace(/[&<>"']/g,function(c){return{'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c];});}
function toast(m){var d=el('lwToast');if(!d){d=document.createElement('div');d.id='lwToast';document.body.appendChild(d);}d.textContent=String(m||'');d.style.display='block';clearTimeout(d._t);d._t=setTimeout(function(){d.style.display='none';},2600);}
function rnd(n){var A='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',s='';n=n||12;for(var i=0;i<n;i++)s+=A.charAt(Math.floor(Math.random()*A.length));return s;}

/* ===== api（串行 fetch，返回 .data；错误保留 code/http）===== */
var _q=Promise.resolve();
function _ser(run){var p=_q.then(run,run);_q=p.then(function(){},function(){});return p;}
function api(path){return _ser(async function(){var r=await fetch('/'+path,{headers:{Accept:'application/json'}});var j=await r.json().catch(function(){return null;});if(!j)throw{code:r.status,http:r.status,msg:'服务无响应',data:null};if(j.code!==200)throw{code:j.code,http:r.status,msg:j.msg||('code '+j.code),data:j.data};return j.data;});}
function postApi(path,body){return _ser(async function(){var r=await fetch('/'+path,{method:'POST',headers:{Accept:'application/json','Content-Type':'application/json'},body:JSON.stringify(body||{})});var j=await r.json().catch(function(){return null;});if(!j)throw{code:r.status,http:r.status,msg:'服务无响应',data:null};if(j.code!==200)throw{code:j.code,http:r.status,msg:j.msg||('code '+j.code),data:j.data};return j.data;});}
function errMsg(e){return(e&&(e.msg||e.message))?e.msg||e.message:'网络异常，请稍后重试';}

/* 账号正则：邮箱尾 {2,6} 或手机 (0|86|+86)?1[3-9]\d{9} */
var ACCOUNT_RE=/^(?:[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}|(0|86|\+86)?1[3-9]\d{9})$/;
var PHONE_RE=/^1\d{10}$/;
var EMAIL_RE=/^[^@\s]+@[^@\s]+\.[^@\s]+$/;

/* 图标 */
var ICS={user:'<svg viewBox="0 0 16 16" fill="none"><circle cx="8" cy="5" r="3" stroke="currentColor" stroke-width="1.4"/><path d="M2.5 13a5.5 5.5 0 0 1 11 0" stroke="currentColor" stroke-width="1.4"/></svg>',
lock:'<svg viewBox="0 0 16 16" fill="none"><rect x="3" y="7" width="10" height="7" rx="1.5" stroke="currentColor" stroke-width="1.4"/><path d="M5 7V5a3 3 0 0 1 6 0v2" stroke="currentColor" stroke-width="1.4"/></svg>',
mail:'<svg viewBox="0 0 16 16" fill="none"><rect x="2" y="3" width="12" height="10" rx="1.6" stroke="currentColor" stroke-width="1.3"/><path d="m2.5 4 5.5 4 5.5-4" stroke="currentColor" stroke-width="1.3"/></svg>',
phone:'<svg viewBox="0 0 16 16" fill="none"><rect x="4" y="2" width="8" height="12" rx="1.6" stroke="currentColor" stroke-width="1.4"/><line x1="7" y1="12.5" x2="9" y2="12.5" stroke="currentColor" stroke-width="1.4"/></svg>'};
function qrSvg(fg,bg,s){s=s||'';var c1=fg||'#303133',c2=bg||'#fff';var t=esc(s).slice(0,6)||'DM';return '<svg viewBox="0 0 9 9" style="background:'+c2+'"><rect width="9" height="9" fill="'+c2+'"/><g fill="'+c1+'"><rect x="0" y="0" width="3" height="3"/><rect x="6" y="0" width="3" height="3"/><rect x="0" y="6" width="3" height="3"/><rect x="1" y="1" width="1" height="1" fill="'+c2+'"/><rect x="7" y="1" width="1" height="1" fill="'+c2+'"/><rect x="1" y="7" width="1" height="1" fill="'+c2+'"/><rect x="4" y="4" width="1" height="1"/><rect x="6" y="5" width="1" height="1"/><rect x="4" y="6" width="1" height="1"/></g><text x="4.5" y="5" font-size="1.1" fill="'+c1+'" text-anchor="middle" font-family="monospace">'+t+'</text></svg>';}

/* ===== 视图 / tab ===== */
function setView(v){
  el('view-login').classList.toggle('hide',v!=='login');
  el('view-register').classList.toggle('hide',v!=='register');
  el('view-forget').classList.toggle('hide',v!=='forget');
  el('loginFoot').style.display=(v==='login')?'':'none';
  if(v==='register'){var f=document.querySelector('#regTabs .tab.on');showRegTab(f?f.getAttribute('data-r'):'email');}
  if(v==='forget'){var f2=document.querySelector('#fgTabs .tab.on');showFgTab(f2?f2.getAttribute('data-f'):'email');}
}
function goRegister(){setView('register');}
function goLogin(){setView('login');}
function goForget(){setView('forget');}
function showLoginTab(name){
  var tabs=el('loginTabs').children;
  for(var i=0;i<tabs.length;i++)tabs[i].classList.toggle('on',tabs[i].getAttribute('data-t')===name);
  document.querySelectorAll('#view-login [data-pane]').forEach(function(p){p.classList.toggle('hide',p.getAttribute('data-pane')!==name);});
  if(name==='wx'){wxLoad();wxStart();}else{wxStop();}
}
function showRegTab(mode){
  regMode=mode;
  var tabs=el('regTabs').children;
  for(var i=0;i<tabs.length;i++)tabs[i].classList.toggle('on',tabs[i].getAttribute('data-r')===mode);
  el('regAccIcon').innerHTML=mode==='phone'?ICS.phone:ICS.mail;
  el('regAccount').placeholder=mode==='phone'?'请输入手机号':'请输入邮箱';
  el('regAccount').value='';
}
function showFgTab(mode){
  fgMode=mode;
  var tabs=el('fgTabs').children;
  for(var i=0;i<tabs.length;i++)tabs[i].classList.toggle('on',tabs[i].getAttribute('data-f')===mode);
  el('fgAccIcon').innerHTML=mode==='phone'?ICS.phone:ICS.mail;
  el('fgAccount').placeholder=mode==='phone'?'请输入手机号':'请输入邮箱';
  el('fgAccount').value='';
  fgShowStep(1);
  clearInterval(fgCdTimer);
  var b=el('fgSendBtn');b.disabled=false;b.textContent='发送验证';
}
window.showLoginTab=showLoginTab;
window.goRegister=goRegister;
window.goLogin=goLogin;
window.goForget=goForget;
Array.prototype.forEach.call(el('loginTabs').children,function(t){t.onclick=function(){showLoginTab(t.getAttribute('data-t'));};});
Array.prototype.forEach.call(el('regTabs').children,function(t){t.onclick=function(){showRegTab(t.getAttribute('data-r'));};});
Array.prototype.forEach.call(el('fgTabs').children,function(t){t.onclick=function(){showFgTab(t.getAttribute('data-f'));};});

/* ===== 微信扫码登录（{qrCodeUrl,loginId}；5s 轮询；300s 刷新）===== */
var wxTimer=null,wxRefreshTimer=null,wxBusy=false,wxLoginId='';
function wxStatus(text,ok){var b=el('wxStatus');b.classList.toggle('ok',!!ok);el('wxStatusText').textContent=text;}
function wxLoad(){
  if(wxBusy)return;wxBusy=true;
  api('api/login/wx/qrcode').then(function(d){
    d=d||{};
    wxLoginId=d.loginId||d.qrcodeKey||'';
    var url=d.qrCodeUrl||d.url||'';
    var box=el('wxQrImg');
    if(url){box.innerHTML='<img src="'+esc(url)+'" alt="微信扫码登录">';}
    else{box.innerHTML=qrSvg('#303133','#fff','wx'+rnd(6));}
    wxStatus('等待扫码…');
  }).catch(function(e){
    el('wxQrImg').innerHTML=qrSvg('#303133','#fff','wx'+rnd(6));
    wxStatus('二维码加载失败，请点击刷新');
  }).finally(function(){wxBusy=false;});
}
function wxPoll(){
  if(!wxLoginId){wxStatus('等待扫码…');return;}
  api('api/login/wx/qrcode/result?loginId='+encodeURIComponent(wxLoginId)).then(function(d){
    d=d||{};
    if(d.status==='success'&&d.token){wxStop();saveTok(d);toast('登录成功');setTimeout(goAdmin,450);}
    else if(d.status==='expired'){wxStatus('二维码已过期，正在刷新…');wxLoad();}
    else{wxStatus('等待扫码…');}
  }).catch(function(e){wxStatus('扫码结果查询失败');});
}
function wxStart(){wxStop();wxTimer=setInterval(wxPoll,5000);wxRefreshTimer=setInterval(wxLoad,300000);}
function wxStop(){if(wxTimer){clearInterval(wxTimer);wxTimer=null;}if(wxRefreshTimer){clearInterval(wxRefreshTimer);wxRefreshTimer=null;}}

/* ===== QQ 登录（authorize/url 跳转 + login/qq 回调）===== */
function qqRedirectUri(){return location.origin+'/index.html';}
window.qqLogin=function qqLogin(){
  api('api/login/qq/authorize/url?redirectUri='+encodeURIComponent(qqRedirectUri())).then(function(d){
    var url=(d&&(d.url||d))||'';
    if(url){location.href=url;}else{toast('QQ 登录地址获取失败');}
  }).catch(function(e){toast(errMsg(e));});
};
function qqCallback(){
  var q=new URLSearchParams(location.search);
  var code=q.get('authorizeCode')||q.get('code');
  if(!code)return;
  postApi('api/login/qq',{authorizeCode:code,redirectUri:qqRedirectUri()}).then(function(d){
    saveTok(d);toast('登录成功');setTimeout(goAdmin,450);
  }).catch(function(e){toast(errMsg(e));});
}

/* ===== 账号密码登录（body 仅 {account,password}）===== */
var loginBusy=false;
function saveTok(d){var t=d&&(d.token||d.accessToken);if(t){try{localStorage.setItem('ACCESS_TOKEN',t);}catch(e){}}}
function goAdmin(){
  var r='';
  try{r=new URLSearchParams(location.search).get('redirect')||'';}catch(e){}
  location.href=r||'admin.html';
}
window.doLogin=function doLogin(){
  var account=el('accInput').value.trim(),password=el('pwdInput').value;
  if(!account){toast('请输入手机号/邮箱');return;}
  if(!password){toast('请输入密码');return;}
  if(!ACCOUNT_RE.test(account)){toast('请输入正确的手机号或邮箱');return;}
  if(loginBusy)return;loginBusy=true;var b=el('loginBtn');b.disabled=true;b.textContent='登录中…';
  postApi('api/login/account',{account:account,password:password}).then(function(d){
    saveTok(d);toast('登录成功');setTimeout(goAdmin,450);
  }).catch(function(e){toast(errMsg(e));})
  .finally(function(){loginBusy=false;b.disabled=false;b.textContent='登 录';});
};

/* ===== 注册（邮箱 / 手机）===== */
var regMode='email',regBusy=false,cdTimer=null,cdN=0;
window.sendCode=function sendCode(){
  var acc=el('regAccount').value.trim();
  if(regMode==='phone'){if(!PHONE_RE.test(acc)){toast('请输入正确的手机号');return;}}
  else{if(!EMAIL_RE.test(acc)){toast('请输入正确的邮箱');return;}}
  var btn=el('sendCode');if(btn.disabled)return;
  api(regMode==='phone'?('api/register/phone/code?phoneNumber='+encodeURIComponent(acc)):('api/register/email/code?email='+encodeURIComponent(acc))).then(function(){
    toast('验证码已发送，5 分钟内有效');
    cdN=60;clearInterval(cdTimer);btn.disabled=true;btn.textContent='60s 后重发';
    cdTimer=setInterval(function(){cdN--;if(cdN<=0){clearInterval(cdTimer);btn.disabled=false;btn.textContent='发送验证码';}else{btn.textContent=cdN+'s 后重发';}},1000);
  }).catch(function(e){toast(errMsg(e));});
};
window.submitReg=function submitReg(){
  var acc=el('regAccount').value.trim(),pwd=el('regPwd').value,code=el('regCode').value.trim();
  if(regMode==='phone'){if(!PHONE_RE.test(acc)){toast('请输入正确的手机号');return;}}
  else{if(!EMAIL_RE.test(acc)){toast('请输入正确的邮箱');return;}}
  if(!pwd||pwd.length<6){toast('密码至少 6 位');return;}
  if(!code){toast('请输入验证码');return;}
  if(regBusy)return;regBusy=true;var b=el('regBtn');b.disabled=true;b.textContent='提交中…';
  postApi(regMode==='phone'?'api/register/phone':'api/register/email',regMode==='phone'?{phoneNumber:acc,password:pwd,code:code}:{email:acc,password:pwd,code:code})
  .then(function(){toast('注册成功，快去登录吧');el('accInput').value=acc;goLogin();showLoginTab('account');})
  .catch(function(e){toast(errMsg(e));})
  .finally(function(){regBusy=false;b.disabled=false;b.textContent='确 定';});
};

/* ===== 找回密码（邮箱 / 手机）===== */
var fgMode='email',fgBusy=false,fgAccount='',fgCdTimer=null,fgCdN=0;
function fgShowStep(n){
  document.querySelectorAll('#view-forget [data-fstep]').forEach(function(p){p.classList.toggle('hide',p.getAttribute('data-fstep')!==String(n));});
}
window.fgSend=function fgSend(){
  var acc=el('fgAccount').value.trim();
  if(fgMode==='phone'){if(!PHONE_RE.test(acc)){toast('请输入正确的手机号');return;}}
  else{if(!EMAIL_RE.test(acc)){toast('请输入正确的邮箱');return;}}
  var btn=el('fgSendBtn');if(btn.disabled)return;btn.disabled=true;btn.textContent='发送中…';
  var url=fgMode==='phone'?('api/retrieve/password/phone/code?phoneNumber='+encodeURIComponent(acc)):('api/retrieve/password/email?email='+encodeURIComponent(acc));
  api(url).then(function(){
    fgAccount=acc;
    toast(fgMode==='phone'?'验证码已发送，5 分钟内有效':'重置邮件已发送，请查收');
    fgShowStep(2);
    var b=el('fgSendBtn');
    if(fgMode==='phone'){fgCdN=60;clearInterval(fgCdTimer);b.disabled=true;b.textContent='60s 后重发';fgCdTimer=setInterval(function(){fgCdN--;if(fgCdN<=0){clearInterval(fgCdTimer);b.disabled=false;b.textContent='重新发送';}else{b.textContent=fgCdN+'s 后重发';}},1000);}
    else{b.disabled=false;b.textContent='重新发送';}
  }).catch(function(e){toast(errMsg(e));btn.disabled=false;btn.textContent='发送验证';});
};
window.fgSubmit=function fgSubmit(){
  var code=el('fgCode').value.trim(),pwd=el('fgPwd').value,rp=el('fgPwd2').value;
  if(!code){toast('请输入验证码');return;}
  if(!pwd||pwd.length<6){toast('密码至少 6 位');return;}
  if(pwd!==rp){toast('两次输入的密码不一致');return;}
  if(fgBusy)return;fgBusy=true;var b=el('fgSubmitBtn');b.disabled=true;b.textContent='提交中…';
  function doReset(c){
    postApi('api/retrieve/password/reset',{code:c,password:pwd,rePassword:rp}).then(function(){
      toast('密码重置成功，快去登录吧');setTimeout(function(){goLogin();showLoginTab('account');},1200);
    }).catch(function(e){toast(errMsg(e));}).finally(function(){fgBusy=false;b.disabled=false;b.textContent='重置密码';});
  }
  if(fgMode==='phone'){
    postApi('api/retrieve/password/check/phone-code',{phoneNumber:fgAccount,code:code}).then(function(d){
      var c=code;
      if(typeof d==='string'){c=d;}
      else if(d&&(d.code||d.token)){c=d.code||d.token;}
      doReset(c);
    }).catch(function(e){toast(errMsg(e));fgBusy=false;b.disabled=false;b.textContent='重置密码';});
  }else{doReset(code);}
};

/* ===== init ===== */
el('regAccIcon').innerHTML=ICS.mail;
el('fgAccIcon').innerHTML=ICS.mail;
el('pwdEye').onclick=function(){var i=el('pwdInput');i.type=i.type==='password'?'text':'password';};
qqCallback();
initConfig();

/* 拉取系统配置：openWxMpLogin 控制微信 tab 显隐，copyright 进页脚 */
function initConfig(){
  api('api/public/systemInfoConfig').then(function(d){
    var cfg=d;
    if(typeof cfg==='string'){try{cfg=JSON.parse(cfg);}catch(e){cfg=null;}}
    cfg=cfg||{};
    el('loginCopy').innerHTML=cfg.copyright||'';
    /* 微信扫码登录已下线：登录页无微信 tab/面板，始终账号密码登录（不引用已移除的 wx 元素，避免加载即抛 TypeError） */
    showLoginTab('account');
  }).catch(function(e){
    el('loginCopy').innerHTML='';
    showLoginTab('account');
  });
}
})();
</script>
</body>
</html>
