(function (root, factory) { // 成长记录模块同时支持浏览器页面和 Node 单元测试。 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; function normalizeList(data) { // 成长记录列表兼容数组、rows、records、list、data。 if (Array.isArray(data)) return data; if (!data || typeof data !== 'object') return []; if (Array.isArray(data.rows)) return data.rows; if (Array.isArray(data.records)) return data.records; if (Array.isArray(data.list)) return data.list; if (Array.isArray(data.data)) return data.data; return []; } function pick(item, keys, fallback) { var source = item || {}; var index; var value; for (index = 0; index < keys.length; index += 1) { value = source[keys[index]]; if (value !== undefined && value !== null && value !== '') return value; } return fallback || ''; } function escapeHtml(value) { // 成长记录标题和内容进入 innerHTML 前统一转义。 return String(value === undefined || value === null ? '' : value) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); } function getApi() { return root.GenealogyApi && root.GenealogyApi.defaultClient; } function query(selector, rootNode) { return (rootNode || documentRef).querySelector(selector); } function queryAll(selector, rootNode) { return Array.prototype.slice.call((rootNode || documentRef).querySelectorAll(selector)); } function showMessage(message) { if (root.layui && root.layui.layer) { root.layui.layer.msg(message); return; } root.alert(message); } function getQueryParam(search, name) { var params = new URLSearchParams(String(search || '').replace(/^\?/, '')); return params.get(name) || ''; } function getCurrentGenealogyId() { var search = root.location && root.location.search; var page = query('[data-growth-page], [data-growth-edit-page]'); return getQueryParam(search, 'genealogyId') || (page && page.getAttribute('data-genealogy-id')) || getQueryParam(search, 'id') || ''; } function getCurrentRecordId() { var search = root.location && root.location.search; return getQueryParam(search, 'recordId') || ''; } function trimOrUndefined(value) { var text = String(value === undefined || value === null ? '' : value).trim(); return text || undefined; } function toNumber(value, fallback) { var text = String(value === undefined || value === null ? '' : value).trim(); var number = Number(text); return text && Number.isFinite(number) ? number : fallback; } function buildGrowthBody(values) { // GrowthRecordBody 来自 APP.openapi.json,recordTitle 必填。 var source = values || {}; return { lineagePersonId: toNumber(source.lineagePersonId), recordType: String(source.recordType || '').trim() || 'growth', recordTitle: String(source.recordTitle || '').trim(), recordContent: trimOrUndefined(source.recordContent), recordDate: trimOrUndefined(source.recordDate), remindTime: trimOrUndefined(source.remindTime), mediaOssIds: trimOrUndefined(source.mediaOssIds), sortOrder: toNumber(source.sortOrder, 1), status: String(source.status || '').trim() || '0' }; } function getRecordId(item) { return pick(item, ['recordId', 'id'], ''); } function buildGrowthRow(item, genealogyId) { var recordId = getRecordId(item); var title = pick(item, ['recordTitle', 'title'], '未命名成长记录'); var type = pick(item, ['recordType', 'type'], 'growth'); var date = pick(item, ['recordDate', 'createTime'], '未记录日期'); return '' + '

' + escapeHtml(title) + '

' + escapeHtml(type) + ' · ' + escapeHtml(date) + '

' + '编辑
'; } function renderGrowthRecords(items, genealogyId) { var container = query('[data-growth-list]'); var list = normalizeList(items); if (!container) return; if (!list.length) { container.innerHTML = '
暂无成长记录
'; return; } container.innerHTML = list.map(function (item) { return buildGrowthRow(item, genealogyId); }).join(''); } function getFormValues(form) { var values = {}; queryAll('[name]', form).forEach(function (field) { values[field.name] = field.value; }); return values; } function fillGrowthForm(record) { var source = record || {}; queryAll('[name]').forEach(function (field) { var value = pick(source, [field.name], ''); if (value !== '') field.value = value; }); } async function loadGrowthRecords() { var api = getApi(); var genealogyId = getCurrentGenealogyId(); var recordId = getCurrentRecordId(); if (!api || !genealogyId) return; try { if (query('[data-growth-list]')) { renderGrowthRecords(await api.growthRecords(genealogyId), genealogyId); } if (recordId && query('[data-growth-edit-page]')) { fillGrowthForm(await api.growthRecordDetail(genealogyId, recordId)); } } catch (error) { showMessage(error.message || '成长记录加载失败'); } } async function submitGrowth(form) { var api = getApi(); var genealogyId = getCurrentGenealogyId(); var recordId = getCurrentRecordId(); var body = buildGrowthBody(getFormValues(form)); if (!api || !genealogyId) return; if (!body.recordTitle) { showMessage('请填写成长记录标题'); return; } try { if (recordId) { await api.updateGrowthRecord(genealogyId, recordId, body); } else { await api.createGrowthRecord(genealogyId, body); } showMessage('成长记录已保存'); root.location.href = 'profile-growth.html?genealogyId=' + encodeURIComponent(genealogyId); } catch (error) { showMessage(error.message || '保存成长记录失败'); } } function bindActions() { if (!documentRef) return; documentRef.addEventListener('submit', function (event) { var form = event.target.closest('[data-growth-form]'); if (!form) return; event.preventDefault(); submitGrowth(form); }); } function init() { if (!documentRef) return; bindActions(); loadGrowthRecords(); } return { normalizeList: normalizeList, buildGrowthBody: buildGrowthBody, buildGrowthRow: buildGrowthRow, init: init }; });