Files
jiapu/dist/public/js/person-document-pages.js
T

2 lines
7.3 KiB
JavaScript

(function(r,I){if(typeof module=="object"&&module.exports){module.exports=I(r);return}r.PersonDocumentPages=I(r),r.document&&r.document.addEventListener("DOMContentLoaded",r.PersonDocumentPages.init)})(typeof globalThis!="undefined"?globalThis:window,function(r){"use strict";function I(e){return r.ProfileUI&&typeof r.ProfileUI.confirmAction=="function"?r.ProfileUI.confirmAction(e):Promise.resolve(!r.confirm||r.confirm(e))}function E(e){return r.ProfileUI&&typeof r.ProfileUI.promptAction=="function"?r.ProfileUI.promptAction({title:e,formType:1,maxlength:100}):Promise.resolve(r.prompt?r.prompt(e):null)}var q={FRONT:"正面",BACK:"反面",ATTACHMENT:"附件"};function c(e){return String(e==null?"":e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}function p(e){var t=String(e==null?"":e).trim();return/^[1-9][0-9]*$/.test(t)?t:""}function P(e){var t=e||{},n=p(t.documentId),d=p(t.lineagePersonId),s=String(t.documentTitle||"").trim();return!n||!d||!s||!Array.isArray(t.resources)?null:{documentId:n,lineagePersonId:d,lineagePersonName:String(t.lineagePersonName||"").trim(),documentType:String(t.documentType||"").trim(),documentTypeLabel:String(t.documentTypeLabel||t.documentType||"").trim(),documentTitle:s,maskedIdentifier:String(t.maskedIdentifier||"").trim(),description:String(t.description||"").trim(),contentProtected:t.contentProtected===!0,contentUnlocked:t.contentUnlocked===!0,canEdit:t.canEdit===!0,canDelete:t.canDelete===!0,resources:t.resources.map(function(m){var T=p(m&&m.resourceId),b=String(m&&m.usageType||"").trim();return T&&q[b]?{resourceId:T,usageType:b}:null}).filter(Boolean)}}function N(e){var t=e||{};return{lineagePersonId:p(t.lineagePersonId),documentType:String(t.documentType||"").trim(),documentTitle:String(t.documentTitle||"").trim(),maskedIdentifier:String(t.maskedIdentifier||"").trim(),description:String(t.description||"").trim(),status:"0"}}function j(e){return e.lineagePersonId?e.documentType?e.documentTitle?e.documentTitle.length>100?"证件标题不能超过 100 个字符":e.maskedIdentifier.length>100?"脱敏标识不能超过 100 个字符":e.description.length>1e3?"说明不能超过 1000 个字符":"":"请填写证件标题":"请选择证件类型":"请选择关联人物"}function U(e){var t=Array.isArray(e)?e.map(P).filter(Boolean):[];return t.length?t.map(function(n){var d=n.resources.map(function(s){return'<button class="btn outline" type="button" data-document-resource="'+c(s.resourceId)+'" data-document-id="'+c(n.documentId)+'">'+c(q[s.usageType])+"</button>"}).join("");return'<article class="module-card" data-document-row="'+c(n.documentId)+'"><h3>'+c(n.documentTitle)+"</h3><p>"+c([n.lineagePersonName,n.documentTypeLabel,n.maskedIdentifier,n.description].filter(Boolean).join(" · "))+'</p><div class="row-actions">'+d+(n.contentProtected&&!n.contentUnlocked?'<button class="btn outline" type="button" data-document-unlock="'+n.documentId+'">解锁</button>':"")+(n.canEdit?'<button class="btn outline" type="button" data-document-protect="'+n.documentId+'">'+(n.contentProtected?"取消密码":"设置密码")+"</button>":"")+(n.canDelete?'<button class="btn danger" type="button" data-document-delete="'+n.documentId+'">删除</button>':"")+"</div></article>"}).join(""):'<div class="api-empty">当前家谱还没有重要证件档案</div>'}async function x(){var e=r.document&&r.document.querySelector("[data-person-document-page]"),t=r.GenealogyApi&&r.GenealogyApi.defaultClient,n=r.ProfileUI&&r.ProfileUI.getGenealogyId?r.ProfileUI.getGenealogyId():"",d=e&&e.querySelector("[data-document-form]"),s=e&&e.querySelector("[data-document-list]"),m=e&&e.querySelector("[data-document-status]"),T=e&&e.querySelector("[data-document-person]"),b=e&&e.querySelector("[data-document-type]"),H=e&&e.querySelector("[data-document-oss-id]"),C=e&&e.querySelector("[data-document-usage]"),A=[],M={},w=!1;if(!e)return;if(!t||!t.getToken||!t.getToken()){r.NavigationUtil.open("login.html");return}if(!n){r.ProfileUI.setApiState(m,"error","缺少家谱上下文,请从“我的家谱”重新进入。");return}function u(o,i){o?r.ProfileUI.setApiState(m,o,i):m.textContent=i||""}async function D(){A=(await t.personDocuments(n)).map(P).filter(Boolean),s.innerHTML=U(A)}try{var f=await Promise.all([t.lineagePersonOptions(n),t.businessDictionary("gen_person_document_type"),t.personDocuments(n)]);T.innerHTML='<option value="">请选择人物</option>'+(Array.isArray(f[0])?f[0]:[]).map(function(o){var i=p(o.personId||o.lineagePersonId),l=String(o.displayName||o.personName||o.name||"").trim();return i&&l?'<option value="'+c(i)+'">'+c(l)+"</option>":""}).join(""),b.innerHTML='<option value="">请选择证件类型</option>'+(Array.isArray(f[1])?f[1]:[]).filter(function(o){return o&&o.enabled===!0&&String(o.value||"").trim()}).map(function(o){return'<option value="'+c(o.value)+'">'+c(o.label||o.value)+"</option>"}).join(""),A=(Array.isArray(f[2])?f[2]:[]).map(P).filter(Boolean),s.innerHTML=U(A),u("","重要证件档案已加载")}catch(o){u("error",o.message||"重要证件档案读取失败");return}d.addEventListener("submit",async function(o){var i,l,g,v;if(o.preventDefault(),!w){if(i=N({lineagePersonId:T.value,documentType:b.value,documentTitle:d.elements.documentTitle.value,maskedIdentifier:d.elements.maskedIdentifier.value,description:d.elements.description.value}),l=j(i),l)return u("error",l);if(v=p(H.value),!v)return u("error","请先上传证件文件");w=!0,u("loading","正在保存证件档案…");try{if(g=P(await t.createPersonDocument(n,i)),!g)throw new Error("创建响应缺少有效档案信息");await t.addPersonDocumentResource(n,g.documentId,{ossId:v,usageType:C.value}),d.reset(),H.value="",await D(),u("","证件档案已保存")}catch(a){u("error",a.message||"证件档案保存失败")}finally{w=!1}}}),s.addEventListener("click",async function(o){var i=o.target.closest("[data-document-resource]"),l=o.target.closest("[data-document-unlock]"),g=o.target.closest("[data-document-protect]"),v=o.target.closest("[data-document-delete]"),a,y,h,S;if(!w)try{if(i){S=r.open("about:blank","_blank","noopener"),a=i.getAttribute("data-document-id");var k=await t.personDocumentResourceAccess(n,a,i.getAttribute("data-document-resource"),M[a]);if(!k||!k.accessUrl)throw new Error("文件访问响应缺少地址");S?S.location.replace(k.accessUrl):r.NavigationUtil.open(k.accessUrl);return}if(l){if(a=l.getAttribute("data-document-unlock"),y=await E("请输入档案访问密码"),!y)return;var B=await t.unlockPersonDocument(n,a,y);if(!B||!B.accessToken)throw new Error("解锁响应缺少访问令牌");M[a]=B.accessToken,await D(),u("","档案已临时解锁");return}if(g){if(a=g.getAttribute("data-document-protect"),h=A.find(function(L){return L.documentId===a}),!h)return;if(h.contentProtected){if(!await I("确认取消此档案的密码保护吗?"))return;await t.unprotectPersonDocument(n,a)}else{if(y=await E("设置档案访问密码"),!y)return;await t.protectPersonDocument(n,a,y)}await D(),u("","密码保护设置已更新");return}if(v){if(a=v.getAttribute("data-document-delete"),!await I("确认删除这份证件档案吗?"))return;await t.deletePersonDocument(n,a),await D(),u("","证件档案已删除")}}catch(L){S&&S.close(),u("error",L.message||"操作失败,请稍后重试")}})}return{normalizeDocument:P,buildDocumentBody:N,validateDocumentBody:j,renderDocuments:U,init:x}});