Files
order-notification-automation/web/modules/mail/history.js
T

52 lines
9.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(() => {
'use strict';
async function render(container,{api,esc,formatDate,notify,user,isActive=()=>true,back}){
const data=await api('/mail');if(!isActive())return;
if(user.role!=='admin'){container.innerHTML='<div class="alert alert-info">历史邮件仅限管理员查阅。</div>';return;}
const channels=data.channels;
container.innerHTML=`<section class="card mb-3"><div class="card-header flex-wrap gap-2"><h2 class="card-title">只读查阅已发送邮件</h2>${back?'<button class="btn btn-sm ms-auto" id="history-back">返回邮箱管理</button>':''}<button class="btn btn-primary" id="history-add">添加只读邮箱</button></div><div class="card-body"><p>用于核对历史通知的主题、正文、主送、抄送和附件名称。只读取服务器上的已发送邮件,不更改已读标记,不发信,不导入模板。</p><div class="text-secondary small">邮箱须开通 IMAP。授权码仅在本机表单填写并加密保存;无需 SMTP 配置。若历史邮件仅保存在 Foxmail 本地,IMAP 无法读取。</div></div></section><div id="history-editor"></div><form class="card" id="history-search"><div class="card-body"><label class="form-label" for="history-channel">查询邮箱</label><div class="d-flex flex-wrap gap-2 mb-3"><select class="form-select" style="max-width:100%" id="history-channel"><option value="">请选择邮箱</option>${channels.map(c=>`<option value="${esc(c.id)}">${esc(c.name)} · ${esc(c.email)}</option>`).join('')}</select><button type="button" class="btn" id="history-folders">连接并列出已发送文件夹</button><button type="button" class="btn" id="history-edit">修改只读邮箱配置</button></div><label class="form-label" for="history-folder">已发送文件夹</label><select class="form-select mb-3" id="history-folder" required><option value="">请先连接邮箱</option></select><div class="row g-3"><div class="col-md-4"><label class="form-label" for="history-since">开始日期(包含)</label><input class="form-control" type="date" id="history-since" required></div><div class="col-md-4"><label class="form-label" for="history-before">结束日期(不包含)</label><input class="form-control" type="date" id="history-before" required></div><div class="col-md-4"><label class="form-label" for="history-subject">主题关键词</label><input class="form-control" id="history-subject" maxlength="100" value="订单"></div></div><div class="form-hint mt-2">每次最多31天,显示最近20封匹配邮件;结果多时缩小日期或关键词。</div><div id="history-error" class="text-danger mt-3" role="alert"></div></div><div class="card-footer"><button class="btn btn-primary" type="submit">查询已发送邮件</button></div></form><div id="history-results" class="mt-3"></div><div id="history-detail" class="mt-3"></div>`;
const find=id=>container.querySelector('#'+id),read=id=>find(id).value;
const day=d=>new Intl.DateTimeFormat('sv-SE',{timeZone:'Asia/Shanghai'}).format(d);
find('history-since').value=day(new Date(Date.now()-14*86400000));find('history-before').value=day(new Date(Date.now()+86400000));
let epoch='',resultChannel='',resultFolder='';
async function act(button,work){button.disabled=true;find('history-error').textContent='';try{await work();}catch(e){if(isActive())find('history-error').textContent=e.message;}finally{if(button.isConnected)button.disabled=false;}}
const query=(id,body)=>api(`/mail/channels/${encodeURIComponent(id)}/history`,body);
find('history-back')?.addEventListener('click',back);
find('history-add').onclick=()=>editor();
find('history-edit').onclick=()=>{const c=channels.find(c=>c.id===read('history-channel'));if(!c?.readOnly){notify('请选择已保存的只读邮箱;发信邮箱请在原邮箱管理页修改。','warning');return;}editor(c);};
find('history-channel').onchange=()=>{find('history-folder').innerHTML='<option value="">请先连接邮箱</option>';find('history-results').replaceChildren();find('history-detail').replaceChildren();epoch='';};
if(channels.length===1)find('history-channel').value=channels[0].id;
find('history-folders').onclick=e=>act(e.target,async()=>{
const id=read('history-channel');if(!id)throw Error('请先选择邮箱。');
const result=await query(id,{action:'folders'});if(!isActive()||read('history-channel')!==id)return;
find('history-folder').innerHTML=result.folders.map(f=>`<option value="${esc(f.path)}">${esc(f.name)}</option>`).join('')||'<option value="">未识别到已发送文件夹</option>';
if(!result.folders.length)throw Error('服务器未提供可识别的已发送文件夹,请确认已发送邮件已同步到服务器。');
});
find('history-search').onsubmit=e=>{e.preventDefault();act(e.submitter,async()=>{
const id=read('history-channel'),folder=read('history-folder');if(!id||!folder)throw Error('请先连接邮箱并选择已发送文件夹。');
const result=await query(id,{action:'search',folder,since:read('history-since'),before:read('history-before'),subject:read('history-subject'),limit:20});
if(!isActive()||read('history-channel')!==id||read('history-folder')!==folder)return;
epoch=result.uidValidity;resultChannel=id;resultFolder=folder;find('history-detail').replaceChildren();
find('history-results').innerHTML=`<section class="card"><div class="card-header"><h3 class="card-title">历史邮件(${result.messages.length})</h3></div>${result.truncated?'<div class="card-body text-warning">仅显示最近20封,请缩小查询范围。</div>':''}<div class="table-responsive"><table class="table card-table"><thead><tr><th>日期 / 主题</th><th></th></tr></thead><tbody>${result.messages.map(m=>`<tr><td class="text-break">${esc(m.subject)}<div class="small text-secondary">${formatDate(m.date)}</div></td><td><button class="btn btn-sm" data-history-uid="${m.uid}">查看邮件</button></td></tr>`).join('')||'<tr><td colspan="2" class="text-secondary">没有匹配的服务器邮件;这不代表本地客户端没有历史邮件。</td></tr>'}</tbody></table></div></section>`;
container.querySelectorAll('[data-history-uid]').forEach(b=>b.onclick=()=>act(b,async()=>{
const savedEpoch=epoch,savedChannel=resultChannel,savedFolder=resultFolder;
const detail=await query(savedChannel,{action:'message',folder:savedFolder,uid:Number(b.dataset.historyUid),uidValidity:savedEpoch});
if(!isActive()||savedEpoch!==epoch||savedChannel!==read('history-channel')||savedFolder!==read('history-folder'))return;
const address=values=>values.map(a=>a.name?`${a.name} <${a.address}>`:a.address).join(';');
find('history-detail').innerHTML=`<section class="card"><div class="card-header"><h3 class="card-title text-break">${esc(detail.subject)}</h3></div><div class="card-body"><div class="small text-break mb-3">日期:${formatDate(detail.date)}<br>主送:${esc(address(detail.to))}<br>抄送:${esc(address(detail.cc))}<br>来源:${esc(detail.folder)} · UID ${detail.uid} · ${esc(detail.messageId)}</div><pre style="white-space:pre-wrap;overflow-wrap:anywhere" id="history-body"></pre>${detail.truncated?'<div class="text-warning">正文过长,仅显示前100000字符。</div>':''}<h4>附件名称</h4><ul>${detail.attachments.map(a=>`<li class="text-break">${esc(a.filename)}(${Math.ceil(a.size/1024)} KB)</li>`).join('')||'<li class="text-secondary">无附件</li>'}</ul></div></section>`;
find('history-body').textContent=detail.text;
}));
});};
function editor(c){
const host=find('history-editor'),v=c?.imap||{};
host.innerHTML=`<form class="card mb-3" id="history-account"><div class="card-header"><h3 class="card-title">${c?'修改':'添加'}只读邮箱</h3></div><div class="card-body"><div class="row g-3">${[['history-name','邮箱名称',c?.name||'','text'],['history-email','邮箱地址',c?.email||'','email'],['history-host','IMAP 服务器',v.host||'','text'],['history-user','IMAP 登录账号',v.user||'','text'],['history-port','IMAP 端口',v.port||993,'number']].map(([id,label,val,type])=>`<div class="col-md-6"><label class="form-label" for="${id}">${label}</label><input class="form-control" id="${id}" type="${type}" value="${esc(val)}" required></div>`).join('')}<div class="col-md-6"><label class="form-label" for="history-security">IMAP 加密方式</label><select class="form-select" id="history-security"><option value="tls">SSL / TLS</option><option value="starttls" ${v.security==='starttls'?'selected':''}>STARTTLS</option></select></div><div class="col-12"><label class="form-label" for="history-password">IMAP 授权码</label><input class="form-control" id="history-password" type="password" maxlength="2048" autocomplete="new-password" ${c?'':'required'} placeholder="${c?'留空保留原授权码':'仅在本机填写'}"></div></div><div id="history-save-error" class="text-danger mt-3" role="alert"></div></div><div class="card-footer btn-list"><button class="btn btn-primary" type="submit">保存只读邮箱</button><button class="btn" type="button" id="history-cancel">取消</button></div></form>`;
find('history-cancel').onclick=()=>host.replaceChildren();
find('history-account').onsubmit=async e=>{e.preventDefault();const button=e.submitter;button.disabled=true;try{
const saved=await api('/mail/channels',{...(c?{id:c.id,version:c.version}:{}),readOnly:true,name:read('history-name'),email:read('history-email'),imap:{host:read('history-host'),user:read('history-user'),port:Number(read('history-port')),security:read('history-security'),password:read('history-password')}});
if(!isActive())return;c=saved;find('history-password').value='';find('history-password').required=false;await window.PageLifecycle.afterSave(()=>render(container,{api,esc,formatDate,notify,user,isActive,back}),'只读邮箱已保存。',notify,isActive);
}catch(error){if(isActive())find('history-save-error').textContent=error.message;}finally{if(button.isConnected)button.disabled=false;}};
}
}
window.MailHistoryViews={render};
})();