feat(api): 添加帮助中心反馈系统和VIP服务功能
- 在ApiClient中新增submitFeedback、myFeedback、helpArticles、helpArticleDetail、 siteArticles、promotions、vipPackages、createVipOrder、vipOrders等方法 - 添加帮助文章和站点资讯的参数验证逻辑 - 更新测试文件添加新的API方法测试用例 - 在HTML页面中添加反馈、帮助和VIP服务相关页面的脚本引用 - 更新加入家谱页面为完整的申请流程界面 - 修改资讯详情页面为站点资讯展示页面 - 更新AxiosRequestUtil中认证处理逻辑 - 添加世系树渲染的HTML生成函数用于页面复用 - 更新文档中的API契约说明和页面规划
This commit is contained in:
@@ -0,0 +1,407 @@
|
||||
(function (root, factory) {
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory(root);
|
||||
return;
|
||||
}
|
||||
|
||||
root.JoinPages = factory(root);
|
||||
if (root.document) {
|
||||
root.document.addEventListener('DOMContentLoaded', function () {
|
||||
root.JoinPages.init();
|
||||
});
|
||||
}
|
||||
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
||||
'use strict';
|
||||
|
||||
var APPLY_STATUS = {
|
||||
'0': '待审核',
|
||||
'1': '已通过',
|
||||
'2': '已拒绝',
|
||||
'3': '已撤销'
|
||||
};
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value === undefined || value === null ? '' : value)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function normalizeId(value) {
|
||||
var text;
|
||||
|
||||
if (typeof value === 'number' && !Number.isSafeInteger(value)) return '';
|
||||
if (value === undefined || value === null) return '';
|
||||
text = String(value).trim();
|
||||
return /^[1-9][0-9]*$/.test(text) ? text : '';
|
||||
}
|
||||
|
||||
function optionalText(value) {
|
||||
var text;
|
||||
|
||||
if (value === undefined || value === null) return '';
|
||||
text = String(value).trim();
|
||||
return text;
|
||||
}
|
||||
|
||||
function buildJoinApplyBody(values) {
|
||||
var source = values || {};
|
||||
var body = {};
|
||||
|
||||
['applicantName', 'phone', 'relationDesc', 'applyReason'].forEach(function (name) {
|
||||
var value = optionalText(source[name]);
|
||||
|
||||
if (value) body[name] = value;
|
||||
});
|
||||
return body;
|
||||
}
|
||||
|
||||
function validateJoinApplyBody(body) {
|
||||
var source = body || {};
|
||||
var limits = {
|
||||
applicantName: [50, '申请人姓名不能超过 50 个字符'],
|
||||
phone: [30, '联系电话不能超过 30 个字符'],
|
||||
relationDesc: [100, '关系说明不能超过 100 个字符'],
|
||||
applyReason: [500, '申请理由不能超过 500 个字符']
|
||||
};
|
||||
var names = Object.keys(limits);
|
||||
var index;
|
||||
|
||||
for (index = 0; index < names.length; index += 1) {
|
||||
if (String(source[names[index]] || '').length > limits[names[index]][0]) {
|
||||
return limits[names[index]][1];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function normalizeJoinGenealogy(item) {
|
||||
var source = item || {};
|
||||
var genealogyId = normalizeId(source.genealogyId);
|
||||
var genealogyName = optionalText(source.genealogyName);
|
||||
var surname = optionalText(source.surname);
|
||||
var status = source.status === undefined || source.status === null ? '' : String(source.status);
|
||||
|
||||
if (!genealogyId || !genealogyName || !surname || (status && status !== '0')) return null;
|
||||
return {
|
||||
genealogyId: genealogyId,
|
||||
genealogyName: genealogyName,
|
||||
surname: surname,
|
||||
regionFullName: optionalText(source.regionFullName),
|
||||
memberCount: source.memberCount,
|
||||
joinMode: source.joinMode === undefined || source.joinMode === null ? '' : String(source.joinMode)
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeJoinApply(item) {
|
||||
var source = item || {};
|
||||
var applyId = normalizeId(source.applyId);
|
||||
var genealogyId = normalizeId(source.genealogyId);
|
||||
var genealogyName = optionalText(source.genealogyName);
|
||||
var status = source.status === undefined || source.status === null ? '' : String(source.status);
|
||||
|
||||
if (!applyId || !genealogyId || !genealogyName || !Object.prototype.hasOwnProperty.call(APPLY_STATUS, status)) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
applyId: applyId,
|
||||
genealogyId: genealogyId,
|
||||
genealogyName: genealogyName,
|
||||
surname: optionalText(source.surname),
|
||||
relationDesc: optionalText(source.relationDesc),
|
||||
applyReason: optionalText(source.applyReason),
|
||||
auditRemark: optionalText(source.auditRemark),
|
||||
auditTime: optionalText(source.auditTime),
|
||||
status: status
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeJoinApplies(data) {
|
||||
var items;
|
||||
|
||||
if (!Array.isArray(data)) return [];
|
||||
items = data.map(normalizeJoinApply);
|
||||
return items.some(function (item) { return !item; }) ? [] : items;
|
||||
}
|
||||
|
||||
function renderJoinGenealogyOptions(data) {
|
||||
var items;
|
||||
|
||||
if (!Array.isArray(data)) return '<div class="api-empty">家谱列表数据无效,请刷新重试</div>';
|
||||
items = data.map(normalizeJoinGenealogy).filter(Boolean);
|
||||
if (!items.length) return '<div class="api-empty">没有找到可申请加入的家谱</div>';
|
||||
return items.map(function (item) {
|
||||
var meta = [
|
||||
item.surname + '氏',
|
||||
item.regionFullName,
|
||||
item.memberCount === undefined || item.memberCount === null ? '' : item.memberCount + ' 位成员'
|
||||
].filter(Boolean).join(' · ');
|
||||
|
||||
return '<button class="module-row" type="button" data-join-genealogy-id="' +
|
||||
escapeHtml(item.genealogyId) + '" data-join-genealogy-name="' +
|
||||
escapeHtml(item.genealogyName) + '"><span><strong>' +
|
||||
escapeHtml(item.genealogyName) + '</strong><small>' +
|
||||
escapeHtml(meta) + '</small></span><span class="pill">选择</span></button>';
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function renderMyJoinApplies(data) {
|
||||
var items = normalizeJoinApplies(data);
|
||||
|
||||
if (!items.length) return '<div class="api-empty">当前没有加入申请</div>';
|
||||
return items.map(function (item) {
|
||||
var details = [
|
||||
item.surname ? item.surname + '氏' : '',
|
||||
item.relationDesc,
|
||||
item.applyReason,
|
||||
item.auditRemark ? '审核说明:' + item.auditRemark : '',
|
||||
item.auditTime ? '审核时间:' + item.auditTime : ''
|
||||
].filter(Boolean).join(' · ');
|
||||
var action = item.status === '0'
|
||||
? '<button class="btn ghost" type="button" data-join-cancel-id="' +
|
||||
escapeHtml(item.applyId) + '">撤销申请</button>'
|
||||
: '';
|
||||
|
||||
return '<article class="module-row"><div><h3>' + escapeHtml(item.genealogyName) +
|
||||
'</h3><p>' + escapeHtml(details || '等待申请状态更新') +
|
||||
'</p></div><div><span class="pill">' + APPLY_STATUS[item.status] +
|
||||
'</span>' + action + '</div></article>';
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function shouldRedirectToLogin(api, error) {
|
||||
var status = error && (error.status || error.code);
|
||||
|
||||
return !api || !api.getToken || !api.getToken() || Number(status) === 401;
|
||||
}
|
||||
|
||||
function redirectToLogin(api) {
|
||||
if (api && api.clearToken) api.clearToken();
|
||||
if (!root.location) return;
|
||||
if (typeof root.location.replace === 'function') root.location.replace('login.html');
|
||||
else root.location.href = 'login.html';
|
||||
}
|
||||
|
||||
function setText(selector, message) {
|
||||
var element = root.document && root.document.querySelector(selector);
|
||||
|
||||
if (element) element.textContent = message || '';
|
||||
}
|
||||
|
||||
function getFormValues(form) {
|
||||
var values = {};
|
||||
|
||||
Array.prototype.forEach.call(form.querySelectorAll('[name]'), function (field) {
|
||||
values[field.name] = field.value;
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
function canJoinGenealogy(quota) {
|
||||
var remaining = Number(quota && quota.joinRemaining);
|
||||
|
||||
if (quota && typeof quota.canJoin === 'boolean') return quota.canJoin;
|
||||
return Number.isInteger(remaining) && (remaining === -1 || remaining > 0);
|
||||
}
|
||||
|
||||
async function initJoinApplyPage() {
|
||||
var page = root.document && root.document.querySelector('[data-join-apply-page]');
|
||||
var searchForm = root.document && root.document.querySelector('[data-join-genealogy-search]');
|
||||
var options = root.document && root.document.querySelector('[data-join-genealogy-options]');
|
||||
var applyForm = root.document && root.document.querySelector('[data-join-apply-form]');
|
||||
var selected = root.document && root.document.querySelector('[data-join-selected-genealogy]');
|
||||
var api = root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||||
var selectedGenealogyId = '';
|
||||
var writePending = false;
|
||||
|
||||
if (!page) return;
|
||||
if (shouldRedirectToLogin(api)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
|
||||
async function loadOptions(keyword) {
|
||||
var data;
|
||||
|
||||
setText('[data-join-apply-status]', '正在读取可申请家谱…');
|
||||
try {
|
||||
data = await api.genealogyOptions(keyword ? { keyword: keyword } : {});
|
||||
options.innerHTML = renderJoinGenealogyOptions(data);
|
||||
setText('[data-join-apply-status]', '');
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
options.innerHTML = '<div class="api-empty">读取家谱列表失败</div>';
|
||||
setText('[data-join-apply-status]', error.message || '读取家谱列表失败,请稍后重试');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (!canJoinGenealogy(await api.genealogyQuota())) {
|
||||
setText('[data-join-apply-status]', '当前没有可用的家谱加入额度');
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = true;
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setText('[data-join-apply-status]', error.message || '无法读取家谱加入额度');
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
searchForm.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
loadOptions(optionalText(searchForm.querySelector('[name="keyword"]').value));
|
||||
});
|
||||
options.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-join-genealogy-id]');
|
||||
|
||||
if (!button || !options.contains(button)) return;
|
||||
selectedGenealogyId = normalizeId(button.getAttribute('data-join-genealogy-id'));
|
||||
if (!selectedGenealogyId) return;
|
||||
selected.textContent = '已选择:' + button.getAttribute('data-join-genealogy-name');
|
||||
Array.prototype.forEach.call(options.querySelectorAll('[data-join-genealogy-id]'), function (item) {
|
||||
item.classList.toggle('is-selected', item === button);
|
||||
});
|
||||
});
|
||||
applyForm.addEventListener('submit', async function (event) {
|
||||
var body;
|
||||
var validation;
|
||||
var created;
|
||||
var refreshed;
|
||||
|
||||
event.preventDefault();
|
||||
if (writePending) return;
|
||||
if (!selectedGenealogyId) {
|
||||
setText('[data-join-apply-status]', '请先从家谱列表中选择要加入的家谱');
|
||||
return;
|
||||
}
|
||||
body = buildJoinApplyBody(getFormValues(applyForm));
|
||||
validation = validateJoinApplyBody(body);
|
||||
if (validation) {
|
||||
setText('[data-join-apply-status]', validation);
|
||||
return;
|
||||
}
|
||||
writePending = true;
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = true;
|
||||
setText('[data-join-apply-status]', '正在提交申请…');
|
||||
try {
|
||||
created = normalizeJoinApply(await api.applyToGenealogy(selectedGenealogyId, body));
|
||||
if (!created) throw new Error('申请响应缺少有效申请信息');
|
||||
refreshed = normalizeJoinApplies(await api.myGenealogyJoinApplies());
|
||||
if (!refreshed.some(function (item) { return item.applyId === created.applyId; })) {
|
||||
throw new Error('提交后无法读取同一申请');
|
||||
}
|
||||
if (root.location) root.location.href = 'profile-join-family.html';
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setText('[data-join-apply-status]', error.message || '提交加入申请失败,请稍后重试');
|
||||
} finally {
|
||||
writePending = false;
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = false;
|
||||
}
|
||||
});
|
||||
await loadOptions('');
|
||||
}
|
||||
|
||||
async function initMyJoinAppliesPage() {
|
||||
var page = root.document && root.document.querySelector('[data-my-join-applies-page]');
|
||||
var list = root.document && root.document.querySelector('[data-join-apply-list="mine"]');
|
||||
var refresh = root.document && root.document.querySelector('[data-join-apply-refresh]');
|
||||
var api = root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||||
var writePending = false;
|
||||
|
||||
if (!page) return;
|
||||
if (shouldRedirectToLogin(api)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
|
||||
async function loadApplies() {
|
||||
var data;
|
||||
|
||||
setText('[data-my-join-applies-status]', '正在读取申请记录…');
|
||||
try {
|
||||
data = await api.myGenealogyJoinApplies();
|
||||
list.innerHTML = renderMyJoinApplies(data);
|
||||
setText('[data-my-join-applies-status]', '');
|
||||
return normalizeJoinApplies(data);
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return [];
|
||||
}
|
||||
list.innerHTML = '<div class="api-empty">读取加入申请失败</div>';
|
||||
setText('[data-my-join-applies-status]', error.message || '读取加入申请失败,请稍后重试');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
refresh.addEventListener('click', function () {
|
||||
loadApplies();
|
||||
});
|
||||
list.addEventListener('click', async function (event) {
|
||||
var button = event.target.closest('[data-join-cancel-id]');
|
||||
var applyId;
|
||||
var refreshed;
|
||||
|
||||
if (!button || !list.contains(button) || writePending) return;
|
||||
applyId = normalizeId(button.getAttribute('data-join-cancel-id'));
|
||||
if (!applyId) return;
|
||||
if (root.confirm && !root.confirm('确认撤销这条加入申请吗?')) return;
|
||||
writePending = true;
|
||||
button.disabled = true;
|
||||
setText('[data-my-join-applies-status]', '正在撤销申请…');
|
||||
try {
|
||||
await api.cancelGenealogyJoinApply(applyId);
|
||||
refreshed = await loadApplies();
|
||||
if (refreshed.some(function (item) { return item.applyId === applyId && item.status === '0'; })) {
|
||||
throw new Error('撤销后申请仍处于待审核状态');
|
||||
}
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setText('[data-my-join-applies-status]', error.message || '撤销加入申请失败,请稍后重试');
|
||||
} finally {
|
||||
writePending = false;
|
||||
}
|
||||
});
|
||||
await loadApplies();
|
||||
}
|
||||
|
||||
async function init() {
|
||||
if (root.document && root.document.querySelector('[data-join-apply-page]')) {
|
||||
await initJoinApplyPage();
|
||||
return;
|
||||
}
|
||||
if (root.document && root.document.querySelector('[data-my-join-applies-page]')) {
|
||||
await initMyJoinAppliesPage();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
buildJoinApplyBody: buildJoinApplyBody,
|
||||
validateJoinApplyBody: validateJoinApplyBody,
|
||||
normalizeJoinGenealogy: normalizeJoinGenealogy,
|
||||
normalizeJoinApply: normalizeJoinApply,
|
||||
normalizeJoinApplies: normalizeJoinApplies,
|
||||
renderJoinGenealogyOptions: renderJoinGenealogyOptions,
|
||||
renderMyJoinApplies: renderMyJoinApplies,
|
||||
canJoinGenealogy: canJoinGenealogy,
|
||||
initJoinApplyPage: initJoinApplyPage,
|
||||
initMyJoinAppliesPage: initMyJoinAppliesPage,
|
||||
init: init
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user