fix(web): 弹窗登录注册后刷新顶栏,导出流程支持内嵌注册
弹窗登录/注册成功后隐藏 guest 顶栏并更新会员身份;导出打印 gate 内立即注册改为同层弹窗,注册完自动继续会员验证。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,30 +1,70 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var _opts = {};
|
||||
var _logoutBound = false;
|
||||
|
||||
function showAuthedNav() {
|
||||
var guest = document.getElementById('auth_guest');
|
||||
var account = document.getElementById('auth_account');
|
||||
var session = document.getElementById('auth_session');
|
||||
var logout = document.getElementById('auth_logout');
|
||||
if (guest) guest.style.display = 'none';
|
||||
if (account) account.style.display = 'inline-flex';
|
||||
if (session) session.style.display = 'inline-flex';
|
||||
if (logout) logout.style.display = 'inline-flex';
|
||||
}
|
||||
|
||||
function showGuestNav() {
|
||||
var guest = document.getElementById('auth_guest');
|
||||
var account = document.getElementById('auth_account');
|
||||
var session = document.getElementById('auth_session');
|
||||
var admin = document.getElementById('auth_admin');
|
||||
var avatar = document.getElementById('auth_avatar');
|
||||
var identity = document.getElementById('auth_identity');
|
||||
var logout = document.getElementById('auth_logout');
|
||||
if (guest) guest.style.display = '';
|
||||
if (account) account.style.display = 'none';
|
||||
if (session) session.style.display = 'none';
|
||||
if (admin) admin.style.display = 'none';
|
||||
if (logout) logout.style.display = 'none';
|
||||
if (avatar) {
|
||||
avatar.style.display = 'none';
|
||||
avatar.textContent = '';
|
||||
avatar.title = '';
|
||||
}
|
||||
if (identity) {
|
||||
identity.style.display = 'none';
|
||||
identity.textContent = '';
|
||||
}
|
||||
}
|
||||
|
||||
function clearSession() {
|
||||
if (typeof window.soonClearAuthSession === 'function') {
|
||||
window.soonClearAuthSession();
|
||||
} else {
|
||||
if (typeof window.soonClearAuthSession === 'function') window.soonClearAuthSession();
|
||||
else {
|
||||
localStorage.removeItem('soon_access');
|
||||
localStorage.removeItem('soon_refresh');
|
||||
}
|
||||
if (typeof window.soonApplyMembership === 'function') {
|
||||
window.soonApplyMembership(null);
|
||||
if (typeof window.soonApplyMembership === 'function') window.soonApplyMembership(null);
|
||||
}
|
||||
|
||||
function applyUserUi(data) {
|
||||
if (typeof window.soonApplyMembership === 'function') window.soonApplyMembership(data);
|
||||
var admin = document.getElementById('auth_admin');
|
||||
var avatar = document.getElementById('auth_avatar');
|
||||
if (data.role === 'admin' && admin) admin.style.display = 'inline-flex';
|
||||
if (avatar && data.email) {
|
||||
avatar.style.display = 'inline-flex';
|
||||
avatar.textContent = data.email.charAt(0).toUpperCase();
|
||||
avatar.title = data.email;
|
||||
}
|
||||
if (typeof window.soonRefreshPortalIdentity === 'function') window.soonRefreshPortalIdentity();
|
||||
}
|
||||
|
||||
function initClearCache() {
|
||||
var btn = document.getElementById('auth_clear_cache');
|
||||
if (!btn) return;
|
||||
if (!btn || btn.getAttribute('data-soon-bound') === '1') return;
|
||||
btn.setAttribute('data-soon-bound', '1');
|
||||
btn.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
if (typeof layui === 'undefined' || typeof window.soonClearLocalDesignCache !== 'function') return;
|
||||
@@ -49,45 +89,82 @@
|
||||
else location.reload();
|
||||
}, 400);
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function initTopbar(options) {
|
||||
options = options || {};
|
||||
function bindLogoutOnce(options) {
|
||||
if (_logoutBound) return;
|
||||
var logout = document.getElementById('auth_logout');
|
||||
if (!logout) return;
|
||||
_logoutBound = true;
|
||||
logout.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
clearSession();
|
||||
showGuestNav();
|
||||
if (options.logoutReload !== false) location.reload();
|
||||
else location.href = 'index.web.html';
|
||||
});
|
||||
}
|
||||
|
||||
function bindPopupLink(id, fn) {
|
||||
var el = document.getElementById(id);
|
||||
if (!el || el.getAttribute('data-soon-popup-bound') === '1') return;
|
||||
el.setAttribute('data-soon-popup-bound', '1');
|
||||
el.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
fn();
|
||||
});
|
||||
}
|
||||
|
||||
function bindPopupAuthLinks() {
|
||||
if (typeof window.soonShowLoginGate !== 'function') return;
|
||||
bindPopupLink('auth_login', function () {
|
||||
window.soonShowLoginGate({ reason: '登录账号' });
|
||||
});
|
||||
if (typeof window.soonShowRegisterGate === 'function') {
|
||||
bindPopupLink('auth_register', function () {
|
||||
window.soonShowRegisterGate({ reason: '注册账号' });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function refreshAuthUi(options) {
|
||||
options = options || _opts || {};
|
||||
initClearCache();
|
||||
bindLogoutOnce(options);
|
||||
bindPopupAuthLinks();
|
||||
|
||||
var tok = localStorage.getItem('soon_access') || '';
|
||||
var login = document.getElementById('auth_login');
|
||||
var reg = document.getElementById('auth_register');
|
||||
var admin = document.getElementById('auth_admin');
|
||||
var logout = document.getElementById('auth_logout');
|
||||
var avatar = document.getElementById('auth_avatar');
|
||||
|
||||
if (!tok) return;
|
||||
if (!tok) {
|
||||
showGuestNav();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
showAuthedNav();
|
||||
if (login) login.style.display = 'none';
|
||||
if (reg) reg.style.display = 'none';
|
||||
if (logout) logout.style.display = 'inline-flex';
|
||||
|
||||
var base = (window.SOON_DEPLOY_CONFIG && window.SOON_DEPLOY_CONFIG.api_v1_base) || '';
|
||||
if (!base) return;
|
||||
if (!base) {
|
||||
if (typeof window.soonRefreshPortalIdentity === 'function') window.soonRefreshPortalIdentity();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
var meFetch = typeof window.soonAuthedFetch === 'function'
|
||||
? window.soonAuthedFetch(base + '/auth/me', { headers: { Authorization: 'Bearer ' + tok } })
|
||||
: fetch(base + '/auth/me', { headers: { Authorization: 'Bearer ' + tok } });
|
||||
meFetch
|
||||
|
||||
return meFetch
|
||||
.then(function (r) {
|
||||
if (r.status === 401) {
|
||||
clearSession();
|
||||
location.reload();
|
||||
showGuestNav();
|
||||
return null;
|
||||
}
|
||||
if (!r.ok) {
|
||||
if (typeof window.soonToast === 'function') window.soonToast('网络异常,部分功能可能不可用', 'warn');
|
||||
if (typeof window.soonRefreshPortalIdentity === 'function') window.soonRefreshPortalIdentity();
|
||||
return null;
|
||||
}
|
||||
return r.json();
|
||||
@@ -96,36 +173,23 @@
|
||||
if (!j) return;
|
||||
if (!j.ok || !j.data) {
|
||||
clearSession();
|
||||
location.reload();
|
||||
showGuestNav();
|
||||
return;
|
||||
}
|
||||
if (typeof window.soonApplyMembership === 'function') {
|
||||
window.soonApplyMembership(j.data);
|
||||
}
|
||||
if (j.data.role === 'admin' && admin) admin.style.display = 'inline-flex';
|
||||
if (avatar && j.data.email) {
|
||||
avatar.style.display = 'inline-flex';
|
||||
avatar.textContent = j.data.email.charAt(0).toUpperCase();
|
||||
avatar.title = j.data.email;
|
||||
}
|
||||
if (typeof window.soonRefreshPortalIdentity === 'function') {
|
||||
window.soonRefreshPortalIdentity();
|
||||
}
|
||||
applyUserUi(j.data);
|
||||
if (typeof options.onAuthed === 'function') options.onAuthed(j.data);
|
||||
})
|
||||
.catch(function () {
|
||||
if (typeof window.soonToast === 'function') window.soonToast('网络异常,部分功能可能不可用', 'warn');
|
||||
if (typeof window.soonRefreshPortalIdentity === 'function') window.soonRefreshPortalIdentity();
|
||||
});
|
||||
|
||||
if (logout) {
|
||||
logout.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
clearSession();
|
||||
if (options.logoutReload !== false) location.reload();
|
||||
else location.href = 'index.web.html';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
window.soonPortalAuth = { init: initTopbar };
|
||||
function initTopbar(options) {
|
||||
_opts = options || {};
|
||||
return refreshAuthUi(_opts);
|
||||
}
|
||||
|
||||
window.soonRefreshPortalAuthUi = refreshAuthUi;
|
||||
window.soonPortalAuth = { init: initTopbar, refresh: refreshAuthUi };
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user