移动端:增加测试批次模式隔离
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import config from "@/config";
|
||||
import { getToken } from "@/utils/auth";
|
||||
import errorCode from "@/utils/errorCode";
|
||||
import constant from "@/utils/constant";
|
||||
import storage from "@/utils/storage";
|
||||
import { useUserStore } from "@/store/modules/user";
|
||||
import { toast, showConfirm, tansParams } from "@/utils/common";
|
||||
import {
|
||||
@@ -14,6 +16,37 @@ import {
|
||||
|
||||
let timeout = 10000;
|
||||
const baseUrl = config.baseUrl;
|
||||
let testBatchRecoveryStarted = false;
|
||||
|
||||
function getResponseBody(payload) {
|
||||
return payload?.data && typeof payload.data === "object" ? payload.data : payload || {};
|
||||
}
|
||||
|
||||
function getResponseData(payload) {
|
||||
const body = getResponseBody(payload);
|
||||
return body.data && typeof body.data === "object" ? body.data : body;
|
||||
}
|
||||
|
||||
function isTestBatchUnavailable(payload) {
|
||||
return getResponseData(payload)?.reason === "TEST_BATCH_UNAVAILABLE";
|
||||
}
|
||||
|
||||
function recoverUnavailableTestBatch(payload) {
|
||||
if (testBatchRecoveryStarted) return;
|
||||
testBatchRecoveryStarted = true;
|
||||
const body = getResponseBody(payload);
|
||||
const message = body.msg || "当前测试批次不可用,已切回正式数据模式";
|
||||
storage.remove(constant.testBatchId);
|
||||
storage.remove(constant.testBatchName);
|
||||
if (typeof uni.$emit === "function") {
|
||||
uni.$emit("test-batch-unavailable", message);
|
||||
}
|
||||
uni.showToast({ title: message, icon: "none", duration: 3500 });
|
||||
uni.reLaunch({ url: "/pages/work/index" });
|
||||
setTimeout(() => {
|
||||
testBatchRecoveryStarted = false;
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
const request = async (config) => {
|
||||
// 是否需要设置 token
|
||||
@@ -23,6 +56,10 @@ const request = async (config) => {
|
||||
if (getToken() && !isToken) {
|
||||
config.header["Authorization"] = "Bearer " + getToken();
|
||||
}
|
||||
const testBatchId = storage.get(constant.testBatchId);
|
||||
if (getToken() && testBatchId) {
|
||||
config.header["X-Test-Batch-Id"] = String(testBatchId);
|
||||
}
|
||||
|
||||
try {
|
||||
config = await encryptTransportRequest(config);
|
||||
@@ -55,8 +92,17 @@ const request = async (config) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const code = res.data.code || 200;
|
||||
const msg = errorCode[code] || res.data.msg || errorCode["default"];
|
||||
if (isTestBatchUnavailable(res)) {
|
||||
recoverUnavailableTestBatch(res);
|
||||
const error = new Error(getResponseBody(res).msg || "当前测试批次不可用");
|
||||
error.response = res;
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
const body = getResponseBody(res);
|
||||
const code = body.code || 200;
|
||||
const msg = errorCode[code] || body.msg || errorCode["default"];
|
||||
if (code === 401) {
|
||||
showConfirm(
|
||||
"登录状态已过期,您可以继续留在该页面,或者重新登录?",
|
||||
@@ -81,7 +127,7 @@ const request = async (config) => {
|
||||
error.response = res;
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(res.data);
|
||||
resolve(body);
|
||||
}
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
@@ -100,10 +146,15 @@ const request = async (config) => {
|
||||
return request(config);
|
||||
}
|
||||
|
||||
if (isTestBatchUnavailable(error?.response)) {
|
||||
recoverUnavailableTestBatch(error.response);
|
||||
}
|
||||
|
||||
const response = error.response;
|
||||
const responseStatus = response?.status ?? response?.statusCode;
|
||||
const responseCode = response?.data?.code;
|
||||
const responseMsg = response?.data?.msg;
|
||||
const responseBody = getResponseBody(response);
|
||||
const responseCode = responseBody?.code;
|
||||
const responseMsg = responseBody?.msg;
|
||||
if (responseMsg) {
|
||||
uni.showToast({
|
||||
title: responseMsg,
|
||||
|
||||
Reference in New Issue
Block a user