Files
MC_Report/public/js/pages/register.js

92 lines
5.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const RegisterPage = {
captcha: null,
async render() {
try { this.captcha = await API.get('/captcha'); } catch { this.captcha = null; }
return `
<div class="pub-card wide">
<div class="logo"><i class="fas fa-user-plus"></i><h2>创建账号</h2><p>加入MC举报系统</p></div>
<form id="reg-form">
<div class="grid-2">
<div class="form-group"><label>站点用户名 *</label><input id="ru" required placeholder="登录用"></div>
<div class="form-group"><label>邮箱 *</label><input type="email" id="re" required placeholder="接收通知"></div>
</div>
<div class="form-group"><label>账号来源</label><select id="rs" onchange="RegisterPage.toggleSource()">
<option value="netease">网易端</option><option value="skin">皮肤站</option>
</select></div>
<div class="grid-2">
<div class="form-group"><label id="rs-label1">游戏名称 *</label><input id="rgn" required placeholder="Minecraft ID/网易昵称"></div>
<div class="form-group"><label id="rs-label2">网易UID *</label><input id="rgu" required placeholder="UID"></div>
</div>
<div class="form-group"><label>密码 *</label><input type="password" id="rp" required placeholder="至少6位"></div>
${this.captcha ? `
<div class="form-group"><label>验证码</label>
<div style="display:flex;align-items:center;gap:10px">
<div id="captcha-img" style="flex-shrink:0;border:1px solid var(--g200);border-radius:var(--r);overflow:hidden;cursor:pointer" onclick="RegisterPage.refreshCaptcha()" title="点击刷新">${this.captcha.svg}</div>
<input type="text" id="captcha-a" placeholder="输入图片字符" required style="max-width:100px">
<input type="hidden" id="captcha-id" value="${this.captcha.id}">
</div>
<div class="help">点击图片刷新验证码</div>
</div>` : ''}
<div id="regerr" class="alert alert-e hidden"></div>
<div id="regok" class="alert alert-s hidden"></div>
<button type="submit" class="btn btn-p btn-lg w-full"><i class="fas fa-user-plus"></i> 注册</button>
</form>
<div style="margin-top:14px;text-align:center;font-size:13px">
<a href="#/login" style="color:var(--p);text-decoration:none;font-weight:600">已有账号?登录</a>
<span style="color:var(--g4);margin:0 10px">|</span>
<a href="#/home" style="color:var(--g5);text-decoration:none">匿名提交</a>
</div>
</div>`;
},
toggleSource() {
const src = document.getElementById('rs').value;
const labels = { netease:['游戏名称','网易UID'], skin:['游戏名称','皮肤站UID'] };
document.getElementById('rs-label1').textContent = labels[src][0] + ' *';
document.getElementById('rs-label2').textContent = labels[src][1] + ' *';
},
async refreshCaptcha() {
try { const c = await API.get('/captcha'); document.getElementById('captcha-img').innerHTML = c.svg; document.getElementById('captcha-id').value = c.id; document.getElementById('captcha-a').value = ''; } catch {}
},
mount() {
document.getElementById('reg-form').addEventListener('submit', async e => {
e.preventDefault();
const btn = e.target.querySelector('button');
const err = document.getElementById('regerr');
const ok = document.getElementById('regok');
err.classList.add('hidden'); ok.classList.add('hidden');
btn.disabled = true;
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 注册中...';
try {
await Auth.register({
username: document.getElementById('ru').value.trim(),
password: document.getElementById('rp').value,
email: document.getElementById('re').value.trim(),
game_name: document.getElementById('rgn').value.trim(),
game_uid: document.getElementById('rgu').value.trim(),
source: document.getElementById('rs').value,
captcha_id: document.getElementById('captcha-id')?.value,
captcha_answer: document.getElementById('captcha-a')?.value.trim(),
});
ok.textContent = '注册成功验证码已发送至邮箱30分钟内有效';
ok.classList.remove('hidden');
e.target.innerHTML = `<div class="form-group"><label>邮箱验证码</label>
<div style="display:flex;gap:8px"><input id="vf-code" placeholder="6位验证码" maxlength="6" pattern="[0-9]{6}" style="flex:1"><button type="button" class="btn btn-s btn-sm" id="vf-btn" style="white-space:nowrap">验证</button></div>
</div>`;
document.getElementById('vf-btn').onclick = async () => {
const code = document.getElementById('vf-code').value.trim();
if (!code) return;
try { await API.post('/auth/verify-email', { code }); alert('验证成功!请登录'); location.hash = '#/login'; }
catch(ex) { alert(ex.message); }
};
} catch (ex) {
err.textContent = ex.message; err.classList.remove('hidden');
btn.disabled = false; btn.innerHTML = '<i class="fas fa-user-plus"></i> 注册';
}
});
}
};