等待后端更新接口
This commit is contained in:
+161
-24
@@ -91,7 +91,7 @@
|
||||
function renderPendingJoinApplies(data, canManage) {
|
||||
var items = normalizePendingApplies(data);
|
||||
|
||||
if (!items.length) return '<div class="api-empty">当前没有待审核申请</div>';
|
||||
if (!items.length) return '';
|
||||
return items.map(function (item) {
|
||||
var details = [
|
||||
item.phone ? '申请联系电话:' + item.phone : '',
|
||||
@@ -124,16 +124,103 @@
|
||||
else root.location.href = 'login.html';
|
||||
}
|
||||
|
||||
function setStatus(message) {
|
||||
function getApiStateType(error) {
|
||||
return Number(error && (error.status || error.code)) === 403 ? 'forbidden' : 'error';
|
||||
}
|
||||
|
||||
function setStatus(message, type) {
|
||||
var element = root.document && root.document.querySelector('[data-join-review-status]');
|
||||
|
||||
if (element) element.textContent = message || '';
|
||||
if (!element) return;
|
||||
if (!message) {
|
||||
element.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(element, type || 'error', message);
|
||||
return;
|
||||
}
|
||||
element.textContent = message;
|
||||
}
|
||||
|
||||
function setReviewFormStatus(message, type) {
|
||||
var element = root.document && root.document.querySelector('[data-join-review-form-status]');
|
||||
|
||||
if (!element) return;
|
||||
if (!message) {
|
||||
element.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(element, type || 'error', message);
|
||||
return;
|
||||
}
|
||||
element.textContent = message;
|
||||
}
|
||||
|
||||
function setPendingListState(message, type) {
|
||||
var element = root.document && root.document.querySelector('[data-join-apply-list="pending"]');
|
||||
|
||||
if (!element) return;
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(element, type || 'error', message);
|
||||
return;
|
||||
}
|
||||
element.textContent = message;
|
||||
}
|
||||
|
||||
function setReviewFormEnabled(enabled) {
|
||||
var form = root.document && root.document.querySelector('[data-join-review-form]');
|
||||
|
||||
if (!form) return;
|
||||
Array.prototype.slice.call(form.querySelectorAll('textarea, button')).forEach(function (field) {
|
||||
field.disabled = !enabled;
|
||||
});
|
||||
}
|
||||
|
||||
function setReviewSubmitPending(pending) {
|
||||
var button = root.document && root.document.querySelector('[data-join-review-submit]');
|
||||
|
||||
if (!button) return;
|
||||
if (!button.dataset.defaultLabel) button.dataset.defaultLabel = button.textContent;
|
||||
button.textContent = pending ? '正在提交…' : button.dataset.defaultLabel;
|
||||
button.setAttribute('aria-busy', pending ? 'true' : 'false');
|
||||
}
|
||||
|
||||
function openReviewForm(applyId, status) {
|
||||
var form = root.document && root.document.querySelector('[data-join-review-form]');
|
||||
var panel = root.document && root.document.querySelector('[data-join-review-editor]');
|
||||
var title = root.document && root.document.querySelector('[data-join-review-form-title]');
|
||||
var submit = root.document && root.document.querySelector('[data-join-review-submit]');
|
||||
|
||||
if (!form || !panel || !applyId || ['1', '2'].indexOf(status) < 0) return;
|
||||
form.reset();
|
||||
form.querySelector('[name="applyId"]').value = applyId;
|
||||
form.querySelector('[name="status"]').value = status;
|
||||
if (title) title.textContent = status === '1' ? '通过加入申请' : '拒绝加入申请';
|
||||
if (submit) submit.textContent = status === '1' ? '确认通过' : '确认拒绝';
|
||||
setReviewFormStatus('');
|
||||
panel.hidden = false;
|
||||
if (root.location && root.location.hash !== '#join-review-form') root.location.hash = 'join-review-form';
|
||||
if (form.querySelector('[name="auditRemark"]') && form.querySelector('[name="auditRemark"]').focus) {
|
||||
form.querySelector('[name="auditRemark"]').focus();
|
||||
}
|
||||
}
|
||||
|
||||
function closeReviewForm() {
|
||||
var form = root.document && root.document.querySelector('[data-join-review-form]');
|
||||
var panel = root.document && root.document.querySelector('[data-join-review-editor]');
|
||||
|
||||
if (form) form.reset();
|
||||
if (panel) panel.hidden = true;
|
||||
setReviewFormStatus('');
|
||||
}
|
||||
|
||||
async function initJoinReviewPage() {
|
||||
var page = root.document && root.document.querySelector('[data-join-review-page]');
|
||||
var list = root.document && root.document.querySelector('[data-join-apply-list="pending"]');
|
||||
var refresh = root.document && root.document.querySelector('[data-join-review-refresh]');
|
||||
var form = root.document && root.document.querySelector('[data-join-review-form]');
|
||||
var api = root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||||
var genealogyId = getCurrentGenealogyId();
|
||||
var canManage = false;
|
||||
@@ -141,7 +228,8 @@
|
||||
|
||||
if (!page) return;
|
||||
if (!genealogyId) {
|
||||
setStatus('请先从“我的家谱”选择要管理的家谱');
|
||||
setStatus('请先从“我的家谱”选择要管理的家谱', 'forbidden');
|
||||
setReviewFormEnabled(false);
|
||||
list.innerHTML = '<a class="module-row" href="profile-families.html?next=profile-join-review.html">选择家谱后继续</a>';
|
||||
return;
|
||||
}
|
||||
@@ -156,19 +244,32 @@
|
||||
async function loadPending() {
|
||||
var data;
|
||||
|
||||
setStatus('正在读取待审核申请…');
|
||||
setStatus('正在读取待审核申请…', 'loading');
|
||||
try {
|
||||
data = await api.pendingGenealogyJoinApplies(genealogyId);
|
||||
if (!Array.isArray(data) || (data.length && !normalizePendingApplies(data).length)) {
|
||||
setPendingListState('待审核申请响应字段不完整,请联系后端核对。', 'error');
|
||||
setStatus('待审核申请响应字段不完整,请联系后端核对。', 'error');
|
||||
return [];
|
||||
}
|
||||
if (!data.length) {
|
||||
setPendingListState('当前没有待审核申请', 'empty');
|
||||
setStatus('');
|
||||
return [];
|
||||
}
|
||||
list.innerHTML = renderPendingJoinApplies(data, canManage);
|
||||
setStatus(canManage ? '' : '当前账号没有审核该家谱申请的权限');
|
||||
setStatus(canManage ? '' : '当前账号没有审核该家谱申请的权限', canManage ? undefined : 'forbidden');
|
||||
return normalizePendingApplies(data);
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return [];
|
||||
}
|
||||
list.innerHTML = '<div class="api-empty">读取待审核申请失败</div>';
|
||||
setStatus(error.message || '读取待审核申请失败,请稍后重试');
|
||||
setPendingListState(
|
||||
getApiStateType(error) === 'forbidden' ? '当前账号没有审核该家谱申请的权限。' : '读取待审核申请失败,请稍后重试。',
|
||||
getApiStateType(error)
|
||||
);
|
||||
setStatus(error.message || '读取待审核申请失败,请稍后重试', getApiStateType(error));
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -178,8 +279,9 @@
|
||||
|
||||
canManage = detail && detail.canManage === true;
|
||||
if (!canManage) {
|
||||
list.innerHTML = '<div class="api-empty">当前账号没有审核该家谱申请的权限</div>';
|
||||
setStatus('当前账号没有审核该家谱申请的权限');
|
||||
setPendingListState('当前账号没有审核该家谱申请的权限。', 'forbidden');
|
||||
setStatus('当前账号没有审核该家谱申请的权限', 'forbidden');
|
||||
setReviewFormEnabled(false);
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -187,55 +289,90 @@
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setStatus(error.message || '无法核验当前家谱管理权限');
|
||||
setStatus(error.message || '无法核验当前家谱管理权限', getApiStateType(error));
|
||||
setPendingListState(
|
||||
getApiStateType(error) === 'forbidden' ? '当前账号没有审核该家谱申请的权限。' : '无法读取待审核申请,请稍后重试。',
|
||||
getApiStateType(error)
|
||||
);
|
||||
setReviewFormEnabled(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setReviewFormEnabled(true);
|
||||
|
||||
refresh.addEventListener('click', function () {
|
||||
loadPending();
|
||||
});
|
||||
list.addEventListener('click', async function (event) {
|
||||
list.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-join-audit-id]');
|
||||
var applyId;
|
||||
var status;
|
||||
var auditRemark = '';
|
||||
var body;
|
||||
var validation;
|
||||
var refreshed;
|
||||
|
||||
if (!button || !list.contains(button) || writePending || !canManage) return;
|
||||
applyId = normalizeId(button.getAttribute('data-join-audit-id'));
|
||||
status = button.getAttribute('data-join-audit-status');
|
||||
if (!applyId || ['1', '2'].indexOf(status) < 0) return;
|
||||
if (status === '1' && root.confirm && !root.confirm('确认通过这条加入申请吗?')) return;
|
||||
if (status === '2' && root.prompt) {
|
||||
auditRemark = root.prompt('可填写拒绝原因(最多 500 个字符)', '') || '';
|
||||
openReviewForm(applyId, status);
|
||||
});
|
||||
form.addEventListener('submit', async function (event) {
|
||||
var values;
|
||||
var applyId;
|
||||
var body;
|
||||
var validation;
|
||||
var refreshed;
|
||||
var forbidden = false;
|
||||
|
||||
event.preventDefault();
|
||||
if (writePending || !canManage) return;
|
||||
values = {};
|
||||
Array.prototype.slice.call(form.querySelectorAll('[name]')).forEach(function (field) {
|
||||
values[field.name] = field.value;
|
||||
});
|
||||
applyId = normalizeId(values.applyId);
|
||||
body = buildJoinAuditBody(values);
|
||||
if (!applyId) {
|
||||
setReviewFormStatus('申请编号无效,请返回列表重新操作。', 'error');
|
||||
return;
|
||||
}
|
||||
body = buildJoinAuditBody({ status: status, auditRemark: auditRemark });
|
||||
validation = validateJoinAuditBody(body);
|
||||
if (validation) {
|
||||
setStatus(validation);
|
||||
setReviewFormStatus(validation, 'error');
|
||||
return;
|
||||
}
|
||||
writePending = true;
|
||||
button.disabled = true;
|
||||
setStatus('正在提交审核结果…');
|
||||
setReviewFormEnabled(false);
|
||||
setReviewSubmitPending(true);
|
||||
setReviewFormStatus('正在提交审核结果…', 'loading');
|
||||
try {
|
||||
await api.auditGenealogyJoinApply(genealogyId, applyId, body);
|
||||
refreshed = await loadPending();
|
||||
if (refreshed.some(function (item) { return item.applyId === applyId; })) {
|
||||
throw new Error('审核后申请仍在待审核列表');
|
||||
}
|
||||
closeReviewForm();
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setStatus(error.message || '提交审核结果失败,请稍后重试');
|
||||
forbidden = getApiStateType(error) === 'forbidden';
|
||||
setReviewFormStatus(
|
||||
forbidden ? '当前账号无权审核该家谱申请。' : (error.message || '提交审核结果失败,请稍后重试。'),
|
||||
getApiStateType(error)
|
||||
);
|
||||
if (forbidden) {
|
||||
canManage = false;
|
||||
setStatus('当前账号无权审核该家谱申请。', 'forbidden');
|
||||
}
|
||||
} finally {
|
||||
writePending = false;
|
||||
setReviewSubmitPending(false);
|
||||
if (!forbidden) setReviewFormEnabled(true);
|
||||
}
|
||||
});
|
||||
root.document.querySelector('[data-join-review-cancel]').addEventListener('click', function () {
|
||||
if (!writePending) closeReviewForm();
|
||||
});
|
||||
await loadPending();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user