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

77 lines
3.0 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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());
}
/*
* 被预览的学生是否「已完成作答」。
* 评阅列表里 scorable 就是后端对「有作答、可评分」的判定(未完成的学生显示为
* 「未完成」且不给评阅按钮);再叠加评分字段做兜底:一旦已经有 AI 或老师评分,
* 说明学生确实交过卷。只有真正没答题的学生才返回 false。
*/
export function isPreviewAnswerSubmitted(answer) {
if (!answer || typeof answer !== "object") return false;
if (answer.scorable === true) return true;
const hasValue = (value) => value !== null && value !== undefined && String(value).trim() !== "";
return hasValue(answer.aiAssessmentScore) || hasValue(answer.teacherScore);
}
/*
* 兜底识别「当前页面是评阅预览 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");
}