| <!DOCTYPE html> |
| <html lang="zh-CN"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>注册</title> |
| </head> |
| <body> |
| <h2>用户注册</h2> |
| <form id="regForm"> |
| <div> |
| <label>用户名:</label> |
| <input type="text" name="username" required> |
| </div> |
| <div> |
| <label>邮箱:</label> |
| <input type="email" name="email" required> |
| <button type="button" id="sendCode">发送验证码</button> |
| </div> |
| <div> |
| <label>邮箱验证码:</label> |
| <input type="text" name="verificationCode" required> |
| </div> |
| <div> |
| <label>密码:</label> |
| <input type="password" name="password" required> |
| </div> |
| <div> |
| <label>身份证号(8 位):</label> |
| <input type="text" name="identificationNumber" required pattern="\d{8}" maxlength="8"> |
| </div> |
| <button type="submit">注册</button> |
| </form> |
| |
| <script> |
| // 发送注册验证码 |
| document.getElementById('sendCode').addEventListener('click', async () => { |
| const email = document.querySelector('[name=email]').value; |
| if (!email) { alert('请先输入邮箱'); return; } |
| const res = await fetch('/sendVerification', { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify({ email }) |
| }); |
| const json = await res.json(); |
| alert(json.msg || json.error); |
| }); |
| |
| // 提交注册 |
| document.getElementById('regForm').addEventListener('submit', async e => { |
| e.preventDefault(); |
| const data = { |
| username: e.target.username.value, |
| email: e.target.email.value, |
| verificationCode: e.target.verificationCode.value, |
| password: e.target.password.value, |
| identificationNumber: e.target.identificationNumber.value |
| }; |
| const res = await fetch('/register', { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify(data) |
| }); |
| const json = await res.json(); |
| alert(json.msg || json.error); |
| if (res.ok && json.code === 0) { |
| window.location.href = 'login.html'; |
| } |
| }); |
| </script> |
| </body> |
| </html> |