|
@@ -2,13 +2,14 @@
|
|
|
const KEY = 'pouchlab.user.v1';
|
|
const KEY = 'pouchlab.user.v1';
|
|
|
let user = null;
|
|
let user = null;
|
|
|
let pending = null;
|
|
let pending = null;
|
|
|
|
|
+ let mode = 'email'; // email | password | phone
|
|
|
let countdown = 0;
|
|
let countdown = 0;
|
|
|
let countdownTimer = null;
|
|
let countdownTimer = null;
|
|
|
const listeners = [];
|
|
const listeners = [];
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
user = JSON.parse(localStorage.getItem(KEY) || 'null');
|
|
user = JSON.parse(localStorage.getItem(KEY) || 'null');
|
|
|
- if (user && !user.phone) user = null;
|
|
|
|
|
|
|
+ if (user && !user.phone && !user.email) user = null;
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
user = null;
|
|
user = null;
|
|
|
}
|
|
}
|
|
@@ -35,26 +36,60 @@
|
|
|
return p.slice(0, 3) + '****' + p.slice(7);
|
|
return p.slice(0, 3) + '****' + p.slice(7);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ function validEmail(v) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v); }
|
|
|
|
|
+
|
|
|
|
|
+ const STYLE = '<style>' +
|
|
|
|
|
+ '.pl-auth-tabs{display:flex;gap:6px;margin:0 0 14px}' +
|
|
|
|
|
+ '.pl-auth-tabs button{flex:1;padding:8px 4px;font-size:13px;border:1px solid #d8cfc0;border-radius:8px;background:#fff;color:#7d7466;cursor:pointer}' +
|
|
|
|
|
+ '.pl-auth-tabs button.on{border-color:#b08d57;color:#5a4a2f;background:#f7efdf;font-weight:600}' +
|
|
|
|
|
+ '.pl-auth-menu button{display:block;width:100%;margin-top:6px}' +
|
|
|
|
|
+ '</style>';
|
|
|
|
|
+
|
|
|
function ensureModal() {
|
|
function ensureModal() {
|
|
|
if (document.getElementById('plAuthModal')) return;
|
|
if (document.getElementById('plAuthModal')) return;
|
|
|
const wrap = document.createElement('div');
|
|
const wrap = document.createElement('div');
|
|
|
wrap.id = 'plAuthModal';
|
|
wrap.id = 'plAuthModal';
|
|
|
wrap.className = 'pl-auth-mask';
|
|
wrap.className = 'pl-auth-mask';
|
|
|
wrap.hidden = true;
|
|
wrap.hidden = true;
|
|
|
- wrap.innerHTML =
|
|
|
|
|
|
|
+ wrap.innerHTML = STYLE +
|
|
|
'<div class="pl-auth-card" role="dialog" aria-modal="true" aria-labelledby="plAuthTitle">' +
|
|
'<div class="pl-auth-card" role="dialog" aria-modal="true" aria-labelledby="plAuthTitle">' +
|
|
|
'<button class="pl-auth-close" type="button" aria-label="关闭">✕</button>' +
|
|
'<button class="pl-auth-close" type="button" aria-label="关闭">✕</button>' +
|
|
|
'<div class="pl-auth-brand">袋研社 <em>POUCHLAB</em></div>' +
|
|
'<div class="pl-auth-brand">袋研社 <em>POUCHLAB</em></div>' +
|
|
|
'<h2 id="plAuthTitle">登录 / 注册</h2>' +
|
|
'<h2 id="plAuthTitle">登录 / 注册</h2>' +
|
|
|
'<p class="pl-auth-sub">登录后可保存设计、同步订单进度与下载记录。</p>' +
|
|
'<p class="pl-auth-sub">登录后可保存设计、同步订单进度与下载记录。</p>' +
|
|
|
- '<label class="pl-auth-label" for="plAuthPhone">手机号</label>' +
|
|
|
|
|
- '<input id="plAuthPhone" class="pl-auth-input" type="tel" inputmode="numeric" maxlength="11" autocomplete="tel" placeholder="请输入 11 位手机号">' +
|
|
|
|
|
- '<label class="pl-auth-label" for="plAuthCode">验证码</label>' +
|
|
|
|
|
- '<div class="pl-auth-code-row">' +
|
|
|
|
|
- '<input id="plAuthCode" class="pl-auth-input" type="text" inputmode="numeric" maxlength="6" autocomplete="one-time-code" placeholder="6 位验证码">' +
|
|
|
|
|
- '<button id="plAuthSend" class="pl-auth-send" type="button">获取验证码</button>' +
|
|
|
|
|
|
|
+ '<div class="pl-auth-tabs">' +
|
|
|
|
|
+ '<button type="button" data-mode="email" class="on">邮箱登录</button>' +
|
|
|
|
|
+ '<button type="button" data-mode="password">密码登录</button>' +
|
|
|
|
|
+ '<button type="button" data-mode="phone">手机号(旧)</button>' +
|
|
|
|
|
+ '</div>' +
|
|
|
|
|
+ // —— 邮箱验证码 ——
|
|
|
|
|
+ '<div id="plSecEmail">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plAuthEmail">邮箱</label>' +
|
|
|
|
|
+ '<input id="plAuthEmail" class="pl-auth-input" type="email" maxlength="120" autocomplete="email" placeholder="name@example.com">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plAuthCode">验证码</label>' +
|
|
|
|
|
+ '<div class="pl-auth-code-row">' +
|
|
|
|
|
+ '<input id="plAuthCode" class="pl-auth-input" type="text" inputmode="numeric" maxlength="6" autocomplete="one-time-code" placeholder="6 位验证码">' +
|
|
|
|
|
+ '<button id="plAuthSend" class="pl-auth-send" type="button">获取验证码</button>' +
|
|
|
|
|
+ '</div>' +
|
|
|
|
|
+ '</div>' +
|
|
|
|
|
+ // —— 邮箱密码 ——
|
|
|
|
|
+ '<div id="plSecPwd" hidden>' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plPwdEmail">邮箱</label>' +
|
|
|
|
|
+ '<input id="plPwdEmail" class="pl-auth-input" type="email" maxlength="120" autocomplete="email" placeholder="name@example.com">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plPwdPass">密码</label>' +
|
|
|
|
|
+ '<input id="plPwdPass" class="pl-auth-input" type="password" maxlength="64" autocomplete="current-password" placeholder="登录密码">' +
|
|
|
|
|
+ '</div>' +
|
|
|
|
|
+ // —— legacy 手机号 ——
|
|
|
|
|
+ '<div id="plSecPhone" hidden>' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plAuthPhone">手机号</label>' +
|
|
|
|
|
+ '<input id="plAuthPhone" class="pl-auth-input" type="tel" inputmode="numeric" maxlength="11" autocomplete="tel" placeholder="请输入 11 位手机号">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plPhoneCode">验证码</label>' +
|
|
|
|
|
+ '<div class="pl-auth-code-row">' +
|
|
|
|
|
+ '<input id="plPhoneCode" class="pl-auth-input" type="text" inputmode="numeric" maxlength="6" autocomplete="one-time-code" placeholder="6 位验证码">' +
|
|
|
|
|
+ '<button id="plPhoneSend" class="pl-auth-send" type="button">获取验证码</button>' +
|
|
|
|
|
+ '</div>' +
|
|
|
'</div>' +
|
|
'</div>' +
|
|
|
- '<p class="pl-auth-hint" id="plAuthHint">输入手机号获取验证码登录;演示环境验证码会直接弹出显示。</p>' +
|
|
|
|
|
|
|
+ '<p class="pl-auth-hint" id="plAuthHint">输入邮箱获取验证码登录;演示环境验证码会直接弹出显示。</p>' +
|
|
|
'<p class="pl-auth-error" id="plAuthError" hidden></p>' +
|
|
'<p class="pl-auth-error" id="plAuthError" hidden></p>' +
|
|
|
'<button id="plAuthSubmit" class="pl-auth-submit" type="button">登录</button>' +
|
|
'<button id="plAuthSubmit" class="pl-auth-submit" type="button">登录</button>' +
|
|
|
'<p class="pl-auth-terms">登录即表示同意《服务条款》与《隐私政策》</p>' +
|
|
'<p class="pl-auth-terms">登录即表示同意《服务条款》与《隐私政策》</p>' +
|
|
@@ -65,29 +100,44 @@
|
|
|
wrap.addEventListener('click', function (e) {
|
|
wrap.addEventListener('click', function (e) {
|
|
|
if (e.target === wrap) close();
|
|
if (e.target === wrap) close();
|
|
|
});
|
|
});
|
|
|
- wrap.querySelector('#plAuthSend').addEventListener('click', sendCode);
|
|
|
|
|
|
|
+ wrap.querySelectorAll('.pl-auth-tabs button').forEach(function (b) {
|
|
|
|
|
+ b.addEventListener('click', function () { setMode(b.dataset.mode); });
|
|
|
|
|
+ });
|
|
|
|
|
+ wrap.querySelector('#plAuthSend').addEventListener('click', function () { sendCode('email'); });
|
|
|
|
|
+ wrap.querySelector('#plPhoneSend').addEventListener('click', function () { sendCode('phone'); });
|
|
|
wrap.querySelector('#plAuthSubmit').addEventListener('click', submit);
|
|
wrap.querySelector('#plAuthSubmit').addEventListener('click', submit);
|
|
|
wrap.querySelector('#plAuthPhone').addEventListener('input', function () {
|
|
wrap.querySelector('#plAuthPhone').addEventListener('input', function () {
|
|
|
- this.value = this.value.replace(/\D/g, '').slice(0, 11);
|
|
|
|
|
- hideError();
|
|
|
|
|
- });
|
|
|
|
|
- wrap.querySelector('#plAuthCode').addEventListener('input', function () {
|
|
|
|
|
- this.value = this.value.replace(/\D/g, '').slice(0, 6);
|
|
|
|
|
- hideError();
|
|
|
|
|
|
|
+ this.value = this.value.replace(/\D/g, '').slice(0, 11); hideError();
|
|
|
});
|
|
});
|
|
|
- wrap.querySelector('#plAuthCode').addEventListener('keydown', function (e) {
|
|
|
|
|
- if (e.key === 'Enter') { e.preventDefault(); submit(); }
|
|
|
|
|
- });
|
|
|
|
|
- wrap.querySelector('#plAuthPhone').addEventListener('keydown', function (e) {
|
|
|
|
|
- if (e.key === 'Enter') { e.preventDefault(); wrap.querySelector('#plAuthCode').focus(); }
|
|
|
|
|
|
|
+ [['#plAuthCode', '#plAuthEmail'], ['#plPhoneCode', '#plAuthPhone'], ['#plPwdPass', '#plPwdEmail']]
|
|
|
|
|
+ .forEach(function (pair) {
|
|
|
|
|
+ wrap.querySelector(pair[0]).addEventListener('keydown', function (e) {
|
|
|
|
|
+ if (e.key === 'Enter') { e.preventDefault(); submit(); }
|
|
|
|
|
+ });
|
|
|
|
|
+ wrap.querySelector(pair[1]).addEventListener('input', hideError);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function setMode(m) {
|
|
|
|
|
+ mode = m;
|
|
|
|
|
+ const modal = document.getElementById('plAuthModal');
|
|
|
|
|
+ modal.querySelectorAll('.pl-auth-tabs button').forEach(function (b) {
|
|
|
|
|
+ b.classList.toggle('on', b.dataset.mode === m);
|
|
|
});
|
|
});
|
|
|
|
|
+ document.getElementById('plSecEmail').hidden = m !== 'email';
|
|
|
|
|
+ document.getElementById('plSecPwd').hidden = m !== 'password';
|
|
|
|
|
+ document.getElementById('plSecPhone').hidden = m !== 'phone';
|
|
|
|
|
+ document.getElementById('plAuthHint').textContent =
|
|
|
|
|
+ m === 'phone' ? '老手机号验证码登录;登录后需绑定邮箱。' :
|
|
|
|
|
+ m === 'password' ? '未设置密码的账号请用邮箱验证码登录。' :
|
|
|
|
|
+ '输入邮箱获取验证码登录;演示环境验证码会直接弹出显示。';
|
|
|
|
|
+ hideError();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function showError(msg) {
|
|
function showError(msg) {
|
|
|
const el = document.getElementById('plAuthError');
|
|
const el = document.getElementById('plAuthError');
|
|
|
if (!el) return;
|
|
if (!el) return;
|
|
|
- el.textContent = msg;
|
|
|
|
|
- el.hidden = false;
|
|
|
|
|
|
|
+ el.textContent = msg; el.hidden = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function hideError() {
|
|
function hideError() {
|
|
@@ -98,13 +148,13 @@
|
|
|
function open() {
|
|
function open() {
|
|
|
ensureModal();
|
|
ensureModal();
|
|
|
const modal = document.getElementById('plAuthModal');
|
|
const modal = document.getElementById('plAuthModal');
|
|
|
- const phone = document.getElementById('plAuthPhone');
|
|
|
|
|
- phone.value = user ? user.phone : '';
|
|
|
|
|
|
|
+ const email = document.getElementById('plAuthEmail');
|
|
|
|
|
+ email.value = user && user.email ? user.email : '';
|
|
|
document.getElementById('plAuthCode').value = '';
|
|
document.getElementById('plAuthCode').value = '';
|
|
|
- hideError();
|
|
|
|
|
|
|
+ setMode(user && user.phone && !user.email ? 'phone' : 'email');
|
|
|
modal.hidden = false;
|
|
modal.hidden = false;
|
|
|
document.body.classList.add('pl-auth-open');
|
|
document.body.classList.add('pl-auth-open');
|
|
|
- setTimeout(function () { (user ? document.getElementById('plAuthCode') : phone).focus(); }, 30);
|
|
|
|
|
|
|
+ setTimeout(function () { (user && user.email ? document.getElementById('plAuthCode') : email).focus(); }, 30);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
function close() {
|
|
@@ -114,35 +164,39 @@
|
|
|
pending = null;
|
|
pending = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function sendCode() {
|
|
|
|
|
- const phone = document.getElementById('plAuthPhone').value.trim();
|
|
|
|
|
- if (!/^1\d{10}$/.test(phone)) {
|
|
|
|
|
- showError('请输入 11 位手机号');
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ function fillDemoCode(r, inputSel, msgPrefix) {
|
|
|
|
|
+ if (r && r.demo && r.code) {
|
|
|
|
|
+ notify(msgPrefix + r.code + '(已自动填入)');
|
|
|
|
|
+ const input = document.querySelector(inputSel);
|
|
|
|
|
+ if (input) { input.value = r.code; input.focus(); }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ notify('验证码已发送,请查收邮件/短信');
|
|
|
}
|
|
}
|
|
|
- hideError();
|
|
|
|
|
- const btn = document.getElementById('plAuthSend');
|
|
|
|
|
- if (!window.PL || !PL.api) { // 极旧的离线演示:无 api 层
|
|
|
|
|
- notify('离线演示验证码:123456');
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function sendCode(m) {
|
|
|
|
|
+ const btn = m === 'phone' ? document.getElementById('plPhoneSend') : document.getElementById('plAuthSend');
|
|
|
|
|
+ if (btn.disabled) return;
|
|
|
|
|
+ if (!window.PL || !PL.api) { notify('离线演示验证码:123456'); return; }
|
|
|
|
|
+ let req, sel;
|
|
|
|
|
+ if (m === 'phone') {
|
|
|
|
|
+ const phone = document.getElementById('plAuthPhone').value.trim();
|
|
|
|
|
+ if (!/^1\d{10}$/.test(phone)) { showError('请输入 11 位手机号'); return; }
|
|
|
|
|
+ req = PL.api.post('/auth/sms/send', { phone: phone }, { timeout: 10000 });
|
|
|
|
|
+ sel = '#plPhoneCode';
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const email = document.getElementById('plAuthEmail').value.trim();
|
|
|
|
|
+ if (!validEmail(email)) { showError('请输入正确的邮箱地址'); return; }
|
|
|
|
|
+ req = PL.api.post('/auth/email/send', { email: email }, { timeout: 15000 });
|
|
|
|
|
+ sel = '#plAuthCode';
|
|
|
}
|
|
}
|
|
|
- btn.disabled = true;
|
|
|
|
|
- btn.textContent = '发送中…';
|
|
|
|
|
- PL.api.post('/auth/sms/send', { phone: phone }, { timeout: 10000 }).then(function (r) {
|
|
|
|
|
- if (r && r.demo && r.code) {
|
|
|
|
|
- notify('演示验证码:' + r.code + '(已自动填入)');
|
|
|
|
|
- const input = document.getElementById('plAuthCode');
|
|
|
|
|
- if (input) {
|
|
|
|
|
- input.value = r.code;
|
|
|
|
|
- input.focus();
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- notify('验证码已发送,请注意查收短信');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ hideError();
|
|
|
|
|
+ btn.disabled = true; btn.textContent = '发送中…';
|
|
|
|
|
+ req.then(function (r) {
|
|
|
|
|
+ fillDemoCode(r, sel, '演示验证码:');
|
|
|
startCountdown(btn);
|
|
startCountdown(btn);
|
|
|
}, function (err) {
|
|
}, function (err) {
|
|
|
- btn.disabled = false;
|
|
|
|
|
- btn.textContent = '获取验证码';
|
|
|
|
|
|
|
+ btn.disabled = false; btn.textContent = '获取验证码';
|
|
|
if (err.network) {
|
|
if (err.network) {
|
|
|
notify('服务器暂不可达,离线演示验证码:123456', 'warn');
|
|
notify('服务器暂不可达,离线演示验证码:123456', 'warn');
|
|
|
startCountdown(btn);
|
|
startCountdown(btn);
|
|
@@ -161,51 +215,258 @@
|
|
|
countdown -= 1;
|
|
countdown -= 1;
|
|
|
if (countdown <= 0) {
|
|
if (countdown <= 0) {
|
|
|
clearInterval(countdownTimer);
|
|
clearInterval(countdownTimer);
|
|
|
- btn.disabled = false;
|
|
|
|
|
- btn.textContent = '获取验证码';
|
|
|
|
|
|
|
+ btn.disabled = false; btn.textContent = '获取验证码';
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
btn.textContent = countdown + 's 后重发';
|
|
btn.textContent = countdown + 's 后重发';
|
|
|
}, 1000);
|
|
}, 1000);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function applyLogin(u) {
|
|
|
|
|
- user = { id: u.id, phone: u.phone, name: u.name || ('用户' + String(u.phone || '').slice(-4)), role: u.role || 'user', at: Date.now() };
|
|
|
|
|
|
|
+ function persist(u) {
|
|
|
|
|
+ user = u;
|
|
|
try { localStorage.setItem(KEY, JSON.stringify(user)); } catch (e) {}
|
|
try { localStorage.setItem(KEY, JSON.stringify(user)); } catch (e) {}
|
|
|
- const cb = pending;
|
|
|
|
|
- pending = null;
|
|
|
|
|
- close();
|
|
|
|
|
emit();
|
|
emit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function applyLogin(u) {
|
|
|
|
|
+ persist({
|
|
|
|
|
+ id: u.id, phone: u.phone || '', email: u.email || '', name: u.name || '',
|
|
|
|
|
+ role: u.role || 'user', hasPassword: !!u.hasPassword,
|
|
|
|
|
+ needsBindEmail: !!u.needsBindEmail, at: Date.now()
|
|
|
|
|
+ });
|
|
|
|
|
+ if (u.needsBindEmail) {
|
|
|
|
|
+ close();
|
|
|
|
|
+ openBind();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ const cb = pending; pending = null;
|
|
|
|
|
+ close();
|
|
|
notify('登录成功');
|
|
notify('登录成功');
|
|
|
if (cb) setTimeout(cb, 60);
|
|
if (cb) setTimeout(cb, 60);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function submit() {
|
|
function submit() {
|
|
|
- const phone = document.getElementById('plAuthPhone').value.trim();
|
|
|
|
|
- const code = document.getElementById('plAuthCode').value.trim();
|
|
|
|
|
- if (!/^1\d{10}$/.test(phone)) {
|
|
|
|
|
- showError('请输入 11 位手机号');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- if (!/^\d{6}$/.test(code)) {
|
|
|
|
|
- showError('请输入 6 位验证码');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
if (!window.PL || !PL.api) return;
|
|
if (!window.PL || !PL.api) return;
|
|
|
- PL.api.post('/auth/sms/login', { phone: phone, code: code }, { timeout: 10000 }).then(function (r) {
|
|
|
|
|
|
|
+ let req;
|
|
|
|
|
+ if (mode === 'password') {
|
|
|
|
|
+ const email = document.getElementById('plPwdEmail').value.trim();
|
|
|
|
|
+ const pass = document.getElementById('plPwdPass').value;
|
|
|
|
|
+ if (!validEmail(email)) { showError('请输入正确的邮箱地址'); return; }
|
|
|
|
|
+ if (!pass) { showError('请输入密码'); return; }
|
|
|
|
|
+ req = PL.api.post('/auth/email/password-login', { email: email, password: pass }, { timeout: 10000 });
|
|
|
|
|
+ } else if (mode === 'phone') {
|
|
|
|
|
+ const phone = document.getElementById('plAuthPhone').value.trim();
|
|
|
|
|
+ const code = document.getElementById('plPhoneCode').value.trim();
|
|
|
|
|
+ if (!/^1\d{10}$/.test(phone)) { showError('请输入 11 位手机号'); return; }
|
|
|
|
|
+ if (!/^\d{6}$/.test(code)) { showError('请输入 6 位验证码'); return; }
|
|
|
|
|
+ req = PL.api.post('/auth/sms/login', { phone: phone, code: code }, { timeout: 10000 })
|
|
|
|
|
+ .catch(function (err) {
|
|
|
|
|
+ if (err.network) { // 保留旧的离线演示登录
|
|
|
|
|
+ applyLogin({ id: 0, phone: phone, name: '用户' + phone.slice(-4), role: 'user', needsBindEmail: false });
|
|
|
|
|
+ notify('离线演示模式登录(数据不会上传)', 'warn');
|
|
|
|
|
+ return new Promise(function () {});
|
|
|
|
|
+ }
|
|
|
|
|
+ throw err;
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const email = document.getElementById('plAuthEmail').value.trim();
|
|
|
|
|
+ const code = document.getElementById('plAuthCode').value.trim();
|
|
|
|
|
+ if (!validEmail(email)) { showError('请输入正确的邮箱地址'); return; }
|
|
|
|
|
+ if (!/^\d{6}$/.test(code)) { showError('请输入 6 位验证码'); return; }
|
|
|
|
|
+ req = PL.api.post('/auth/email/login', { email: email, code: code }, { timeout: 10000 });
|
|
|
|
|
+ }
|
|
|
|
|
+ const btn = document.getElementById('plAuthSubmit');
|
|
|
|
|
+ btn.disabled = true; btn.textContent = '登录中…';
|
|
|
|
|
+ req.then(function (r) {
|
|
|
|
|
+ btn.disabled = false; btn.textContent = '登录';
|
|
|
|
|
+ if (!r || !r.token) return;
|
|
|
PL.api.setToken(r.token);
|
|
PL.api.setToken(r.token);
|
|
|
applyLogin(r.user);
|
|
applyLogin(r.user);
|
|
|
}, function (err) {
|
|
}, function (err) {
|
|
|
- if (err.network) {
|
|
|
|
|
- // 服务器不可达:保留旧的离线演示登录,站点可继续本地体验
|
|
|
|
|
- applyLogin({ id: 0, phone: phone, name: '用户' + phone.slice(-4), role: 'user' });
|
|
|
|
|
- notify('离线演示模式登录(数据不会上传)', 'warn');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ btn.disabled = false; btn.textContent = '登录';
|
|
|
showError(err.message || '登录失败,请重试');
|
|
showError(err.message || '登录失败,请重试');
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // ---------- 强制绑定邮箱 ----------
|
|
|
|
|
+
|
|
|
|
|
+ function ensureBindModal() {
|
|
|
|
|
+ if (document.getElementById('plBindModal')) return;
|
|
|
|
|
+ const wrap = document.createElement('div');
|
|
|
|
|
+ wrap.id = 'plBindModal';
|
|
|
|
|
+ wrap.className = 'pl-auth-mask';
|
|
|
|
|
+ wrap.hidden = true;
|
|
|
|
|
+ wrap.innerHTML =
|
|
|
|
|
+ '<div class="pl-auth-card" role="dialog" aria-modal="true">' +
|
|
|
|
|
+ '<div class="pl-auth-brand">袋研社 <em>POUCHLAB</em></div>' +
|
|
|
|
|
+ '<h2>绑定邮箱</h2>' +
|
|
|
|
|
+ '<p class="pl-auth-sub">为保障订单通知与账号安全,请先绑定邮箱。绑定后方可继续使用。</p>' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plBindEmail">邮箱</label>' +
|
|
|
|
|
+ '<input id="plBindEmail" class="pl-auth-input" type="email" maxlength="120" autocomplete="email" placeholder="name@example.com">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plBindCode">验证码</label>' +
|
|
|
|
|
+ '<div class="pl-auth-code-row">' +
|
|
|
|
|
+ '<input id="plBindCode" class="pl-auth-input" type="text" inputmode="numeric" maxlength="6" autocomplete="one-time-code" placeholder="6 位验证码">' +
|
|
|
|
|
+ '<button id="plBindSend" class="pl-auth-send" type="button">获取验证码</button>' +
|
|
|
|
|
+ '</div>' +
|
|
|
|
|
+ '<p class="pl-auth-error" id="plBindError" hidden></p>' +
|
|
|
|
|
+ '<button id="plBindSubmit" class="pl-auth-submit" type="button">确认绑定</button>' +
|
|
|
|
|
+ '</div>';
|
|
|
|
|
+ document.body.appendChild(wrap);
|
|
|
|
|
+ wrap.querySelector('#plBindSend').addEventListener('click', bindSendCode);
|
|
|
|
|
+ wrap.querySelector('#plBindSubmit').addEventListener('click', submitBind);
|
|
|
|
|
+ wrap.querySelector('#plBindCode').addEventListener('keydown', function (e) {
|
|
|
|
|
+ if (e.key === 'Enter') { e.preventDefault(); submitBind(); }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function openBind() {
|
|
|
|
|
+ ensureBindModal();
|
|
|
|
|
+ const m = document.getElementById('plBindModal');
|
|
|
|
|
+ document.getElementById('plBindEmail').value = '';
|
|
|
|
|
+ document.getElementById('plBindCode').value = '';
|
|
|
|
|
+ const err = document.getElementById('plBindError'); err.hidden = true;
|
|
|
|
|
+ m.hidden = false;
|
|
|
|
|
+ document.body.classList.add('pl-auth-open');
|
|
|
|
|
+ setTimeout(function () { document.getElementById('plBindEmail').focus(); }, 30);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function closeBind() {
|
|
|
|
|
+ const m = document.getElementById('plBindModal');
|
|
|
|
|
+ if (m) m.hidden = true;
|
|
|
|
|
+ if (!document.getElementById('plAuthModal') || document.getElementById('plAuthModal').hidden) {
|
|
|
|
|
+ document.body.classList.remove('pl-auth-open');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function bindError(msg) {
|
|
|
|
|
+ const el = document.getElementById('plBindError');
|
|
|
|
|
+ el.textContent = msg; el.hidden = false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function bindSendCode() {
|
|
|
|
|
+ const email = document.getElementById('plBindEmail').value.trim();
|
|
|
|
|
+ if (!validEmail(email)) { bindError('请输入正确的邮箱地址'); return; }
|
|
|
|
|
+ const btn = document.getElementById('plBindSend');
|
|
|
|
|
+ btn.disabled = true; btn.textContent = '发送中…';
|
|
|
|
|
+ PL.api.post('/auth/email/send', { email: email }, { timeout: 15000 }).then(function (r) {
|
|
|
|
|
+ fillDemoCode(r, '#plBindCode', '演示验证码:');
|
|
|
|
|
+ startCountdown(btn);
|
|
|
|
|
+ }, function (err) {
|
|
|
|
|
+ btn.disabled = false; btn.textContent = '获取验证码';
|
|
|
|
|
+ bindError(err.message || '发送失败,请稍后再试');
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function submitBind() {
|
|
|
|
|
+ const email = document.getElementById('plBindEmail').value.trim();
|
|
|
|
|
+ const code = document.getElementById('plBindCode').value.trim();
|
|
|
|
|
+ if (!validEmail(email)) { bindError('请输入正确的邮箱地址'); return; }
|
|
|
|
|
+ if (!/^\d{6}$/.test(code)) { bindError('请输入 6 位验证码'); return; }
|
|
|
|
|
+ const btn = document.getElementById('plBindSubmit');
|
|
|
|
|
+ btn.disabled = true; btn.textContent = '绑定中…';
|
|
|
|
|
+ PL.api.post('/auth/bind-email', { email: email, code: code }, { timeout: 10000 }).then(function (r) {
|
|
|
|
|
+ btn.disabled = false; btn.textContent = '确认绑定';
|
|
|
|
|
+ user.email = r.user.email; user.needsBindEmail = false; user.name = r.user.name || user.name;
|
|
|
|
|
+ persist(user);
|
|
|
|
|
+ closeBind();
|
|
|
|
|
+ notify('邮箱已绑定');
|
|
|
|
|
+ if (PL.api.flushBind) PL.api.flushBind();
|
|
|
|
|
+ }, function (err) {
|
|
|
|
|
+ btn.disabled = false; btn.textContent = '确认绑定';
|
|
|
|
|
+ bindError(err.message || '绑定失败,请重试');
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ document.addEventListener('pl:bind-email-required', function () {
|
|
|
|
|
+ if (user) openBind();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // ---------- 账户设置 ----------
|
|
|
|
|
+
|
|
|
|
|
+ function ensureAccountModal() {
|
|
|
|
|
+ if (document.getElementById('plAccModal')) return;
|
|
|
|
|
+ const wrap = document.createElement('div');
|
|
|
|
|
+ wrap.id = 'plAccModal';
|
|
|
|
|
+ wrap.className = 'pl-auth-mask';
|
|
|
|
|
+ wrap.hidden = true;
|
|
|
|
|
+ wrap.innerHTML = STYLE +
|
|
|
|
|
+ '<div class="pl-auth-card" role="dialog" aria-modal="true">' +
|
|
|
|
|
+ '<button class="pl-auth-close" type="button" aria-label="关闭">✕</button>' +
|
|
|
|
|
+ '<div class="pl-auth-brand">袋研社 <em>POUCHLAB</em></div>' +
|
|
|
|
|
+ '<h2>账户设置</h2>' +
|
|
|
|
|
+ '<p class="pl-auth-sub" id="plAccInfo"></p>' +
|
|
|
|
|
+ '<div id="plAccPwdSec">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plAccOld" id="plAccOldLabel">旧密码</label>' +
|
|
|
|
|
+ '<input id="plAccOld" class="pl-auth-input" type="password" maxlength="64" autocomplete="current-password">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plAccNew">新密码</label>' +
|
|
|
|
|
+ '<input id="plAccNew" class="pl-auth-input" type="password" maxlength="64" autocomplete="new-password" placeholder="至少 8 位,含字母和数字">' +
|
|
|
|
|
+ '<label class="pl-auth-label" for="plAccNew2">确认新密码</label>' +
|
|
|
|
|
+ '<input id="plAccNew2" class="pl-auth-input" type="password" maxlength="64" autocomplete="new-password">' +
|
|
|
|
|
+ '</div>' +
|
|
|
|
|
+ '<p class="pl-auth-error" id="plAccError" hidden></p>' +
|
|
|
|
|
+ '<button id="plAccSubmit" class="pl-auth-submit" type="button">保存密码</button>' +
|
|
|
|
|
+ '<button id="plAccBind" class="pl-auth-submit" type="button" hidden>去绑定邮箱</button>' +
|
|
|
|
|
+ '</div>';
|
|
|
|
|
+ document.body.appendChild(wrap);
|
|
|
|
|
+ wrap.querySelector('.pl-auth-close').addEventListener('click', function () { wrap.hidden = true; });
|
|
|
|
|
+ wrap.querySelector('#plAccSubmit').addEventListener('click', submitAccount);
|
|
|
|
|
+ wrap.querySelector('#plAccBind').addEventListener('click', function () {
|
|
|
|
|
+ wrap.hidden = true;
|
|
|
|
|
+ openBind();
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function openAccount() {
|
|
|
|
|
+ ensureAccountModal();
|
|
|
|
|
+ if (!user) return;
|
|
|
|
|
+ const info = document.getElementById('plAccInfo');
|
|
|
|
|
+ info.textContent = user.email
|
|
|
|
|
+ ? '邮箱:' + user.email + ' · ' + (user.hasPassword ? '已设置密码' : '未设置密码')
|
|
|
|
|
+ : '尚未绑定邮箱(' + (user.phone ? '手机号 ' + maskPhone(user.phone) : '演示账号') + ')';
|
|
|
|
|
+ document.getElementById('plAccBind').hidden = !!user.email;
|
|
|
|
|
+ document.getElementById('plAccPwdSec').hidden = !!user.needBindBlock || !user.email && user.id === 0;
|
|
|
|
|
+ document.getElementById('plAccSubmit').hidden = document.getElementById('plAccPwdSec').hidden;
|
|
|
|
|
+ document.getElementById('plAccOldLabel').textContent = user.hasPassword ? '旧密码' : '旧密码(首次设置无需填写)';
|
|
|
|
|
+ document.getElementById('plAccOld').hidden = !user.hasPassword;
|
|
|
|
|
+ document.getElementById('plAccError').hidden = true;
|
|
|
|
|
+ document.getElementById('plAccModal').hidden = false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function accError(msg) {
|
|
|
|
|
+ const el = document.getElementById('plAccError');
|
|
|
|
|
+ el.textContent = msg; el.hidden = false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function submitAccount() {
|
|
|
|
|
+ const np = document.getElementById('plAccNew').value;
|
|
|
|
|
+ const np2 = document.getElementById('plAccNew2').value;
|
|
|
|
|
+ if (np !== np2) { accError('两次输入的密码不一致'); return; }
|
|
|
|
|
+ if (np.length < 8 || !/[A-Za-z]/.test(np) || !/\d/.test(np)) {
|
|
|
|
|
+ accError('密码需至少 8 位且包含字母和数字'); return;
|
|
|
|
|
+ }
|
|
|
|
|
+ const btn = document.getElementById('plAccSubmit');
|
|
|
|
|
+ btn.disabled = true;
|
|
|
|
|
+ PL.api.post('/auth/set-password',
|
|
|
|
|
+ { oldPassword: user.hasPassword ? document.getElementById('plAccOld').value : '', newPassword: np },
|
|
|
|
|
+ { timeout: 10000 }).then(function () {
|
|
|
|
|
+ btn.disabled = false;
|
|
|
|
|
+ user.hasPassword = true;
|
|
|
|
|
+ persist(user);
|
|
|
|
|
+ document.getElementById('plAccModal').hidden = true;
|
|
|
|
|
+ notify('密码已保存');
|
|
|
|
|
+ }, function (err) {
|
|
|
|
|
+ btn.disabled = false;
|
|
|
|
|
+ accError(err.message || '保存失败,请重试');
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ---------- 入口渲染 ----------
|
|
|
|
|
+
|
|
|
|
|
+ function displayName() {
|
|
|
|
|
+ if (!user) return '登录';
|
|
|
|
|
+ return user.name || user.email || maskPhone(user.phone) || '登录';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
function logout() {
|
|
function logout() {
|
|
|
user = null;
|
|
user = null;
|
|
|
pending = null;
|
|
pending = null;
|
|
@@ -217,6 +478,7 @@
|
|
|
|
|
|
|
|
function requireAuth(cb) {
|
|
function requireAuth(cb) {
|
|
|
if (user) {
|
|
if (user) {
|
|
|
|
|
+ if (user.needsBindEmail) openBind();
|
|
|
if (cb) cb();
|
|
if (cb) cb();
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
@@ -229,11 +491,11 @@
|
|
|
const btn = entry.querySelector('.pl-auth-btn');
|
|
const btn = entry.querySelector('.pl-auth-btn');
|
|
|
const menu = entry.querySelector('.pl-auth-menu');
|
|
const menu = entry.querySelector('.pl-auth-menu');
|
|
|
if (!btn) return;
|
|
if (!btn) return;
|
|
|
- btn.textContent = user ? maskPhone(user.phone) : '登录';
|
|
|
|
|
|
|
+ btn.textContent = displayName();
|
|
|
btn.classList.toggle('on', !!user);
|
|
btn.classList.toggle('on', !!user);
|
|
|
if (menu) {
|
|
if (menu) {
|
|
|
const name = menu.querySelector('.pl-auth-name');
|
|
const name = menu.querySelector('.pl-auth-name');
|
|
|
- if (name) name.textContent = user ? '已登录 · ' + user.name : '';
|
|
|
|
|
|
|
+ if (name) name.textContent = user ? '已登录 · ' + (user.email || user.name) : '';
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -242,18 +504,21 @@
|
|
|
'<button class="pl-auth-btn" type="button"></button>' +
|
|
'<button class="pl-auth-btn" type="button"></button>' +
|
|
|
'<div class="pl-auth-menu" hidden>' +
|
|
'<div class="pl-auth-menu" hidden>' +
|
|
|
'<span class="pl-auth-name"></span>' +
|
|
'<span class="pl-auth-name"></span>' +
|
|
|
|
|
+ '<button type="button" class="pl-auth-account">账户设置</button>' +
|
|
|
'<button type="button" class="pl-auth-logout">退出登录</button>' +
|
|
'<button type="button" class="pl-auth-logout">退出登录</button>' +
|
|
|
'</div>';
|
|
'</div>';
|
|
|
const btn = entry.querySelector('.pl-auth-btn');
|
|
const btn = entry.querySelector('.pl-auth-btn');
|
|
|
const menu = entry.querySelector('.pl-auth-menu');
|
|
const menu = entry.querySelector('.pl-auth-menu');
|
|
|
btn.addEventListener('click', function (e) {
|
|
btn.addEventListener('click', function (e) {
|
|
|
e.stopPropagation();
|
|
e.stopPropagation();
|
|
|
- if (!user) {
|
|
|
|
|
- open();
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!user) { open(); return; }
|
|
|
menu.hidden = !menu.hidden;
|
|
menu.hidden = !menu.hidden;
|
|
|
});
|
|
});
|
|
|
|
|
+ entry.querySelector('.pl-auth-account').addEventListener('click', function (e) {
|
|
|
|
|
+ e.stopPropagation();
|
|
|
|
|
+ menu.hidden = true;
|
|
|
|
|
+ openAccount();
|
|
|
|
|
+ });
|
|
|
entry.querySelector('.pl-auth-logout').addEventListener('click', function (e) {
|
|
entry.querySelector('.pl-auth-logout').addEventListener('click', function (e) {
|
|
|
e.stopPropagation();
|
|
e.stopPropagation();
|
|
|
menu.hidden = true;
|
|
menu.hidden = true;
|
|
@@ -270,14 +535,10 @@
|
|
|
trigger.addEventListener('click', function (e) { e.preventDefault(); });
|
|
trigger.addEventListener('click', function (e) { e.preventDefault(); });
|
|
|
}
|
|
}
|
|
|
trigger.addEventListener('click', function () {
|
|
trigger.addEventListener('click', function () {
|
|
|
- if (user) {
|
|
|
|
|
- logout();
|
|
|
|
|
- } else {
|
|
|
|
|
- open();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (user) { logout(); } else { open(); }
|
|
|
});
|
|
});
|
|
|
const sync = function () {
|
|
const sync = function () {
|
|
|
- trigger.textContent = user ? maskPhone(user.phone) : '登录';
|
|
|
|
|
|
|
+ trigger.textContent = displayName();
|
|
|
trigger.classList.toggle('on', !!user);
|
|
trigger.classList.toggle('on', !!user);
|
|
|
trigger.title = user ? '点击退出登录' : '登录';
|
|
trigger.title = user ? '点击退出登录' : '登录';
|
|
|
};
|
|
};
|
|
@@ -299,6 +560,7 @@
|
|
|
if (e.key === 'Escape') {
|
|
if (e.key === 'Escape') {
|
|
|
const modal = document.getElementById('plAuthModal');
|
|
const modal = document.getElementById('plAuthModal');
|
|
|
if (modal && !modal.hidden) close();
|
|
if (modal && !modal.hidden) close();
|
|
|
|
|
+ // 强制绑定框不响应 Esc
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -308,7 +570,12 @@
|
|
|
open: open,
|
|
open: open,
|
|
|
logout: logout,
|
|
logout: logout,
|
|
|
require: requireAuth,
|
|
require: requireAuth,
|
|
|
- onChange: function (fn) { listeners.push(fn); return function () { const i = listeners.indexOf(fn); if (i >= 0) listeners.splice(i, 1); }; }
|
|
|
|
|
|
|
+ openBind: openBind,
|
|
|
|
|
+ openAccount: openAccount,
|
|
|
|
|
+ onChange: function (fn) {
|
|
|
|
|
+ listeners.push(fn);
|
|
|
|
|
+ return function () { const i = listeners.indexOf(fn); if (i >= 0) listeners.splice(i, 1); };
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
if (document.readyState === 'loading') {
|
|
if (document.readyState === 'loading') {
|