You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dianshang-qianduan/src/utils/trainingReviewPreview.js

64 lines
2.3 KiB
JavaScript

const PREVIEW_STORAGE_PREFIX = "teacher-task-review-preview:";
function currentLocation() {
return typeof window === "undefined" ? null : window.location;
}
function previewKeyFromLocation() {
const location = currentLocation();
if (!location) return "";
const directKey = new URLSearchParams(location.search).get("reviewPreviewKey");
if (directKey) return directKey;
const hashQueryIndex = location.hash.indexOf("?");
if (hashQueryIndex < 0) return "";
return new URLSearchParams(location.hash.slice(hashQueryIndex + 1)).get("reviewPreviewKey") || "";
}
export function getTrainingReviewPreviewKey() {
return previewKeyFromLocation();
}
export function writeTrainingReviewPreview(previewKey, value) {
if (!previewKey || typeof sessionStorage === "undefined") return;
sessionStorage.setItem(`${PREVIEW_STORAGE_PREFIX}${previewKey}`, JSON.stringify(value));
}
export function readTrainingReviewPreview(previewKey) {
if (!previewKey || typeof sessionStorage === "undefined") return null;
try {
return JSON.parse(sessionStorage.getItem(`${PREVIEW_STORAGE_PREFIX}${previewKey}`) || "null");
} catch (_) {
return null;
}
}
export function clearTrainingReviewPreview(previewKey) {
if (previewKey && typeof sessionStorage !== "undefined") {
sessionStorage.removeItem(`${PREVIEW_STORAGE_PREFIX}${previewKey}`);
}
}
export function getTrainingReviewPreviewContext() {
return readTrainingReviewPreview(getTrainingReviewPreviewKey());
}
/*
* 兜底识别「当前页面是评阅预览 iframe」:不依赖 reviewPreviewKey 是否还能解析出
* 有效上下文。key 同步一旦失效,取作答/只读守卫仍能凭路由特征兜住,绝不真发请求。
*/
export function isReviewPreviewHost() {
if (typeof window === "undefined") return false;
return String(window.location.hash || "").includes("task-review-preview");
}
export function isTrainingReviewPreviewRequest(config = {}) {
const method = String(config.method || "get").toLowerCase();
const url = String(config.url || "").split("?")[0];
const inPreview = Boolean(getTrainingReviewPreviewKey()) || isReviewPreviewHost();
return inPreview
&& ["post", "put", "patch", "delete"].includes(method)
&& (url.startsWith("/api/student-training-answers/")
|| url.startsWith("/api/student/project-study-time/")
|| url === "/common/upload");
}