家谱现有接口调试50%
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
(function (root, factory) {
|
||||
// 推广内容模块同时服务应用介绍、下载页和公告详情页。
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory(root);
|
||||
return;
|
||||
}
|
||||
|
||||
root.PromotionPages = factory(root);
|
||||
if (root.document) {
|
||||
root.document.addEventListener('DOMContentLoaded', function () {
|
||||
root.PromotionPages.init();
|
||||
});
|
||||
}
|
||||
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
||||
'use strict';
|
||||
|
||||
var documentRef = root.document;
|
||||
|
||||
function normalizeList(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) {
|
||||
// 标题、摘要、链接等普通文本进入 HTML 前统一转义。
|
||||
return String(value === undefined || value === null ? '' : value)
|
||||
.replace(/&/g, '&')
|
||||
.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 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 getPromotionId(item) {
|
||||
return String(pick(item, ['promotionId', 'id', 'articleId'], ''));
|
||||
}
|
||||
|
||||
function getTitle(item) {
|
||||
return pick(item, ['promotionTitle', 'title', 'name'], '推广内容');
|
||||
}
|
||||
|
||||
function getSummary(item) {
|
||||
return pick(item, ['summary', 'description', 'promotionDesc', 'remark'], '了解平台最新内容与应用服务。');
|
||||
}
|
||||
|
||||
function getContent(item) {
|
||||
return pick(item, ['promotionContent', 'content', 'articleContent'], '<p>暂无详细内容</p>');
|
||||
}
|
||||
|
||||
function getTime(item) {
|
||||
return pick(item, ['publishTime', 'createTime', 'updateTime'], '');
|
||||
}
|
||||
|
||||
function getLink(item, fallback) {
|
||||
return pick(item, ['linkUrl', 'url', 'targetUrl'], fallback || 'notice-detail.html?id=' + encodeURIComponent(getPromotionId(item)));
|
||||
}
|
||||
|
||||
function getCover(item) {
|
||||
return pick(item, ['coverUrl', 'coverImage', 'imageUrl', 'bannerUrl'], 'public/images/app-home.png');
|
||||
}
|
||||
|
||||
function buildPromotionCard(item) {
|
||||
var title = getTitle(item);
|
||||
var summary = getSummary(item);
|
||||
var time = getTime(item);
|
||||
var link = getLink(item);
|
||||
|
||||
return '<a class="promotion-card" href="' + escapeHtml(link) + '" data-promotion-id="' + escapeHtml(getPromotionId(item)) + '">' +
|
||||
'<img src="' + escapeHtml(getCover(item)) + '" alt="' + escapeHtml(title) + '" />' +
|
||||
'<div><h3>' + escapeHtml(title) + '</h3><p>' + escapeHtml(summary) + '</p><span>' + escapeHtml(time) + '</span></div></a>';
|
||||
}
|
||||
|
||||
function buildDownloadCard(item) {
|
||||
var title = getTitle(item);
|
||||
var summary = getSummary(item);
|
||||
var link = getLink(item, 'download.html');
|
||||
|
||||
return '<a class="card soft download-card tilt-card" href="' + escapeHtml(link) + '" data-promotion-id="' + escapeHtml(getPromotionId(item)) + '">' +
|
||||
'<h3>' + escapeHtml(title) + '</h3><p>' + escapeHtml(summary) + '</p></a>';
|
||||
}
|
||||
|
||||
function pickPromotion(list, promotionId) {
|
||||
var items = normalizeList(list);
|
||||
var id = String(promotionId || '');
|
||||
var index;
|
||||
|
||||
if (!items.length) return null;
|
||||
if (!id) return items[0];
|
||||
|
||||
for (index = 0; index < items.length; index += 1) {
|
||||
if (getPromotionId(items[index]) === id) return items[index];
|
||||
}
|
||||
|
||||
return items[0];
|
||||
}
|
||||
|
||||
function buildNoticeDetail(item) {
|
||||
return {
|
||||
title: getTitle(item),
|
||||
summary: getSummary(item),
|
||||
content: getContent(item),
|
||||
time: getTime(item)
|
||||
};
|
||||
}
|
||||
|
||||
function renderPromotionCards(items) {
|
||||
var container = query('[data-promotion-list]');
|
||||
var list = normalizeList(items);
|
||||
|
||||
if (!container) return;
|
||||
if (!list.length) {
|
||||
container.innerHTML = '<div class="api-empty">暂无推广内容</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = list.map(buildPromotionCard).join('');
|
||||
}
|
||||
|
||||
function renderDownloadCards(items) {
|
||||
var container = query('[data-promotion-download-list]');
|
||||
var list = normalizeList(items);
|
||||
|
||||
if (!container) return;
|
||||
if (!list.length) {
|
||||
container.innerHTML = '<div class="api-empty">暂无下载推广内容</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = list.map(buildDownloadCard).join('');
|
||||
}
|
||||
|
||||
function renderNoticeDetail(items) {
|
||||
var search = root.location && root.location.search;
|
||||
var selected = pickPromotion(items, getQueryParam(search, 'id'));
|
||||
var detail = selected ? buildNoticeDetail(selected) : null;
|
||||
var title = query('[data-promotion-title]');
|
||||
var summary = query('[data-promotion-summary]');
|
||||
var content = query('[data-promotion-content]');
|
||||
var time = query('[data-promotion-time]');
|
||||
|
||||
if (!detail) return;
|
||||
if (title) title.textContent = detail.title;
|
||||
if (summary) summary.textContent = detail.summary;
|
||||
if (content) content.innerHTML = detail.content;
|
||||
if (time) time.textContent = detail.time || '发布时间待定';
|
||||
}
|
||||
|
||||
async function loadPromotions() {
|
||||
var api = getApi();
|
||||
var data;
|
||||
|
||||
if (!api) return;
|
||||
|
||||
try {
|
||||
data = await api.promotions();
|
||||
renderPromotionCards(data);
|
||||
renderDownloadCards(data);
|
||||
renderNoticeDetail(data);
|
||||
} catch (error) {
|
||||
showMessage(error.message || '推广内容加载失败');
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (!documentRef || !query('[data-promotion-page]')) return;
|
||||
|
||||
loadPromotions();
|
||||
}
|
||||
|
||||
return {
|
||||
normalizeList: normalizeList,
|
||||
buildPromotionCard: buildPromotionCard,
|
||||
buildDownloadCard: buildDownloadCard,
|
||||
pickPromotion: pickPromotion,
|
||||
buildNoticeDetail: buildNoticeDetail,
|
||||
init: init
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user