学校搜索

master
lion 7 days ago
parent 5c88ccae72
commit bf20a0c144

@ -184,15 +184,79 @@ export function formatFileSize(size) {
}
}
/**
* 将资源地址转成当前页面协议下的同源路径避免 HTTPS 页面下载被 302 HTTP 后触发 Mixed Content
*/
export function toSameOriginUrl(url) {
if (!url || typeof url !== "string") {
return url;
}
try {
const parsed = new URL(url, window.location.origin);
if (parsed.hostname === window.location.hostname) {
return parsed.pathname + parsed.search + parsed.hash;
}
if (window.location.protocol === "https:" && parsed.protocol === "http:") {
parsed.protocol = "https:";
return parsed.toString();
}
return parsed.toString();
} catch (e) {
if (window.location.protocol === "https:") {
return url.replace(/^http:\/\//i, "https://");
}
return url;
}
}
export function download(url) {
var downloadLink = document.createElement("a");
downloadLink.href = url;
// 将下载链接添加到页面并模拟点击下载
document.body.appendChild(downloadLink);
downloadLink.click();
// 清理下载链接
document.body.removeChild(downloadLink);
if (!url) {
return;
}
const finalUrl = toSameOriginUrl(url);
const filename = decodeURIComponent(
(String(finalUrl).split("?")[0].split("/").pop()) || "download"
);
const trigger = (href, downloadName) => {
const downloadLink = document.createElement("a");
downloadLink.href = href;
if (downloadName) {
downloadLink.setAttribute("download", downloadName);
}
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
};
// 同源文件走 blob 下载,不跟随 http 跳转,避免 Mixed Content 拦截。
if (typeof finalUrl === "string" && finalUrl.startsWith("/")) {
fetch(finalUrl, { credentials: "same-origin", redirect: "follow" })
.then((res) => {
if (!res.ok) {
throw new Error("download failed");
}
const responseUrl = res.url || "";
if (
window.location.protocol === "https:" &&
/^http:\/\//i.test(responseUrl)
) {
throw new Error("insecure redirect");
}
return res.blob();
})
.then((blob) => {
const blobURL = window.URL.createObjectURL(blob);
trigger(blobURL, filename);
window.URL.revokeObjectURL(blobURL);
})
.catch(() => {
trigger(finalUrl, filename);
});
return;
}
trigger(finalUrl, filename);
}
export function paramsSerializer(params) {

@ -23,6 +23,21 @@
>新增</el-button
>
<template v-if="isHasAuth('search')">
<el-select
v-model="select['filter[1][value]']"
size="small"
style="width: 220px"
clearable
filterable
placeholder="学校.."
>
<el-option
v-for="item in school"
:key="item.id"
:label="item.name"
:value="item.id"
></el-option>
</el-select>
<el-date-picker
type="year"
value-format="yyyy"
@ -360,6 +375,9 @@ export default {
"filter[0][key]": "year",
"filter[0][op]": "eq",
"filter[0][value]": "",
"filter[1][key]": "school_id",
"filter[1][op]": "eq",
"filter[1][value]": "",
},
total: 0,
allAlign: null,
@ -499,7 +517,7 @@ export default {
const res = await schoolIndex(
{
page: 1,
page_size: 999,
page_size: 9999,
},
false
);

@ -24,6 +24,7 @@
<el-select
v-model="form['school_id']"
clearable
filterable
placeholder="请填写学校"
style="width: 100%"
>

Loading…
Cancel
Save