7dadc41bb1
弹窗登录/注册成功后隐藏 guest 顶栏并更新会员身份;导出打印 gate 内立即注册改为同层弹窗,注册完自动继续会员验证。 Co-authored-by: Cursor <cursoragent@cursor.com>
196 lines
6.8 KiB
JavaScript
196 lines
6.8 KiB
JavaScript
(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 {
|
|
localStorage.removeItem('soon_access');
|
|
localStorage.removeItem('soon_refresh');
|
|
}
|
|
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 || 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;
|
|
layui.use('layer', function () {
|
|
var layer = layui.layer;
|
|
layer.open({
|
|
type: 1,
|
|
skin: 'soon-layer',
|
|
title: '清除本地缓存',
|
|
content: '<div class="soon-dialog-body">将清除最近文件与本地缓存,不会退出登录。未上传的本地编辑将丢失。</div>',
|
|
btn: ['确定清除', '取消'],
|
|
btnAlign: 'r',
|
|
area: ['320px', 'auto'],
|
|
shadeClose: true,
|
|
yes: function (index) {
|
|
layer.close(index);
|
|
window.soonClearLocalDesignCache().then(function () {
|
|
if (typeof window.soonToast === 'function') window.soonToast('已清除本地缓存', 'success');
|
|
else layer.msg('已清除本地缓存', { icon: 1, time: 1200 });
|
|
setTimeout(function () {
|
|
if (/design[12]\.web\.html/i.test(location.pathname || '')) location.href = 'index.web.html';
|
|
else location.reload();
|
|
}, 400);
|
|
});
|
|
},
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
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') || '';
|
|
if (!tok) {
|
|
showGuestNav();
|
|
return Promise.resolve();
|
|
}
|
|
|
|
showAuthedNav();
|
|
|
|
var base = (window.SOON_DEPLOY_CONFIG && window.SOON_DEPLOY_CONFIG.api_v1_base) || '';
|
|
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 } });
|
|
|
|
return meFetch
|
|
.then(function (r) {
|
|
if (r.status === 401) {
|
|
clearSession();
|
|
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();
|
|
})
|
|
.then(function (j) {
|
|
if (!j) return;
|
|
if (!j.ok || !j.data) {
|
|
clearSession();
|
|
showGuestNav();
|
|
return;
|
|
}
|
|
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();
|
|
});
|
|
}
|
|
|
|
function initTopbar(options) {
|
|
_opts = options || {};
|
|
return refreshAuthUi(_opts);
|
|
}
|
|
|
|
window.soonRefreshPortalAuthUi = refreshAuthUi;
|
|
window.soonPortalAuth = { init: initTopbar, refresh: refreshAuthUi };
|
|
})();
|