Commit 913ec8e6 by liimin

下载中心优化

1 parent 17e654c7
Pipeline #576 for 913ec8e6 failed in 0 seconds
...@@ -58,15 +58,19 @@ export function TaskPreviewModal() { ...@@ -58,15 +58,19 @@ export function TaskPreviewModal() {
const [downloadReq, setDownloadReq] = useState<{ taskId: string; seq: number }>({ taskId: '', seq: 0 }); const [downloadReq, setDownloadReq] = useState<{ taskId: string; seq: number }>({ taskId: '', seq: 0 });
if (!previewTaskId || !task) return null; if (!previewTaskId || !task) return null;
const url = getTaskPreviewUrl(previewTaskId); // previewUrl 仅用于渲染:有字节是 blob:,无字节是直链。
if (!url) return null; // linkUrl 是真实文件地址(出口已把明文 http 升级为 https),用于展示和复制——
// 复制 blob: 粘到浏览器打不开,必须区分开。
const previewUrl = getTaskPreviewUrl(previewTaskId);
if (!previewUrl) return null;
const linkUrl = task.fileUrl;
const kind = previewKind(task); const kind = previewKind(task);
const hasBytes = hasTaskFile(previewTaskId); const hasBytes = hasTaskFile(previewTaskId);
const downloadActive = downloadReq.taskId === previewTaskId && downloadReq.seq > 0; const downloadActive = downloadReq.taskId === previewTaskId && downloadReq.seq > 0;
const handleCopy = () => { const handleCopy = () => {
copyText(url); copyText(linkUrl ?? previewUrl);
setCopied(true); setCopied(true);
window.setTimeout(() => setCopied(false), 1600); window.setTimeout(() => setCopied(false), 1600);
}; };
...@@ -94,10 +98,8 @@ export function TaskPreviewModal() { ...@@ -94,10 +98,8 @@ export function TaskPreviewModal() {
: '请在预览画面上右键另存为'} : '请在预览画面上右键另存为'}
</span> </span>
<span style={{ display: 'flex', gap: 8, flexShrink: 0 }}> <span style={{ display: 'flex', gap: 8, flexShrink: 0 }}>
{/* 有字节的 other 复制的是 blob: 地址,粘到浏览器打不开,隐藏掉避免误导。 */} {/* 复制真实文件地址(出口已升级 https),不是 blob: 预览地址。 */}
{!(hasBytes && kind === 'other') && ( {linkUrl && <Button onClick={handleCopy}>{copied ? '已复制' : '复制地址'}</Button>}
<Button onClick={handleCopy}>{copied ? '已复制' : '复制链接'}</Button>
)}
{/* 只有确实拿到字节才显示:没有字节时 a[download] 也救不了跨域直链。 */} {/* 只有确实拿到字节才显示:没有字节时 a[download] 也救不了跨域直链。 */}
{hasBytes && ( {hasBytes && (
<Button type="primary" onClick={() => saveTaskAs(task.id)}> <Button type="primary" onClick={() => saveTaskAs(task.id)}>
...@@ -112,18 +114,18 @@ export function TaskPreviewModal() { ...@@ -112,18 +114,18 @@ export function TaskPreviewModal() {
{kind === 'image' && ( {kind === 'image' && (
<div style={{ textAlign: 'center', background: '#fafafa', padding: 12 }}> <div style={{ textAlign: 'center', background: '#fafafa', padding: 12 }}>
<img <img
src={url} src={previewUrl}
alt={task.label} alt={task.label}
style={{ maxWidth: '100%', maxHeight: '65vh', objectFit: 'contain' }} style={{ maxWidth: '100%', maxHeight: '65vh', objectFit: 'contain' }}
/> />
</div> </div>
)} )}
{kind === 'pdf' && ( {kind === 'pdf' && (
<iframe src={url} title={task.label} style={{ width: '100%', height: '65vh', border: 0 }} /> <iframe src={previewUrl} title={task.label} style={{ width: '100%', height: '65vh', border: 0 }} />
)} )}
{kind === 'video' && ( {kind === 'video' && (
<div style={{ textAlign: 'center', background: '#fafafa', padding: 12 }}> <div style={{ textAlign: 'center', background: '#fafafa', padding: 12 }}>
<video src={url} controls style={{ maxWidth: '100%', maxHeight: '65vh' }} /> <video src={previewUrl} controls style={{ maxWidth: '100%', maxHeight: '65vh' }} />
</div> </div>
)} )}
{kind === 'other' && !hasBytes && ( {kind === 'other' && !hasBytes && (
...@@ -137,7 +139,7 @@ export function TaskPreviewModal() { ...@@ -137,7 +139,7 @@ export function TaskPreviewModal() {
{downloadActive && ( {downloadActive && (
<iframe <iframe
key={downloadReq.seq} key={downloadReq.seq}
src={url} src={previewUrl}
title={task.label} title={task.label}
aria-hidden="true" aria-hidden="true"
style={{ position: 'fixed', width: 0, height: 0, border: 0, left: -9999, top: -9999 }} style={{ position: 'fixed', width: 0, height: 0, border: 0, left: -9999, top: -9999 }}
...@@ -152,35 +154,88 @@ export function TaskPreviewModal() { ...@@ -152,35 +154,88 @@ export function TaskPreviewModal() {
{downloadActive ? '重新下载' : '下载文件'} {downloadActive ? '重新下载' : '下载文件'}
</Button> </Button>
</div> </div>
<div {linkUrl && (
role="button" <div
tabIndex={0} role="button"
title="点击复制地址" tabIndex={0}
onClick={handleCopy} title="点击复制地址"
onKeyDown={(e) => { onClick={handleCopy}
if (e.key === 'Enter' || e.key === ' ') handleCopy(); onKeyDown={(e) => {
}} if (e.key === 'Enter' || e.key === ' ') handleCopy();
style={{ }}
marginTop: 8, style={{
padding: 10, marginTop: 8,
background: '#fafafa', padding: 10,
border: '1px solid #f0f0f0', background: '#fafafa',
borderRadius: 4, border: '1px solid #f0f0f0',
fontFamily: 'monospace', borderRadius: 4,
fontSize: 12, fontFamily: 'monospace',
wordBreak: 'break-all', fontSize: 12,
cursor: 'pointer', wordBreak: 'break-all',
color: '#1677ff', cursor: 'pointer',
}} color: '#1677ff',
> }}
{url} >
</div> {linkUrl}
</div>
)}
</div> </div>
)} )}
{kind === 'other' && hasBytes && ( {kind === 'other' && hasBytes && (
<div style={{ padding: 12, color: '#8c8c8c', lineHeight: 1.7 }}> <div style={{ padding: 12 }}>
此格式无法在页面内预览,但文件已完整缓存到本地。点右下角「保存到本地」即可保存, <div style={{ color: '#8c8c8c', lineHeight: 1.7, marginBottom: 8 }}>
保存位置由浏览器的下载设置决定。 此格式无法在页面内预览,但文件已完整缓存到本地。点右下角「保存到本地」即可保存,
保存位置由浏览器的下载设置决定。
</div>
{linkUrl && (
<div
role="button"
tabIndex={0}
title="点击复制地址"
onClick={handleCopy}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') handleCopy();
}}
style={{
padding: 10,
background: '#fafafa',
border: '1px solid #f0f0f0',
borderRadius: 4,
fontFamily: 'monospace',
fontSize: 12,
wordBreak: 'break-all',
cursor: 'pointer',
color: '#1677ff',
}}
>
{linkUrl}
</div>
)}
</div>
)}
{kind !== 'other' && linkUrl && (
<div
role="button"
tabIndex={0}
title="点击复制地址"
onClick={handleCopy}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') handleCopy();
}}
style={{
margin: '8px 12px 0',
padding: 10,
background: '#fafafa',
border: '1px solid #f0f0f0',
borderRadius: 4,
fontFamily: 'monospace',
fontSize: 12,
wordBreak: 'break-all',
cursor: 'pointer',
color: '#1677ff',
}}
>
{linkUrl}
</div> </div>
)} )}
</Modal> </Modal>
......
...@@ -348,11 +348,16 @@ export function invalidDownload(payload: unknown): Error { ...@@ -348,11 +348,16 @@ export function invalidDownload(payload: unknown): Error {
* - error:抛错,由引擎落 failed 并弹失败提示。 * - error:抛错,由引擎落 failed 并弹失败提示。
*/ */
export async function deliverRemoteFile( export async function deliverRemoteFile(
url: string, rawUrl: string,
fallbackName: string, fallbackName: string,
signal: AbortSignal | undefined, signal: AbortSignal | undefined,
opts: { preview?: boolean; autoDownload?: boolean; openInNewTab?: boolean } = {}, opts: { preview?: boolean; autoDownload?: boolean; openInNewTab?: boolean } = {},
): Promise<RemoteDelivery> { ): Promise<RemoteDelivery> {
// HTTPS 页面里的明文 http 直链必被浏览器 Mixed Content 拦死。必须在 probe 之前
// 升级:assertDownloadableUrl 见到明文 http 会直接判失败,异步导出拿到的后端
// fileUrl(dj.dev.glsx.com.cn 等)过去就卡在这里,没有兜底。
// 升级是幂等的(https / 相对 / blob 原样返回),urlHandler 里那次升级可保留。
const url = upgradeInsecureUrl(rawUrl);
const probe = await probeDownloadUrl(url, fallbackName, signal); const probe = await probeDownloadUrl(url, fallbackName, signal);
// 探测期间可能被取消(abort 会让 probe 返回 opaque 而不是抛错)。 // 探测期间可能被取消(abort 会让 probe 返回 opaque 而不是抛错)。
// 必须在任何落地动作(saveBlob / 开标签)之前停下,否则取消后还弹出标签页。 // 必须在任何落地动作(saveBlob / 开标签)之前停下,否则取消后还弹出标签页。
...@@ -363,6 +368,7 @@ export async function deliverRemoteFile( ...@@ -363,6 +368,7 @@ export async function deliverRemoteFile(
if (opts.preview) { if (opts.preview) {
// 只回传字节,由主壳 Modal 渲染;不自动落盘(点缩略图是要看原图,不是下载)。 // 只回传字节,由主壳 Modal 渲染;不自动落盘(点缩略图是要看原图,不是下载)。
return { return {
url,
fileName: probe.fileName, fileName: probe.fileName,
bytes: probe.bytes, bytes: probe.bytes,
blob: probe.blob, blob: probe.blob,
...@@ -372,6 +378,7 @@ export async function deliverRemoteFile( ...@@ -372,6 +378,7 @@ export async function deliverRemoteFile(
} }
if (opts.autoDownload !== false) saveBlob(probe.blob, probe.fileName); if (opts.autoDownload !== false) saveBlob(probe.blob, probe.fileName);
return { return {
url,
fileName: probe.fileName, fileName: probe.fileName,
bytes: probe.bytes, bytes: probe.bytes,
blob: probe.blob, blob: probe.blob,
...@@ -384,6 +391,7 @@ export async function deliverRemoteFile( ...@@ -384,6 +391,7 @@ export async function deliverRemoteFile(
// 不再开可见标签打断用户:预览统一由下载中心的 Modal 渲染,用户在画面上右键另存。 // 不再开可见标签打断用户:预览统一由下载中心的 Modal 渲染,用户在画面上右键另存。
assertDownloadableUrl(url); assertDownloadableUrl(url);
return { return {
url,
fileName: fallbackName, fileName: fallbackName,
note: opts.preview note: opts.preview
? '已获取预览地址,请在预览画面上右键另存为' ? '已获取预览地址,请在预览画面上右键另存为'
......
...@@ -310,14 +310,15 @@ export const asyncExportHandler: DownloadHandler = { ...@@ -310,14 +310,15 @@ export const asyncExportHandler: DownloadHandler = {
} }
const fileName = task.input.fileName ?? task.label; const fileName = task.input.fileName ?? task.label;
// 与 url 直链共用落地出口:拿到 fileUrl 后先探测再保存,inline 的 PDF / 图片 // 与 url 直链共用落地出口:拿到 fileUrl 后先探测再保存,inline 的 PDF / 图片
// 不再只在 0×0 隐藏 iframe 里渲染;拿不到字节(跨域无 CORS)时开可见标签, // 不再只在 0×0 隐藏 iframe 里渲染;明文 http 在出口统一升级为 https(否则
// 并把「未自动保存,请右键另存为」写进 stage,不再误报「完成」。 // HTTPS 主壳下直接被 Mixed Content 拦死);拿不到字节(跨域无 CORS)时走
// opaque 兜底,并把「未自动保存」写进 stage,不再误报「完成」。
const delivery = await ctx.deliver.deliverRemoteFile(fileUrl, fileName, ctx.signal, { const delivery = await ctx.deliver.deliverRemoteFile(fileUrl, fileName, ctx.signal, {
autoDownload: task.input.autoDownload, autoDownload: task.input.autoDownload,
}); });
if (ctx.isCanceled()) throw new DOMException('已取消', 'AbortError'); if (ctx.isCanceled()) throw new DOMException('已取消', 'AbortError');
return { return {
fileUrl, fileUrl: delivery.url,
fileName: delivery.fileName, fileName: delivery.fileName,
bytes: delivery.bytes, bytes: delivery.bytes,
blob: delivery.blob, blob: delivery.blob,
......
...@@ -27,15 +27,14 @@ export const urlHandler: DownloadHandler = { ...@@ -27,15 +27,14 @@ export const urlHandler: DownloadHandler = {
async execute(task, ctx): Promise<DownloadResult> { async execute(task, ctx): Promise<DownloadResult> {
const payload = task.input.payload as UrlPayload; const payload = task.input.payload as UrlPayload;
if (!payload?.url) throw new Error('URL 下载缺少 url'); if (!payload?.url) throw new Error('URL 下载缺少 url');
// 后端历史数据里还有明文 http 直链(典型是付款回单 // 明文 http 直链(付款回单 http://qcimg01.glsx.com.cn:7060/...)在 HTTPS 主壳下
// http://qcimg01.glsx.com.cn:7060/group1/...)。主壳是 HTTPS,原样交给浏览器 // 必被 Mixed Content 拦死。这里先升级一次用于从路径推文件名;真正的协议兜底在
// 必被 Mixed Content 拦死,过去只能整条链路报失败。入口统一升级协议并去掉 // deliverRemoteFile 出口统一做(异步导出也走那里),入口这步是幂等的。
// 原 HTTP 业务端口,业务页(finance 列表页 / 详情抽屉)不用各自处理。
const url = upgradeInsecureUrl(payload.url); const url = upgradeInsecureUrl(payload.url);
const fileName = task.input.fileName ?? fileNameFromUrl(url); const fileName = task.input.fileName ?? fileNameFromUrl(url);
// 探测 + 落地统一走 deliverRemoteFile:ok 用响应体真落盘并回传字节; // 探测 + 落地统一走 deliverRemoteFile:ok 用响应体真落盘并回传字节;
// opaque 开可见新标签(不再用 0×0 隐藏 iframe 静默吞掉 inline 图片); // opaque 走弹窗预览(不再用 0×0 隐藏 iframe 静默吞掉 inline 图片);
// error 抛错由引擎落 failed。取消与落地顺序也由它统一保证。 // error 抛错由引擎落 failed。取消与落地顺序也由它统一保证。
const delivery = await ctx.deliver.deliverRemoteFile(url, fileName, ctx.signal, { const delivery = await ctx.deliver.deliverRemoteFile(url, fileName, ctx.signal, {
preview: payload.preview, preview: payload.preview,
...@@ -44,7 +43,7 @@ export const urlHandler: DownloadHandler = { ...@@ -44,7 +43,7 @@ export const urlHandler: DownloadHandler = {
}); });
if (ctx.isCanceled()) throw new DOMException('已取消', 'AbortError'); if (ctx.isCanceled()) throw new DOMException('已取消', 'AbortError');
return { return {
fileUrl: url, fileUrl: delivery.url,
fileName: delivery.fileName, fileName: delivery.fileName,
bytes: delivery.bytes, bytes: delivery.bytes,
blob: delivery.blob, blob: delivery.blob,
......
...@@ -48,6 +48,8 @@ export interface DeliverTools { ...@@ -48,6 +48,8 @@ export interface DeliverTools {
/** deliverRemoteFile 的返回,单独导出供类型引用。 */ /** deliverRemoteFile 的返回,单独导出供类型引用。 */
export interface RemoteDelivery { export interface RemoteDelivery {
/** 实际访问的下载地址(明文 http 已升级为 https,回写给任务供弹窗展示 / 复制)。 */
url: string;
fileName: string; fileName: string;
bytes?: number; bytes?: number;
blob?: Blob; blob?: Blob;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!