等待后端更新接口
This commit is contained in:
@@ -86,12 +86,16 @@
|
||||
function renderPromotionList(data) {
|
||||
var items = normalizePromotionList(data);
|
||||
|
||||
if (!items.length) return '<div class="api-empty">当前暂无应用推广</div>';
|
||||
return items.map(renderPromotionCard).join('');
|
||||
}
|
||||
|
||||
function renderPromotionLoginRequired() {
|
||||
return '<div class="api-empty">登录后可查看应用推广,<a href="login.html">立即登录</a></div>';
|
||||
function setPromotionState(container, type, message) {
|
||||
if (!container) return;
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(container, type, message);
|
||||
return;
|
||||
}
|
||||
container.textContent = message;
|
||||
}
|
||||
|
||||
async function loadPromotions(api) {
|
||||
@@ -111,6 +115,10 @@
|
||||
return Number(error && (error.status || error.code)) === 401;
|
||||
}
|
||||
|
||||
function isForbidden(error) {
|
||||
return Number(error && (error.status || error.code)) === 403;
|
||||
}
|
||||
|
||||
async function init() {
|
||||
var page = root.document && root.document.querySelector('[data-promotion-page]');
|
||||
var list = root.document && root.document.querySelector('[data-promotion-list]');
|
||||
@@ -118,22 +126,30 @@
|
||||
|
||||
if (!page || !list) return;
|
||||
if (!api || !api.getToken || !api.getToken()) {
|
||||
list.innerHTML = renderPromotionLoginRequired();
|
||||
setPromotionState(list, 'forbidden', '登录后可查看应用推广。');
|
||||
return;
|
||||
}
|
||||
|
||||
setPromotionState(list, 'loading', '正在读取应用推广…');
|
||||
try {
|
||||
list.classList.add('is-loading');
|
||||
list.innerHTML = renderPromotionList(await loadPromotions(api));
|
||||
var items = await loadPromotions(api);
|
||||
|
||||
if (!items.length) {
|
||||
setPromotionState(list, 'empty', '当前暂无应用推广。');
|
||||
return;
|
||||
}
|
||||
list.innerHTML = renderPromotionList(items);
|
||||
} catch (error) {
|
||||
if (isUnauthorized(error)) {
|
||||
if (api.clearToken) api.clearToken();
|
||||
list.innerHTML = renderPromotionLoginRequired();
|
||||
setPromotionState(list, 'forbidden', '登录后可查看应用推广。');
|
||||
return;
|
||||
}
|
||||
list.innerHTML = '<div class="api-empty">应用推广读取失败,请稍后重试</div>';
|
||||
} finally {
|
||||
list.classList.remove('is-loading');
|
||||
setPromotionState(
|
||||
list,
|
||||
isForbidden(error) ? 'forbidden' : 'error',
|
||||
isForbidden(error) ? '当前账号无权查看应用推广。' : '应用推广读取失败,请稍后重试。'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,9 +158,10 @@
|
||||
normalizePromotionList: normalizePromotionList,
|
||||
normalizeTargetUrl: normalizeTargetUrl,
|
||||
renderPromotionList: renderPromotionList,
|
||||
renderPromotionLoginRequired: renderPromotionLoginRequired,
|
||||
setPromotionState: setPromotionState,
|
||||
loadPromotions: loadPromotions,
|
||||
isUnauthorized: isUnauthorized,
|
||||
isForbidden: isForbidden,
|
||||
init: init
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user