Files
jiapu/dist/public/js/help-pages.js
T

2 lines
4.6 KiB
JavaScript

(function(i,d){if(typeof module=="object"&&module.exports){module.exports=d(i);return}i.HelpPages=d(i),i.document&&i.document.addEventListener("DOMContentLoaded",function(){i.HelpPages.init()})})(typeof globalThis!="undefined"?globalThis:window,function(i){"use strict";function d(e){return String(e==null?"":e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}function A(e){var t;return typeof e=="number"&&!Number.isSafeInteger(e)||e==null?"":(t=String(e).trim(),/^[1-9][0-9]*$/.test(t)?t:"")}function S(e){var t;return e==null||e===""?"0":typeof e=="number"&&(!Number.isSafeInteger(e)||e<0)?"":(t=String(e).trim(),/^(?:0|[1-9][0-9]*)$/.test(t)?t:"")}function m(e){return e==null?"":String(e).trim()}function p(e){var t=e||{},n=A(t.helpId),l=m(t.helpTitle),o=m(t.helpContent),c=S(t.viewCount);return!n||!l||!o||!c||String(t.status)!=="0"?null:{helpId:n,helpCategory:m(t.helpCategory),helpTitle:l,helpContent:o,viewCount:c}}function y(e){var t;return Array.isArray(e)?(t=e.map(p),t.some(function(n){return!n})?[]:t):[]}function w(e){var t=y(e);return t.length?t.map(function(n){var l=[n.helpCategory,"浏览 "+n.viewCount+" 次"].filter(Boolean).join(" · ");return'<details class="tilt-card" data-help-article-id="'+d(n.helpId)+'"><summary>'+d(n.helpTitle)+"</summary><p>"+d(l)+'</p><div class="help-detail" data-help-detail-panel="'+d(n.helpId)+'"><button class="btn ghost" type="button" data-help-detail-id="'+d(n.helpId)+'">查看完整解答</button></div></details>'}).join(""):'<div class="api-empty">当前暂无帮助文章</div>'}function b(e){var t=p(e),n;return t?(n=[t.helpCategory,"浏览 "+t.viewCount+" 次"].filter(Boolean).join(" · "),'<div class="help-detail-content"><p class="module-meta">'+d(n)+"</p><p>"+d(t.helpContent).replace(/\r?\n/g,"<br />")+"</p></div>"):'<div class="api-empty">帮助文章不存在或已停用</div>'}async function L(e,t){var n=A(t),l;if(!e||typeof e.helpArticleDetail!="function")throw new Error("帮助文章接口不可用");if(!n)throw new Error("帮助文章编号无效");if(l=p(await e.helpArticleDetail(n)),!l)throw new Error("帮助文章详情响应无效");if(l.helpId!==n)throw new Error("详情响应与请求编号不一致");return l}async function C(e){var t,n;if(!e||typeof e.helpArticles!="function")throw new Error("帮助文章接口不可用");if(t=await e.helpArticles(),n=y(t),!Array.isArray(t)||n.length!==t.length)throw new Error("帮助文章列表响应无效");return n}function h(e,t){var n=t&&(t.status||t.code);return!e||!e.getToken||!e.getToken()||Number(n)===401}function v(e){e&&e.clearToken&&e.clearToken(),i.NavigationUtil.open("login.html")}function s(e){return Number(e&&(e.status||e.code))===403}async function T(){var e=i.document&&i.document.querySelector("[data-help-list]"),t=i.document&&i.document.querySelector("[data-help-status]"),n=i.document&&i.document.querySelector("[data-help-refresh]"),l=i.GenealogyApi&&i.GenealogyApi.defaultClient;if(!e)return;if(h(l)){v(l);return}function o(r,a,u){if(r){if(i.ProfileUI&&i.ProfileUI.setApiState&&a){i.ProfileUI.setApiState(r,a,u);return}r.textContent=u||""}}function c(r,a){o(t,r,a)}function E(r){if(!r.length){o(e,"empty","当前暂无帮助文章。");return}e.innerHTML=w(r)}function H(r,a,u){r&&(r.dataset.defaultLabel||(r.dataset.defaultLabel=r.textContent),r.disabled=a,r.textContent=a?u:r.dataset.defaultLabel)}async function I(){try{e.classList.add("is-loading"),o(e,"loading","正在读取帮助文章…"),c("loading","正在读取帮助文章…"),E(await C(l)),c("","")}catch(r){if(h(l,r)){v(l);return}o(e,s(r)?"forbidden":"error",s(r)?"暂无权限查看帮助文章。":"帮助文章读取失败,请稍后重试。"),c(s(r)?"forbidden":"error",s(r)?"暂无权限查看帮助文章。":r.message||"帮助文章读取失败,请稍后重试。")}finally{e.classList.remove("is-loading")}}e.addEventListener("click",async function(r){var a=r.target.closest("[data-help-detail-id]"),u,f;if(!(!a||a.disabled)&&(u=a.getAttribute("data-help-detail-id"),f=e.querySelector('[data-help-detail-panel="'+u+'"]'),!!f)){a.disabled=!0,o(f,"loading","正在读取完整解答…");try{f.innerHTML=b(await L(l,u))}catch(g){if(h(l,g)){v(l);return}o(f,s(g)?"forbidden":"error",s(g)?"暂无权限查看该帮助文章。":g.message||"帮助文章详情读取失败,请稍后重试。")}}}),n&&n.addEventListener("click",async function(){H(n,!0,"正在刷新…");try{await I()}finally{H(n,!1,"正在刷新…")}}),await I()}return{normalizeHelpArticle:p,normalizeHelpArticleList:y,renderHelpArticleList:w,renderHelpArticleDetail:b,loadHelpArticleDetail:L,loadHelpArticles:C,isForbidden:s,shouldRedirectToLogin:h,init:T}});