Web 端数据交互与 design2 布局修复
统一门户 JSON 契约:新增 GET /templates/{id},模板/云文件/保存/打开走虚拟 key;
layer 保存弹层替代 prompt,本地 .soon 已登录后台 POST 登记;修复保存静默失败与 design2 全宽布局。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+26
-49
@@ -512,41 +512,26 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
function resolveTemplateItem(m, base) {
|
||||
var type = Number(m.type) || 1;
|
||||
var id = m.id;
|
||||
var v = m.updated_at ? ('?v=' + encodeURIComponent(m.updated_at)) : '';
|
||||
return {
|
||||
id: id,
|
||||
name: m.name || m.title || '模板',
|
||||
type: type,
|
||||
thumbSrc: id ? (base + '/templates/' + id + '/thumb') : templateFallbackThumb(type),
|
||||
file_url: id ? (base + '/templates/' + id + '/file') : (m.file_url || '')
|
||||
thumbSrc: id ? (base + '/templates/' + id + '/thumb' + v) : templateFallbackThumb(type)
|
||||
};
|
||||
}
|
||||
|
||||
function openTemplateItem(m) {
|
||||
var type = Number(m.type) || 1;
|
||||
function openWithJson(j) {
|
||||
if (!j) {
|
||||
openDesign(type, '');
|
||||
return;
|
||||
}
|
||||
var key = 'soondesign_session:tpl-' + Date.now();
|
||||
try {
|
||||
sessionStorage.setItem(key, JSON.stringify(j));
|
||||
openDesign(type, key);
|
||||
} catch (e) {
|
||||
openDesign(type, '');
|
||||
}
|
||||
}
|
||||
var fileUrl = m.file_url || '';
|
||||
if (!fileUrl) {
|
||||
openDesign(type, '');
|
||||
var id = m && m.id;
|
||||
if (!id) {
|
||||
if (typeof window.soonToast === 'function') window.soonToast('模板不可用', 'error');
|
||||
return;
|
||||
}
|
||||
fetch(fileUrl).then(function (r) {
|
||||
if (!r.ok) throw new Error('fetch');
|
||||
return r.json();
|
||||
}).then(openWithJson).catch(function () {
|
||||
openDesign(type, '');
|
||||
});
|
||||
var templateKey = typeof window.soonMakeTemplateKey === 'function'
|
||||
? window.soonMakeTemplateKey(id)
|
||||
: ('soondesign_template:' + id);
|
||||
openDesign(type, templateKey);
|
||||
}
|
||||
|
||||
function templateCardHtml(m, itemIndex) {
|
||||
@@ -614,7 +599,9 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
renderTemplatePager();
|
||||
}
|
||||
|
||||
async function loadTemplates() {
|
||||
var _templatesLoadedAt = 0;
|
||||
|
||||
async function loadTemplates(force) {
|
||||
|
||||
var grid = document.getElementById('templatesGrid');
|
||||
|
||||
@@ -622,6 +609,10 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
|
||||
if (!grid) return;
|
||||
|
||||
if (!force && _templatesLoadedAt && (Date.now() - _templatesLoadedAt) < 60000) {
|
||||
return;
|
||||
}
|
||||
|
||||
var base = (window.SOON_DEPLOY_CONFIG && window.SOON_DEPLOY_CONFIG.api_v1_base) || '';
|
||||
|
||||
if (!base) {
|
||||
@@ -655,6 +646,7 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
templateListState.total = templateListState.items.length;
|
||||
templateListState.page = 1;
|
||||
renderTemplatesPage();
|
||||
_templatesLoadedAt = Date.now();
|
||||
|
||||
} catch (e) {
|
||||
|
||||
@@ -670,6 +662,14 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
|
||||
loadTemplates();
|
||||
|
||||
window.soonReloadTemplates = function () { loadTemplates(true); };
|
||||
document.addEventListener('visibilitychange', function () {
|
||||
if (document.visibilityState === 'visible') loadTemplates(false);
|
||||
});
|
||||
window.addEventListener('pageshow', function (e) {
|
||||
if (e.persisted) loadTemplates(false);
|
||||
});
|
||||
|
||||
var templatePrevNav = document.getElementById('templatePrev');
|
||||
var templateNextNav = document.getElementById('templateNext');
|
||||
if (templatePrevNav) {
|
||||
@@ -879,30 +879,7 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
|
||||
}
|
||||
|
||||
var tok = typeof window.soonGetAccessToken === 'function'
|
||||
? window.soonGetAccessToken()
|
||||
: (localStorage.getItem('soon_access') || '');
|
||||
|
||||
if (!tok && typeof window.soonOpenSoonJsonLocally === 'function') {
|
||||
window.soonOpenSoonJsonLocally(soonData, fileName);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!window.platformBridge || !window.platformBridge.importSoonFile) return;
|
||||
|
||||
try {
|
||||
|
||||
var res = await window.platformBridge.importSoonFile(fileName, soonData);
|
||||
|
||||
if (!res || !res.fileKey) return;
|
||||
|
||||
var type = soonData.soonType ? soonData.soonType : (soonData.backBlackPic ? 2 : 1);
|
||||
|
||||
openDesign(type, res.fileKey);
|
||||
|
||||
loadHistory();
|
||||
|
||||
} catch (err) { /* errors shown by bridge */ }
|
||||
window.soonOpenSoonJsonLocally(soonData, fileName);
|
||||
|
||||
}).catch(function (err) { console.log(err); });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user