Files
jiapu/public/js/genealogy-lifecycle-pages.js
T
2026-08-29 19:06:07 +08:00

140 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(function (root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory(root);
return;
}
root.GenealogyLifecyclePages = factory(root);
if (root.document) root.document.addEventListener('DOMContentLoaded', root.GenealogyLifecyclePages.init);
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
'use strict';
function confirmAction(message) {
if (root.ProfileUI && typeof root.ProfileUI.confirmAction === 'function') {
return root.ProfileUI.confirmAction(message);
}
return Promise.resolve(!root['confirm'] || root['confirm'](message));
}
var DISABLED_REASON_TEXT = {
NOT_OWNER: '只有谱主可以永久删除家谱',
NO_VERIFIED_MOBILE: '谱主账号尚未绑定已验证手机号',
ACTIVE_PAYMENT: '当前家谱存在未完成的支付单',
DELETION_IN_PROGRESS: '永久删除任务正在处理中'
};
function normalizeCapability(value) {
var source = value || {};
return {
canDeletePermanently: source.canDeletePermanently === true,
verifiedMobileMasked: String(source.verifiedMobileMasked || '').trim(),
disabledReasons: Array.isArray(source.disabledReasons) ? source.disabledReasons.map(String) : []
};
}
function capabilityMessage(capability) {
var data = normalizeCapability(capability);
if (data.canDeletePermanently) {
return data.verifiedMobileMasked ? '验证码将发送至 ' + data.verifiedMobileMasked : '当前账号具备永久删除资格';
}
return data.disabledReasons.map(function (code) {
return DISABLED_REASON_TEXT[code] || code;
}).join('') || '当前家谱不允许永久删除';
}
async function init() {
var page = root.document && root.document.querySelector('[data-genealogy-lifecycle-page]');
var api = root.GenealogyApi && root.GenealogyApi.defaultClient;
var genealogyId = root.ProfileUI && root.ProfileUI.getGenealogyId ? root.ProfileUI.getGenealogyId() : '';
var status = page && page.querySelector('[data-lifecycle-status]');
var archive = page && page.querySelector('[data-genealogy-archive]');
var restore = page && page.querySelector('[data-genealogy-restore]');
var deleteForm = page && page.querySelector('[data-genealogy-delete-form]');
var sendCode = page && page.querySelector('[data-genealogy-delete-code]');
var submitDelete = page && page.querySelector('[data-genealogy-delete-submit]');
var nameInput = page && page.querySelector('[data-genealogy-delete-name]');
var codeInput = page && page.querySelector('[data-genealogy-delete-sms]');
var capability;
var pending = false;
if (!page) return;
if (!api || !api.getToken || !api.getToken()) {
if (root.location) root.location.href = 'login.html';
return;
}
if (!genealogyId) {
root.ProfileUI.setApiState(status, 'error', '缺少家谱上下文,请从“我的家谱”重新进入。');
return;
}
function setStatus(type, message) {
if (type) root.ProfileUI.setApiState(status, type, message);
else status.textContent = message || '';
}
function setPending(value) {
pending = value;
[archive, restore, sendCode, submitDelete].forEach(function (button) {
if (button) button.disabled = value ||
((button === submitDelete || button === sendCode) && capability && !capability.canDeletePermanently);
});
}
try {
var initial = await Promise.all([api.genealogyDetail(genealogyId), api.genealogyDeletionCapability(genealogyId)]);
capability = normalizeCapability(initial[1]);
nameInput.placeholder = '请输入“' + String(initial[0] && initial[0].genealogyName || '家谱全名') + '”确认';
submitDelete.disabled = !capability.canDeletePermanently;
sendCode.disabled = !capability.canDeletePermanently;
setStatus(capability.canDeletePermanently ? '' : 'forbidden', capabilityMessage(capability));
} catch (error) {
setStatus('error', error.message || '家谱维护能力读取失败');
return;
}
archive.addEventListener('click', async function () {
if (pending || !await confirmAction('归档后家谱将停止日常维护,确认归档吗?')) return;
setPending(true); setStatus('loading', '正在归档家谱…');
try { await api.archiveGenealogy(genealogyId); setStatus('', '家谱已归档,需要时可在此恢复。'); }
catch (error) { setStatus('error', error.message || '归档失败'); }
finally { setPending(false); }
});
restore.addEventListener('click', async function () {
if (pending) return;
setPending(true); setStatus('loading', '正在恢复家谱…');
try { await api.restoreGenealogy(genealogyId); setStatus('', '家谱已恢复。'); }
catch (error) { setStatus('error', error.message || '恢复失败'); }
finally { setPending(false); }
});
sendCode.addEventListener('click', async function () {
if (pending || !capability.canDeletePermanently) return;
setPending(true); setStatus('loading', '正在发送验证码…');
try { await api.sendGenealogyDeletionCode(genealogyId, {}); setStatus('', '验证码已发送至谱主手机号。'); }
catch (error) { setStatus('error', error.message || '验证码发送失败'); }
finally { setPending(false); }
});
deleteForm.addEventListener('submit', async function (event) {
event.preventDefault();
if (pending || !capability.canDeletePermanently) return;
if (!nameInput.value.trim() || !codeInput.value.trim()) {
setStatus('error', '请填写家谱全名和短信验证码');
return;
}
if (!await confirmAction('永久删除不可恢复,确认继续吗?')) return;
setPending(true); setStatus('loading', '正在提交永久删除任务…');
try {
await api.permanentlyDeleteGenealogy(genealogyId, {
genealogyName: nameInput.value.trim(), smsCode: codeInput.value.trim()
});
if (root.ProfileUI && root.ProfileUI.clearCurrentGenealogyContext) root.ProfileUI.clearCurrentGenealogyContext();
if (root.location) root.location.href = 'profile-families.html';
} catch (error) { setStatus('error', error.message || '永久删除提交失败'); }
finally { setPending(false); }
});
}
return { normalizeCapability: normalizeCapability, capabilityMessage: capabilityMessage, init: init };
});