|
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
import { ElNotification, ElMessageBox, ElMessage, ElLoading } from "element-plus";
|
|
|
|
|
|
import { getToken } from "@/utils/auth";
|
|
|
|
|
|
import errorCode from "@/utils/errorCode";
|
|
|
|
|
|
import { tansParams, blobValidate } from "@/utils/ruoyi";
|
|
|
|
|
|
import cache from "@/plugins/cache";
|
|
|
|
|
|
import { saveAs } from "file-saver";
|
|
|
|
|
|
import useUserStore from "@/store/modules/user";
|
|
|
|
|
|
import { getTrainingReviewPreviewContext, isPreviewAnswerSubmitted, isTrainingReviewPreviewRequest } from "@/utils/trainingReviewPreview";
|
|
|
|
|
|
|
|
|
|
|
|
let downloadLoadingInstance;
|
|
|
|
|
|
// 是否显示重新登录
|
|
|
|
|
|
export let isRelogin = { show: false };
|
|
|
|
|
|
|
|
|
|
|
|
// 进行中的 axios 请求计数:任务评阅预览页用它判断「接口全部返回」后再放行页面
|
|
|
|
|
|
let pendingRequestCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
export function getPendingRequestCount() {
|
|
|
|
|
|
return pendingRequestCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function settleRequest() {
|
|
|
|
|
|
pendingRequestCount = Math.max(0, pendingRequestCount - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
|
|
|
|
|
|
// 创建axios实例
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
|
// axios中请求配置有baseURL选项,表示请求URL公共部分
|
|
|
|
|
|
baseURL: import.meta.env.VITE_APP_BASE_API,
|
|
|
|
|
|
// baseURL: "https://szyx.sztzjy.com:8042/",
|
|
|
|
|
|
// baseURL:'http://192.168.2.13:9868/',
|
|
|
|
|
|
// 超时
|
|
|
|
|
|
timeout: 100000,
|
|
|
|
|
|
});
|
|
|
|
|
|
// request拦截器
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
|
(config) => {
|
|
|
|
|
|
if (isTrainingReviewPreviewRequest(config)) {
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 评阅预览内的写请求一律不出网(学生数据零风险),但不能把错误直接抛给页面:
|
|
|
|
|
|
* 部分学生页「下一步」前会先 persist(步骤校验 + 保存进度),保存被拒会让
|
|
|
|
|
|
* 步骤永远切不动。这里用标记对象拒绝,由响应错误拦截器转换成「已跳过」的
|
|
|
|
|
|
* 假成功返回值让页面流程走完;步骤校验类请求额外返回 valid:true 与说明文案。
|
|
|
|
|
|
*/
|
|
|
|
|
|
const error = new Error("任务评阅预览为只读模式,已跳过该操作");
|
|
|
|
|
|
error.previewWriteSkipped = true;
|
|
|
|
|
|
error.previewSkipValidation = /\/step-\d+\/(validate|check|analyze)\/?$/i.test(String(config.url || "").split("?")[0]);
|
|
|
|
|
|
error.config = config;
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 是否需要设置 token
|
|
|
|
|
|
const isToken = (config.headers || {}).isToken === false;
|
|
|
|
|
|
// 是否需要防止数据重复提交
|
|
|
|
|
|
const isRepeatSubmit = (config.headers || {}).repeatSubmit === false;
|
|
|
|
|
|
// 外部服务(绝对地址,如 AI 服务)不携带本系统 token:
|
|
|
|
|
|
// 第三方按自己的 token 体系校验 Authorization,带上本系统 JWT 反而会被判 401
|
|
|
|
|
|
const isExternalUrl = /^https?:\/\//i.test(config.url);
|
|
|
|
|
|
if (getToken() && !isToken && !isExternalUrl) {
|
|
|
|
|
|
config.headers["Authorization"] = "Bearer " + getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
|
|
|
|
|
|
}
|
|
|
|
|
|
// get请求映射params参数
|
|
|
|
|
|
if (config.method === "get" && config.params) {
|
|
|
|
|
|
let url = config.url + "?" + tansParams(config.params);
|
|
|
|
|
|
url = url.slice(0, -1);
|
|
|
|
|
|
config.params = {};
|
|
|
|
|
|
config.url = url;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!isRepeatSubmit && (config.method === "post" || config.method === "put")) {
|
|
|
|
|
|
const requestObj = {
|
|
|
|
|
|
url: config.url,
|
|
|
|
|
|
data: typeof config.data === "object" ? JSON.stringify(config.data) : config.data,
|
|
|
|
|
|
time: new Date().getTime(),
|
|
|
|
|
|
};
|
|
|
|
|
|
const sessionObj = cache.session.getJSON("sessionObj");
|
|
|
|
|
|
if (sessionObj === undefined || sessionObj === null || sessionObj === "") {
|
|
|
|
|
|
cache.session.setJSON("sessionObj", requestObj);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
pendingRequestCount++;
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
console.log(error);
|
|
|
|
|
|
Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
|
(res) => {
|
|
|
|
|
|
settleRequest();
|
|
|
|
|
|
// 未设置状态码则默认成功状态
|
|
|
|
|
|
const code = res.data.code || 200;
|
|
|
|
|
|
// console.log(code);
|
|
|
|
|
|
// 获取错误信息
|
|
|
|
|
|
const msg = errorCode[code] || res.data.msg || errorCode["default"];
|
|
|
|
|
|
// 二进制数据则直接返回
|
|
|
|
|
|
if (res.request.responseType === "blob" || res.request.responseType === "arraybuffer") {
|
|
|
|
|
|
return res.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (code === 401) {
|
|
|
|
|
|
if (!isRelogin.show) {
|
|
|
|
|
|
isRelogin.show = true;
|
|
|
|
|
|
ElMessageBox.confirm("登录状态已过期,您可以继续留在该页面,或者重新登录", "系统提示", { confirmButtonText: "重新登录", cancelButtonText: "取消", type: "warning" })
|
|
|
|
|
|
.then(() => {
|
|
|
|
|
|
isRelogin.show = false;
|
|
|
|
|
|
useUserStore()
|
|
|
|
|
|
.logOut()
|
|
|
|
|
|
.then(() => {
|
|
|
|
|
|
location.href = "/index";
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => {
|
|
|
|
|
|
isRelogin.show = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject("无效的会话,或者会话已过期,请重新登录。");
|
|
|
|
|
|
} else if (code === 500) {
|
|
|
|
|
|
ElMessage({ message: msg, type: "error" });
|
|
|
|
|
|
return Promise.reject(new Error(msg));
|
|
|
|
|
|
} else if (code === 601) {
|
|
|
|
|
|
ElMessage({ message: msg, type: "warning" });
|
|
|
|
|
|
return Promise.reject(new Error(msg));
|
|
|
|
|
|
} else if (code !== 200) {
|
|
|
|
|
|
if (!res.config?.skipErrorMessage) {
|
|
|
|
|
|
ElNotification.error({ title: msg });
|
|
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(new Error(msg));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return Promise.resolve(res.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 评阅预览内被跳过的写请求(见请求拦截器注释):不出网、不计入请求计数、
|
|
|
|
|
|
* 不报错,转成与真实响应同构的假成功,页面流程照常。
|
|
|
|
|
|
* 步骤校验返回 valid:true + 说明文案,不向教师展示伪造的校验结论。
|
|
|
|
|
|
*/
|
|
|
|
|
|
if (error?.previewWriteSkipped) {
|
|
|
|
|
|
if (error.previewSkipValidation) {
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 步骤校验:只有「已完成作答(有分数)」的学生才放行步骤切换。
|
|
|
|
|
|
* 未答题的学生本就无内容可看,返回 valid:false 让页面自己的门控把步骤拦住,
|
|
|
|
|
|
* 并把原因写在 message 里,教师看到的是真实原因而不是偷偷放行。
|
|
|
|
|
|
*/
|
|
|
|
|
|
const previewAnswer = getTrainingReviewPreviewContext()?.answer || null;
|
|
|
|
|
|
if (!isPreviewAnswerSubmitted(previewAnswer)) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
code: 200,
|
|
|
|
|
|
msg: "该学生尚未完成作答",
|
|
|
|
|
|
data: { valid: false, message: "该学生尚未完成作答,暂不能切换步骤" },
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
code: 200,
|
|
|
|
|
|
msg: "评阅预览已跳过步骤校验",
|
|
|
|
|
|
data: { valid: true, message: "评阅预览:已跳过步骤校验,可直接查看各步骤作答" },
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
return { code: 200, msg: "评阅预览为只读模式,已跳过保存", data: null };
|
|
|
|
|
|
}
|
|
|
|
|
|
settleRequest();
|
|
|
|
|
|
console.log("err" + error);
|
|
|
|
|
|
let { message } = error;
|
|
|
|
|
|
const serverMessage = String(error?.response?.data?.msg || "").trim();
|
|
|
|
|
|
if (message == "Network Error") {
|
|
|
|
|
|
message = "后端接口连接异常";
|
|
|
|
|
|
} else if (message.includes("timeout")) {
|
|
|
|
|
|
message = "系统接口请求超时";
|
|
|
|
|
|
} else if (message.includes("Request failed with status code")) {
|
|
|
|
|
|
if (message.includes("401")) {
|
|
|
|
|
|
ElMessageBox.confirm("登录状态已过期,您可以继续留在该页面,或者重新登录", "系统提示", { confirmButtonText: "重新登录", cancelButtonText: "取消", type: "warning" })
|
|
|
|
|
|
.then(() => {
|
|
|
|
|
|
isRelogin.show = true;
|
|
|
|
|
|
useUserStore()
|
|
|
|
|
|
.logOut()
|
|
|
|
|
|
.then(() => {
|
|
|
|
|
|
location.href = "/login";
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => {
|
|
|
|
|
|
isRelogin.show = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
} else if (message.includes("code 400")) {
|
|
|
|
|
|
if (!error.config?.skipErrorMessage) {
|
|
|
|
|
|
ElMessage({ message: serverMessage || "请求参数不符合要求", type: "error" });
|
|
|
|
|
|
} else {
|
|
|
|
|
|
error.message = serverMessage || "请求参数不符合要求";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
message = "系统接口" + message.substr(message.length - 3) + "异常";
|
|
|
|
|
|
}
|
|
|
|
|
|
// ElMessage({ message: message, type: 'error', duration: 5 * 1000 })
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 通用下载方法
|
|
|
|
|
|
export function download(url, params, filename, config) {
|
|
|
|
|
|
downloadLoadingInstance = ElLoading.service({ text: "正在下载数据,请稍候", background: "rgba(0, 0, 0, 0.7)" });
|
|
|
|
|
|
return service
|
|
|
|
|
|
.post(url, params, {
|
|
|
|
|
|
transformRequest: [
|
|
|
|
|
|
(params) => {
|
|
|
|
|
|
return tansParams(params);
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
|
|
|
|
responseType: "blob",
|
|
|
|
|
|
...config,
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(async (data) => {
|
|
|
|
|
|
const isBlob = blobValidate(data);
|
|
|
|
|
|
if (isBlob) {
|
|
|
|
|
|
const blob = new Blob([data]);
|
|
|
|
|
|
saveAs(blob, filename);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const resText = await data.text();
|
|
|
|
|
|
const rspObj = JSON.parse(resText);
|
|
|
|
|
|
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
|
|
|
|
|
|
ElMessage.error(errMsg);
|
|
|
|
|
|
}
|
|
|
|
|
|
downloadLoadingInstance.close();
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch((r) => {
|
|
|
|
|
|
console.error(r);
|
|
|
|
|
|
ElMessage.error("下载文件出现错误,请联系管理员!");
|
|
|
|
|
|
downloadLoadingInstance.close();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default service;
|