5fb269cc0a
- 保存先写本地缓存,登录后可选云端同步;退出登录不再因文件操作跳转登录 - 修复 Layui 遮罩残留、design2 保存后线条、toast 被 finally 提前关闭 - 保存过程恢复 loading spinner;成功提示 2.5 秒 - 云端 payload 瘦身与体积超限提示;后端 schema 迁移与 FileService 容错 Co-authored-by: Cursor <cursoragent@cursor.com>
132 lines
4.6 KiB
JavaScript
132 lines
4.6 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
function showAuthedNav() {
|
|
var guest = document.getElementById('auth_guest');
|
|
var account = document.getElementById('auth_account');
|
|
var session = document.getElementById('auth_session');
|
|
if (guest) guest.style.display = 'none';
|
|
if (account) account.style.display = 'inline-flex';
|
|
if (session) session.style.display = 'inline-flex';
|
|
}
|
|
|
|
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 initClearCache() {
|
|
var btn = document.getElementById('auth_clear_cache');
|
|
if (!btn) return;
|
|
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 initTopbar(options) {
|
|
options = options || {};
|
|
initClearCache();
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
var meFetch = typeof window.soonAuthedFetch === 'function'
|
|
? window.soonAuthedFetch(base + '/auth/me', { headers: { Authorization: 'Bearer ' + tok } })
|
|
: fetch(base + '/auth/me', { headers: { Authorization: 'Bearer ' + tok } });
|
|
meFetch
|
|
.then(function (r) {
|
|
if (r.status === 401) {
|
|
clearSession();
|
|
location.reload();
|
|
return null;
|
|
}
|
|
if (!r.ok) {
|
|
if (typeof window.soonToast === 'function') window.soonToast('网络异常,部分功能可能不可用', 'warn');
|
|
return null;
|
|
}
|
|
return r.json();
|
|
})
|
|
.then(function (j) {
|
|
if (!j) return;
|
|
if (!j.ok || !j.data) {
|
|
clearSession();
|
|
location.reload();
|
|
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();
|
|
}
|
|
if (typeof options.onAuthed === 'function') options.onAuthed(j.data);
|
|
})
|
|
.catch(function () {
|
|
if (typeof window.soonToast === 'function') window.soonToast('网络异常,部分功能可能不可用', 'warn');
|
|
});
|
|
|
|
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 };
|
|
})();
|