Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ujia
/
uj-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit cd97e695
authored
Nov 17, 2026
by
liimin
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
处理链接协议不一致的下载问题
1 parent
5c97a6c9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
11 deletions
docs/PORT_CONVERSION.md
packages/download-center/src/deliver.ts
packages/download-center/src/handlers/simple.ts
docs/PORT_CONVERSION.md
View file @
cd97e69
...
@@ -408,6 +408,8 @@ downloadCenter.register({ ... }); // 显式 kind / 自定义 kind 的统一登
...
@@ -408,6 +408,8 @@ downloadCenter.register({ ... }); // 显式 kind / 自定义 kind 的统一登
公共字段(
`CommonDownloadInput`
):
`source`
、
`label`
、
`fileName`
、
`autoDownload`
(默认 true;false 只进面板供点击)、
`silent`
(默认 false)、
`dedupeKey`
(防连点去重)、
`channel`
(多网关,见 14.6)。
公共字段(
`CommonDownloadInput`
):
`source`
、
`label`
、
`fileName`
、
`autoDownload`
(默认 true;false 只进面板供点击)、
`silent`
(默认 false)、
`dedupeKey`
(防连点去重)、
`channel`
(多网关,见 14.6)。
`async-export`
另可覆盖:
`submitMethod`
/
`queryMethod`
/
`pollInterval`
(默认 3000ms)/
`timeoutMs`
(默认 10 分钟)。
`async-export`
另可覆盖:
`submitMethod`
/
`queryMethod`
/
`pollInterval`
(默认 3000ms)/
`timeoutMs`
(默认 10 分钟)。
**`url` kind 会在执行前归一明文地址**
:HTTPS 主壳 +
`http://`
直链时,
`packages/download-center/src/deliver.ts`
的
`upgradeInsecureUrl()`
把协议换成 https
**并丢掉原 HTTP 业务端口**
(回单
`http://qcimg01.glsx.com.cn:7060/...`
→
`https://qcimg01.glsx.com.cn/...`
,443 上返回同一份文件)。业务页只管把后端给的地址原样交给
`downloadCenter.url()`
,
**不要自己改协议**
。
### 14.5 后端零改动:对齐源系统的异步导出协议
### 14.5 后端零改动:对齐源系统的异步导出协议
源系统(
`D:/work/saas+/dj-car-life-system`
)的公共编排接口,
**完整 method 带 `dj.api.` 前缀**
(
`src/api/sys.base.js`
的
`path = "dj.api."`
,别漏前缀);提交 / 轮询 / 通知 / 自动下载的实现见
`src/layout/header-aside/components/header-user/index.vue`
:
源系统(
`D:/work/saas+/dj-car-life-system`
)的公共编排接口,
**完整 method 带 `dj.api.` 前缀**
(
`src/api/sys.base.js`
的
`path = "dj.api."`
,别漏前缀);提交 / 轮询 / 通知 / 自动下载的实现见
`src/layout/header-aside/components/header-user/index.vue`
:
...
@@ -441,6 +443,7 @@ downloadCenter.register({ ... }); // 显式 kind / 自定义 kind 的统一登
...
@@ -441,6 +443,7 @@ downloadCenter.register({ ... }); // 显式 kind / 自定义 kind 的统一登
-
`<a href={fileUrl} target="_blank">`
/
`<UButton href={fileUrl}>`
**触发文件下载**
→ 同样禁止,改用
`downloadCenter.url()`
;纯外链预览 / H5 跳转不算下载(静态门禁只拦明确的文件落地写法,href 靠语义判定 + 代码评审)。
-
`<a href={fileUrl} target="_blank">`
/
`<UButton href={fileUrl}>`
**触发文件下载**
→ 同样禁止,改用
`downloadCenter.url()`
;纯外链预览 / H5 跳转不算下载(静态门禁只拦明确的文件落地写法,href 靠语义判定 + 代码评审)。
-
自己 POST 导出接口后只
`message.success('导出任务已提交')`
→ 漏了轮询 / 浮窗提示 / 历史,改用
`asyncExport()`
。
-
自己 POST 导出接口后只
`message.success('导出任务已提交')`
→ 漏了轮询 / 浮窗提示 / 历史,改用
`asyncExport()`
。
-
自己写
`content-disposition`
解析 / C0 控制字符清洗 / Blob 落地 → 已在
`deliver.ts`
统一实现,别抄第二份。
-
自己写
`content-disposition`
解析 / C0 控制字符清洗 / Blob 落地 → 已在
`deliver.ts`
统一实现,别抄第二份。
-
业务页自己
`url.replace(/^http:/, 'https:')`
升级协议 →
`url`
kind 已在
`upgradeInsecureUrl()`
里做,且
**必须连端口一起丢**
:
`http://qcimg01.glsx.com.cn:7060/x`
只换协议会得到
`https://qcimg01.glsx.com.cn:7060/x`
,该端口只跑裸 HTTP(TLS 握手
`ERR_SSL_PACKET_LENGTH_TOO_LONG`
),等于把「被浏览器 Mixed Content 拦」换成「网络错误」。
`apps/mp/usedcar/subvehicleConversion/utils/imageUrl.js`
的
`ensureHttpsUrl`
正是只换协议保留端口的写法,别照抄到 web 下载链路。
-
给下载接口拼 URL、手写 sign、直接
`axios`
/
`fetch`
拿流 → 违反 §1.1 与 §4。
-
给下载接口拼 URL、手写 sign、直接
`axios`
/
`fetch`
拿流 → 违反 §1.1 与 §4。
-
把
`@uj/download-center`
运行时 import 进子应用 → 子应用只依赖
`@uj/platform-sdk`
(登记)+
`@uj/contracts`
(类型)。
-
把
`@uj/download-center`
运行时 import 进子应用 → 子应用只依赖
`@uj/platform-sdk`
(登记)+
`@uj/contracts`
(类型)。
...
...
packages/download-center/src/deliver.ts
View file @
cd97e69
...
@@ -46,6 +46,51 @@ export function saveBlob(blob: Blob, fileName: string): void {
...
@@ -46,6 +46,51 @@ export function saveBlob(blob: Blob, fileName: string): void {
}
}
}
}
/** 本地回环地址没有 TLS 服务,浏览器本身也不拦,改写只会把它改坏。 */
const
LOCAL_HOSTNAMES
=
new
Set
([
'localhost'
,
'127.0.0.1'
,
'::1'
]);
/**
* 把 HTTPS 页面里的明文 HTTP 直链升级成 HTTPS,并去掉原 HTTP 业务端口。
*
* 为什么需要:后端历史数据里的文件直链仍是 http(典型是回单
* `http://qcimg01.glsx.com.cn:7060/group1/...`)。主壳是 HTTPS,这类地址交给浏览器
* 必然被 Mixed Content 拦死,assertDownloadableUrl 也会直接判任务失败。
*
* 规则:
* - 非 `http://` 开头(https / 相对路径 / data: / blob:)原样返回,不做任何加工;
* - 页面不是 https(本地 HTTP 开发、小程序 webview 白名单调试)时原样返回,
* 这类环境本来就能下明文地址,没必要改;
* - http 且页面 https:协议换 https,**并丢掉端口**。
*
* 为什么必须丢端口:同源站点的 HTTPS 监听在 443,而不在原来的 HTTP 业务端口上。
* 实测 `qcimg01.glsx.com.cn`:`:7060` 只跑裸 HTTP(TLS 握手直接
* `ERR_SSL_PACKET_LENGTH_TOO_LONG`,服务端把 ClientHello 当 HTTP 请求解析返回 400),
* 而 `https://qcimg01.glsx.com.cn/<同路径>` 返回的是同一份文件(200、字节数一致)。
* 只换协议不丢端口会得到 `https://host:7060/...`——照样连不上,只是把「被浏览器拦」
* 换成「网络错误」,问题从明处藏到暗处。
*
* 代价边界:改写只作用于「在 HTTPS 页面里本来必然失败」的地址,最坏结果是从一种
* 失败变成另一种失败,不会让原本可用的下载变坏。
*/
export
function
upgradeInsecureUrl
(
url
:
string
):
string
{
const
raw
=
String
(
url
??
''
).
trim
();
if
(
!
/^http:
\/\/
/i
.
test
(
raw
))
return
raw
;
if
(
typeof
window
===
'undefined'
||
window
.
location
.
protocol
!==
'https:'
)
return
raw
;
let
parsed
:
URL
;
try
{
parsed
=
new
URL
(
raw
);
}
catch
{
// 解析不了就交给后面的 assertDownloadableUrl 报「地址无效」,这里不抢判断。
return
raw
;
}
if
(
LOCAL_HOSTNAMES
.
has
(
parsed
.
hostname
))
return
raw
;
parsed
.
protocol
=
'https:'
;
parsed
.
port
=
''
;
return
parsed
.
toString
();
}
/**
/**
* 下载地址前置校验:在把 URL 交给 iframe / 锚点之前,先拦截「发起前即可判定」的错误。
* 下载地址前置校验:在把 URL 交给 iframe / 锚点之前,先拦截「发起前即可判定」的错误。
*
*
...
@@ -68,7 +113,7 @@ export function assertDownloadableUrl(url: string): void {
...
@@ -68,7 +113,7 @@ export function assertDownloadableUrl(url: string): void {
if
(
if
(
window
.
location
.
protocol
===
'https:'
&&
window
.
location
.
protocol
===
'https:'
&&
parsed
.
protocol
===
'http:'
&&
parsed
.
protocol
===
'http:'
&&
!
[
'localhost'
,
'127.0.0.1'
,
'::1'
].
include
s
(
parsed
.
hostname
)
!
LOCAL_HOSTNAMES
.
ha
s
(
parsed
.
hostname
)
)
{
)
{
throw
new
Error
(
throw
new
Error
(
`下载地址被浏览器拦截:当前页面为 HTTPS,但文件地址是明文 HTTP(
${
parsed
.
host
}
)。`
+
`下载地址被浏览器拦截:当前页面为 HTTPS,但文件地址是明文 HTTP(
${
parsed
.
host
}
)。`
+
...
@@ -188,7 +233,9 @@ export async function probeDownloadUrl(
...
@@ -188,7 +233,9 @@ export async function probeDownloadUrl(
* 同源、需要签名头才能取到的文件必须走 stream kind,经主壳 request 拿流。
* 同源、需要签名头才能取到的文件必须走 stream kind,经主壳 request 拿流。
*/
*/
export
function
openUrl
(
url
:
string
,
openInNewTab
=
true
):
void
{
export
function
openUrl
(
url
:
string
,
openInNewTab
=
true
):
void
{
assertDownloadableUrl
(
url
);
// 兜住所有直接调 openUrl 的入口(不限 urlHandler):明文地址先升级再校验。
const
target
=
upgradeInsecureUrl
(
url
);
assertDownloadableUrl
(
target
);
if
(
openInNewTab
)
{
if
(
openInNewTab
)
{
const
iframe
=
document
.
createElement
(
'iframe'
);
const
iframe
=
document
.
createElement
(
'iframe'
);
iframe
.
style
.
position
=
'fixed'
;
iframe
.
style
.
position
=
'fixed'
;
...
@@ -198,14 +245,14 @@ export function openUrl(url: string, openInNewTab = true): void {
...
@@ -198,14 +245,14 @@ export function openUrl(url: string, openInNewTab = true): void {
iframe
.
style
.
left
=
'-9999px'
;
iframe
.
style
.
left
=
'-9999px'
;
iframe
.
style
.
top
=
'-9999px'
;
iframe
.
style
.
top
=
'-9999px'
;
iframe
.
setAttribute
(
'aria-hidden'
,
'true'
);
iframe
.
setAttribute
(
'aria-hidden'
,
'true'
);
iframe
.
src
=
url
;
iframe
.
src
=
target
;
document
.
body
.
appendChild
(
iframe
);
document
.
body
.
appendChild
(
iframe
);
// 下载由浏览器接管后移除 iframe,避免长期驻留;时长足以覆盖大文件落盘启动。
// 下载由浏览器接管后移除 iframe,避免长期驻留;时长足以覆盖大文件落盘启动。
setTimeout
(()
=>
iframe
.
remove
(),
60
_000
);
setTimeout
(()
=>
iframe
.
remove
(),
60
_000
);
return
;
return
;
}
}
const
link
=
document
.
createElement
(
'a'
);
const
link
=
document
.
createElement
(
'a'
);
link
.
href
=
url
;
link
.
href
=
target
;
link
.
rel
=
'noreferrer'
;
link
.
rel
=
'noreferrer'
;
document
.
body
.
appendChild
(
link
);
document
.
body
.
appendChild
(
link
);
link
.
click
();
link
.
click
();
...
...
packages/download-center/src/handlers/simple.ts
View file @
cd97e69
...
@@ -5,7 +5,7 @@ import type {
...
@@ -5,7 +5,7 @@ import type {
DownloadResult
,
DownloadResult
,
UrlPayload
,
UrlPayload
,
}
from
'../types'
;
}
from
'../types'
;
import
{
assertDownloadableUrl
,
blobFromBase64
,
probeDownloadUrl
}
from
'../deliver'
;
import
{
assertDownloadableUrl
,
blobFromBase64
,
probeDownloadUrl
,
upgradeInsecureUrl
}
from
'../deliver'
;
/** 从 URL 路径末段推断文件名(预签名 URL 的 query 不参与)。 */
/** 从 URL 路径末段推断文件名(预签名 URL 的 query 不参与)。 */
function
fileNameFromUrl
(
url
:
string
):
string
{
function
fileNameFromUrl
(
url
:
string
):
string
{
...
@@ -27,12 +27,17 @@ export const urlHandler: DownloadHandler = {
...
@@ -27,12 +27,17 @@ 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'
);
const
fileName
=
task
.
input
.
fileName
??
fileNameFromUrl
(
payload
.
url
);
// 后端历史数据里还有明文 http 直链(典型是付款回单
// http://qcimg01.glsx.com.cn:7060/group1/...)。主壳是 HTTPS,原样交给浏览器
// 必被 Mixed Content 拦死,过去只能整条链路报失败。入口统一升级协议并去掉
// 原 HTTP 业务端口,业务页(finance 列表页 / 详情抽屉)不用各自处理。
const
url
=
upgradeInsecureUrl
(
payload
.
url
);
const
fileName
=
task
.
input
.
fileName
??
fileNameFromUrl
(
url
);
// 先 fetch 探测:同源 / 带 CORS 的直链能读到 HTTP 状态与响应体,
// 先 fetch 探测:同源 / 带 CORS 的直链能读到 HTTP 状态与响应体,
// 过去 iframe 读不到的「请求资源不存在」(404 或 200+JSON 业务错误)
// 过去 iframe 读不到的「请求资源不存在」(404 或 200+JSON 业务错误)
// 现在会走 error 分支抛错,由引擎落 failed 并触发失败通知。
// 现在会走 error 分支抛错,由引擎落 failed 并触发失败通知。
const
probe
=
await
probeDownloadUrl
(
payload
.
url
,
fileName
,
ctx
.
signal
);
const
probe
=
await
probeDownloadUrl
(
url
,
fileName
,
ctx
.
signal
);
// 探测期间任务可能被取消(signal abort 会让 probe 返回 opaque 而非抛错)。
// 探测期间任务可能被取消(signal abort 会让 probe 返回 opaque 而非抛错)。
// 这里主动抛 AbortError,交回引擎按「已取消」落状态,避免取消后仍继续下载。
// 这里主动抛 AbortError,交回引擎按「已取消」落状态,避免取消后仍继续下载。
if
(
ctx
.
isCanceled
())
throw
new
DOMException
(
'已取消'
,
'AbortError'
);
if
(
ctx
.
isCanceled
())
throw
new
DOMException
(
'已取消'
,
'AbortError'
);
...
@@ -44,7 +49,7 @@ export const urlHandler: DownloadHandler = {
...
@@ -44,7 +49,7 @@ export const urlHandler: DownloadHandler = {
ctx
.
deliver
.
saveBlob
(
probe
.
blob
,
probe
.
fileName
);
ctx
.
deliver
.
saveBlob
(
probe
.
blob
,
probe
.
fileName
);
}
}
return
{
return
{
fileUrl
:
payload
.
url
,
fileUrl
:
url
,
fileName
:
probe
.
fileName
,
fileName
:
probe
.
fileName
,
bytes
:
probe
.
bytes
,
bytes
:
probe
.
bytes
,
note
:
'下载完成'
,
note
:
'下载完成'
,
...
@@ -53,11 +58,11 @@ export const urlHandler: DownloadHandler = {
...
@@ -53,11 +58,11 @@ export const urlHandler: DownloadHandler = {
// opaque:跨域无 CORS 的 OSS 预签名地址,无法探测,退回原 iframe 行为。
// opaque:跨域无 CORS 的 OSS 预签名地址,无法探测,退回原 iframe 行为。
// 这类地址的加载失败浏览器不让 JS 感知,是既定限制,不能因此误判为失败。
// 这类地址的加载失败浏览器不让 JS 感知,是既定限制,不能因此误判为失败。
assertDownloadableUrl
(
payload
.
url
);
assertDownloadableUrl
(
url
);
if
(
task
.
input
.
autoDownload
!==
false
)
{
if
(
task
.
input
.
autoDownload
!==
false
)
{
ctx
.
deliver
.
openUrl
(
payload
.
url
,
payload
.
openInNewTab
??
true
);
ctx
.
deliver
.
openUrl
(
url
,
payload
.
openInNewTab
??
true
);
}
}
return
{
fileUrl
:
payload
.
url
,
fileName
,
note
:
'已打开下载地址'
};
return
{
fileUrl
:
url
,
fileName
,
note
:
'已打开下载地址'
};
},
},
};
};
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment