Files
jiapu/public/js/growth-pages.js
T

302 lines
9.7 KiB
JavaScript

(function (root, factory) {
// 成长记录页面只消费 ApiClient 中已在 Apifox PC 目录核验的方法。
if (typeof module === 'object' && module.exports) {
module.exports = factory(root);
return;
}
root.GrowthPages = factory(root);
if (root.document) {
root.document.addEventListener('DOMContentLoaded', function () {
root.GrowthPages.init();
});
}
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
'use strict';
var documentRef = root.document;
var writePending = false;
function getApi() {
return root.GenealogyApi && root.GenealogyApi.defaultClient;
}
function query(selector, rootNode) {
return documentRef ? (rootNode || documentRef).querySelector(selector) : null;
}
function queryAll(selector, rootNode) {
return documentRef ? Array.prototype.slice.call((rootNode || documentRef).querySelectorAll(selector)) : [];
}
function trimOrUndefined(value) {
var text = String(value === undefined || value === null ? '' : value).trim();
return text || undefined;
}
function toSafeIntegerOrUndefined(value) {
var text = trimOrUndefined(value);
var number;
if (!text) return undefined;
number = Number(text);
return Number.isInteger(number) && Number.isSafeInteger(number) ? number : undefined;
}
function getQueryParam(search, name) {
var params = new URLSearchParams(String(search || '').replace(/^\?/, ''));
return params.get(name) || '';
}
function getCurrentGenealogyId(search) {
var page = query('[data-growth-page], [data-growth-edit-page]');
var source = search === undefined && root.location ? root.location.search : search;
var profileGenealogyId;
if (search === undefined && root.ProfileUI && root.ProfileUI.getGenealogyId) {
profileGenealogyId = root.ProfileUI.getGenealogyId();
if (profileGenealogyId) return profileGenealogyId;
}
return getQueryParam(source, 'genealogyId') || (page && page.getAttribute('data-genealogy-id')) || '';
}
function buildGrowthRecordBody(values) {
var source = values || {};
var body = {
recordTitle: String(source.recordTitle || '').trim()
};
var lineagePersonId = trimOrUndefined(source.lineagePersonId);
var recordType = trimOrUndefined(source.recordType);
var recordContent = trimOrUndefined(source.recordContent);
var recordDate = trimOrUndefined(source.recordDate);
var remindTime = trimOrUndefined(source.remindTime);
var mediaOssIds = trimOrUndefined(source.mediaOssIds);
var status = trimOrUndefined(source.status);
var sortOrderText = trimOrUndefined(source.sortOrder);
if (lineagePersonId !== undefined) body.lineagePersonId = lineagePersonId;
if (recordType !== undefined) body.recordType = recordType;
if (recordContent !== undefined) body.recordContent = recordContent;
if (recordDate !== undefined) body.recordDate = recordDate;
if (remindTime !== undefined) body.remindTime = remindTime;
if (mediaOssIds !== undefined) body.mediaOssIds = mediaOssIds;
if (sortOrderText !== undefined) body.sortOrder = toSafeIntegerOrUndefined(sortOrderText);
if (status !== undefined) body.status = status;
return body;
}
function validateGrowthRecordBody(body) {
if (!body || !body.recordTitle) return '请填写记录标题';
if (body.lineagePersonId !== undefined && !/^\d+$/.test(body.lineagePersonId)) return '世系人物 ID 必须是整数';
if (body.mediaOssIds !== undefined && !/^[1-9][0-9]*(,[1-9][0-9]*)*$/.test(body.mediaOssIds)) {
return '附件 OSS ID 请使用英文逗号分隔的正整数';
}
if (Object.prototype.hasOwnProperty.call(body, 'sortOrder') && body.sortOrder === undefined) return '排序值必须是安全整数';
return '';
}
function normalizeGrowthList(data) {
return Array.isArray(data) ? data : [];
}
function escapeHtml(value) {
return String(value === undefined || value === null ? '' : value)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}
function stringifyRecord(record) {
try {
return typeof record === 'string' ? record : JSON.stringify(record);
} catch (error) {
return String(record);
}
}
function renderGrowthRecords(data) {
var container = query('[data-growth-list]');
var records = normalizeGrowthList(data);
if (!container) return;
if (!Array.isArray(data)) {
container.innerHTML = '<div class="api-empty">成长记录响应未按 Apifox ListResult 返回数组,无法安全展示或操作记录。</div>';
return;
}
if (!records.length) {
container.innerHTML = '<div class="api-empty">暂无成长记录</div>';
return;
}
// ListResult 的元素 DTO 尚未展开,不能假设 recordId、标题或权限字段。
container.innerHTML = records.map(function (record, index) {
return '<article class="module-row"><div><h3>成长记录 ' + (index + 1) + '</h3><p>' +
escapeHtml(stringifyRecord(record)) + '</p></div></article>';
}).join('');
}
function showMessage(message) {
if (root.layui && root.layui.layer) {
root.layui.layer.msg(message);
return;
}
if (root.alert) root.alert(message);
}
function shouldRedirectToLogin(api, error) {
var status = error && (error.status || error.code);
return !api || !api.getToken || !api.getToken() || Number(status) === 401;
}
function redirectUnauthorized(api, error) {
if (!shouldRedirectToLogin(api, error)) return false;
if (api && api.clearToken) api.clearToken();
if (!root.location) return true;
if (typeof root.location.replace === 'function') {
root.location.replace('login.html');
return true;
}
root.location.href = 'login.html';
return true;
}
function getFormValues(form) {
var values = {};
queryAll('[name]', form).forEach(function (field) {
values[field.name] = field.value;
});
return values;
}
function setFormStatus(message) {
var status = query('[data-growth-form-status]');
if (status) status.textContent = message || '';
}
function setWritePending(pending) {
writePending = Boolean(pending);
queryAll('[data-growth-form] button, [data-growth-form] input, [data-growth-form] textarea').forEach(function (control) {
control.disabled = writePending;
});
}
function syncGenealogyLinks(genealogyId) {
if (root.ProfileUI && root.ProfileUI.syncGenealogyContextLinks) {
root.ProfileUI.syncGenealogyContextLinks();
return;
}
queryAll('[data-genealogy-context-link]').forEach(function (link) {
var href = link.getAttribute('href');
var base;
if (!href || href === '#') return;
base = href.split('?')[0];
link.href = base + '?genealogyId=' + encodeURIComponent(genealogyId);
});
}
function renderNoContext() {
var list = query('[data-growth-list]');
if (list) list.innerHTML = '<div class="api-empty">等待家谱入口接口;请从具体家谱进入成长记录。</div>';
setFormStatus('等待家谱入口接口;请从具体家谱进入成长记录。');
}
function requireGenealogyId() {
var genealogyId = getCurrentGenealogyId();
if (!genealogyId) {
renderNoContext();
showMessage('等待家谱入口接口;请从具体家谱进入成长记录。');
}
return genealogyId;
}
function buildListUrl(genealogyId) {
return 'profile-growth.html?genealogyId=' + encodeURIComponent(genealogyId);
}
async function loadGrowthRecords() {
var api = getApi();
var genealogyId;
if (redirectUnauthorized(api)) return;
genealogyId = requireGenealogyId();
if (!genealogyId) return;
syncGenealogyLinks(genealogyId);
try {
renderGrowthRecords(await api.growthRecords(genealogyId));
} catch (error) {
if (redirectUnauthorized(api, error)) return;
renderGrowthRecords(null);
showMessage(error.message || '成长记录加载失败');
}
}
async function submitGrowthRecord(form) {
var api = getApi();
var genealogyId;
var body;
var validation;
if (writePending || redirectUnauthorized(api)) return;
genealogyId = requireGenealogyId();
if (!genealogyId) return;
body = buildGrowthRecordBody(getFormValues(form));
validation = validateGrowthRecordBody(body);
if (validation) {
setFormStatus(validation);
return;
}
setWritePending(true);
setFormStatus('正在保存成长记录...');
try {
await api.createGrowthRecord(genealogyId, body);
if (root.location) root.location.href = buildListUrl(genealogyId);
} catch (error) {
if (redirectUnauthorized(api, error)) return;
setFormStatus(error.message || '成长记录保存失败');
} finally {
setWritePending(false);
}
}
function bindActions() {
if (!documentRef) return;
documentRef.addEventListener('submit', function (event) {
var form = event.target.closest('[data-growth-form]');
if (!form) return;
event.preventDefault();
submitGrowthRecord(form);
});
}
function init() {
var genealogyId;
if (!documentRef) return;
bindActions();
if (query('[data-growth-page]')) loadGrowthRecords();
if (query('[data-growth-edit-page]')) {
genealogyId = requireGenealogyId();
if (genealogyId) syncGenealogyLinks(genealogyId);
}
}
return {
getCurrentGenealogyId: getCurrentGenealogyId,
buildGrowthRecordBody: buildGrowthRecordBody,
validateGrowthRecordBody: validateGrowthRecordBody,
normalizeGrowthList: normalizeGrowthList,
shouldRedirectToLogin: shouldRedirectToLogin,
init: init
};
});