全局axios请求全面支持Status Code拦截处理

This commit is contained in:
初志鑫 2020-07-29 20:54:08 +08:00
parent 0a1d6ff66f
commit b28cf91468
4 changed files with 19 additions and 9 deletions

View File

@ -158,6 +158,10 @@ const data = [
content: "主题配置添加海洋之心、绿荫草场、荣耀典藏、暗黑之子模式",
timestamp: "2020-07-18",
},
{
content: "全局axios请求全面支持Status Code拦截处理",
timestamp: "2020-07-29",
},
];
export default [

View File

@ -11,7 +11,7 @@ const mockDir = path.join(process.cwd(), "mock");
* @param app
* @returns {{mockStartIndex: number, mockRoutesLength: number}}
*/
function registerRoutes(app) {
const registerRoutes = (app) => {
let mockLastIndex;
const { default: mocks } = require("./index.js");
const mocksForServer = mocks.map((route) => {
@ -26,7 +26,7 @@ function registerRoutes(app) {
mockRoutesLength: mockRoutesLength,
mockStartIndex: mockLastIndex - mockRoutesLength,
};
}
};
/**
*
@ -40,6 +40,7 @@ const responseFake = (url, type, respond) => {
url: new RegExp(`${baseURL}${url}`),
type: type || "get",
response(req, res) {
res.status(200);
if (JSON.stringify(req.body) !== "{}") {
console.log(chalk.green(`> 请求地址:${req.path}`));
console.log(chalk.green(`> 请求参数:${JSON.stringify(req.body)}\n`));

View File

@ -127,6 +127,10 @@ service.interceptors.response.use(
if (message.includes("Request failed with status code")) {
message = "后端接口" + message.substr(message.length - 3) + "异常";
}
if (error.response) {
const { data } = error.response;
message = data.msg;
}
errorMsg(message || "后端接口未知异常");
return Promise.reject(error);
}

View File

@ -24,26 +24,27 @@ const Webpack = require("webpack");
const WebpackBar = require("webpackbar");
const FileManagerPlugin = require("filemanager-webpack-plugin");
const dayjs = require("dayjs");
const date = new dayjs().format("YYYY_M_D");
const time = new dayjs().format("YYYY-M-D HH:mm:ss");
const date = dayjs().format("YYYY_M_D");
const time = dayjs().format("YYYY-M-D HH:mm:ss");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = ["html", "js", "css", "svg"];
process.env.VUE_APP_TITLE = title || "vue-admin-beautiful";
process.env.VUE_APP_AUTHOR = author || "chuzhixin";
process.env.VUE_APP_UPDATE_TIME = time;
process.env.VUE_APP_VERSION = version;
function resolve(dir) {
return path.join(__dirname, dir);
}
function mockServer() {
const resolve = (dir) => {
return path.join(__dirname, dir);
};
const mockServer = () => {
if (process.env.NODE_ENV === "development") {
const mockServer = require("./mock/mockServer.js");
return mockServer;
} else {
return "";
}
}
};
module.exports = {
publicPath,