添加错误处理

This commit is contained in:
name
2025-09-26 16:37:08 +08:00
parent 74b79b5889
commit f7ec061c25
2 changed files with 134 additions and 122 deletions

View File

@@ -1,23 +1,23 @@
import axios from 'axios' import axios from "axios";
import type { AxiosInstance, AxiosRequestConfig } from 'axios' import type { AxiosInstance, AxiosRequestConfig } from "axios";
class HttpRequest { class HttpRequest {
private readonly baseUrl: string private readonly baseUrl: string;
constructor() { constructor() {
this.baseUrl = '/api' this.baseUrl = "/api";
} }
getInsideConfig() { getInsideConfig() {
const config = { const config = {
baseURL: this.baseUrl, // 所有的请求地址前缀部分(没有后端请求不用写) baseURL: this.baseUrl, // 所有的请求地址前缀部分(没有后端请求不用写)
timeout: 80000 // 请求超时时间(毫秒) timeout: 80000, // 请求超时时间(毫秒)
} };
return config return config;
} }
// 请求拦截 // 请求拦截
interceptors(instance: AxiosInstance, url: string | number | undefined) { interceptors(instance: AxiosInstance, url: string | number | undefined) {
instance.interceptors.request.use( instance.interceptors.request.use(
config => { (config) => {
// 添加全局的loading.. // 添加全局的loading..
// config.headers['Authorization'] = // config.headers['Authorization'] =
// 'bearer ' + JSON.parse(window.localStorage.getItem('adminInfo') || '{}').access_token; // 请求头带上token token要在登录的时候保存在localStorage中 // 'bearer ' + JSON.parse(window.localStorage.getItem('adminInfo') || '{}').access_token; // 请求头带上token token要在登录的时候保存在localStorage中
@@ -27,37 +27,42 @@ class HttpRequest {
// config.headers // config.headers
// ); // );
config.headers['Authorization'] = config.headers["Authorization"] =
'bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5kZXgiOiJmYTM3YjkzY2M5MGQ0YzE3ODRjYThmNmRlYmRkZWUxYSIsInVzZXJfbmFtZSI6InJvb3QiLCJzY29wZSI6WyJhbGwiXSwibmlja25hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJ1c2VyVHlwZSI6MCwiZGVwdEluZGV4IjoiNTY5OWU1OTE2YTE4YTYzODFlMWFjOTJkYTViZDI2MjgiL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ENjdCMDUxLmpwZyJ9.WjeYl1lvvJdDE1FUGIhS99rE5qKaBXOypWxmxK0svWweGqEbu1XCLjKm_YkiTwjZJ_oIcn5JOO9rvHFkkea76BUsYo5wlzuBBiy7sKqM1fFzOFQq6hdFevNTJAbYH9FiBxYxI-e9DZ5mvLGE6umOjUfn_FAsku2w6Uj5DtvpOKBWYzLEPTEifOqNI9he4zJAmVZniUUMf26SDoEdfu0TyrIS1j_qKaEb-cqR1XDhivdthEBK5m9vxJyXFZ5kofNxwQQkit_oiqJRkCZIt9TWAjCh-frzMHCvA30hkAr-VCD2JfCmmEr3hW_lmwfINaPtFVbHCdCKqdrl6VmF1HObaQ";
// 请求头携带token // 请求头携带token
return config return config;
}, },
(error: any) => { (error: any) => {
return Promise.reject(error) return Promise.reject(error);
} }
) );
//响应拦截 //响应拦截
instance.interceptors.response.use( instance.interceptors.response.use(
res => { (res) => {
//返回数据 //返回数据
const { data } = res const { data } = res;
// console.log('返回数据处理', res) // console.log("返回数据处理", res);
return data if (res.data && res.data.code == "A0000") {
return data;
} else {
ElMessage.warning(res.data.message);
}
// return data
}, },
(error: any) => { (error: any) => {
console.log('error==>', error) console.log("error==>", error);
return Promise.reject(error) return Promise.reject(error);
} }
) );
} }
request(options: AxiosRequestConfig) { request(options: AxiosRequestConfig) {
const instance = axios.create() const instance = axios.create();
options = Object.assign(this.getInsideConfig(), options) options = Object.assign(this.getInsideConfig(), options);
this.interceptors(instance, options.url) this.interceptors(instance, options.url);
return instance(options) return instance(options);
} }
} }
const http = new HttpRequest() const http = new HttpRequest();
export default http export default http;

View File

@@ -677,12 +677,15 @@ const generateMetrics = async () => {
type: form.type, type: form.type,
}) })
.then((res: any) => { .then((res: any) => {
if (res.code == "A0000") {
tabList.value[activeName.value].dynamicData = res.data.responsibilities; tabList.value[activeName.value].dynamicData = res.data.responsibilities;
let [min, max] = yMethod( let [min, max] = yMethod(
res.data.datas.map((item: any) => item.valueDatas).flat() res.data.datas.map((item: any) => item.valueDatas).flat()
); );
let series: any[] = []; let series: any[] = [];
let time: any[] = res.data.timeDatas.map((item: any) => timeFormat(item)); let time: any[] = res.data.timeDatas.map((item: any) =>
timeFormat(item)
);
res.data.datas.forEach((item: any) => { res.data.datas.forEach((item: any) => {
series.push({ series.push({
name: item.customerName, name: item.customerName,
@@ -748,8 +751,12 @@ const generateMetrics = async () => {
}; };
tabList.value[activeName.value].showDynamic = true; tabList.value[activeName.value].showDynamic = true;
} else {
ElMessage.warning(res.message);
loading1.value = false;
}
}) })
.catch(() => { .catch((error) => {
loading1.value = false; loading1.value = false;
}); });
loading1.value = false; loading1.value = false;