|
|
|
import Request from 'luch-request';
|
|
|
|
import store from '@/store';
|
|
|
|
import { AxiosInstance } from 'axios';
|
|
|
|
import { session } from '@/store/modules/session';
|
|
|
|
//import { session } from '@/store/modules/session';
|
|
|
|
import vm from '@/main';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 标识调试状态
|
|
|
|
*/
|
|
|
|
const debug = process.env.NODE_ENV !== 'production';
|
|
|
|
|
|
|
|
const http = new Request({
|
|
|
|
//baseURL: process.env.VUE_APP_BASE_URL,
|
|
|
|
//baseURL: 'http://192.168.3.93:81',
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* 标识加载中状态
|
|
|
|
*/
|
|
|
|
let loading: any;
|
|
|
|
|
|
|
|
http.interceptors.request.use(
|
|
|
|
(config: any) => {
|
|
|
|
//console.log('123456',config)
|
|
|
|
//生产请将注释解开重要重要
|
|
|
|
// if (config.url.includes('/api')) {
|
|
|
|
// config.baseURL = 'http://192.168.202.23:5001';
|
|
|
|
// }else if(config.url.includes('http')) {
|
|
|
|
// //直接使用写死地址
|
|
|
|
// }else {
|
|
|
|
// config.baseURL = 'http://192.168.202.20:9000'
|
|
|
|
// //'http://192.168.202.20:9000' ///测试
|
|
|
|
// //'http://192.168.202.34:30000'; //正式
|
|
|
|
// }
|
|
|
|
// 所有请求默认提示加载中
|
|
|
|
if (!loading) {
|
|
|
|
uni.showLoading({ title: '加载中' });
|
|
|
|
} else {
|
|
|
|
clearTimeout(loading);
|
|
|
|
}
|
|
|
|
// 在请求发送之前做一些处理
|
|
|
|
config.header['Referrer-Policy'] = 'strict-origin-when-cross-origin';
|
|
|
|
config.header['poolName'] = session.PoolName;
|
|
|
|
// config.header['poolName'] = '';
|
|
|
|
try {
|
|
|
|
const token = session.user.access_token; //store.state.session.accessToken;
|
|
|
|
if (token) {
|
|
|
|
// 让每个请求携带token
|
|
|
|
config.header['Authorization'] = 'Bearer ' + token;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// error
|
|
|
|
}
|
|
|
|
if (typeof config.data === 'string' && config.data.constructor === String) {
|
|
|
|
config.header['content-type'] = 'application/x-www-form-urlencoded';
|
|
|
|
}
|
|
|
|
// if (session.lang) {
|
|
|
|
// config.url = config.url + '?lang=' + (session.lang == 'en' ? 'en_US' : 'zh_CN');
|
|
|
|
// }
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
(error: Error) => {
|
|
|
|
// 发送失败
|
|
|
|
console.log(error);
|
|
|
|
Promise.reject(error);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
http.interceptors.response.use(
|
|
|
|
(response: any) => {
|
|
|
|
// 延时关闭加载中提示
|
|
|
|
loading = setTimeout(() => {
|
|
|
|
loading = undefined;
|
|
|
|
uni.hideLoading();
|
|
|
|
}, 500);
|
|
|
|
debug && console.log(response);
|
|
|
|
// dataAxios 是 axios 返回数据中的 data
|
|
|
|
const dataAxios = response.data;
|
|
|
|
// 测试服务接口直接返回结果
|
|
|
|
if (response.config.url === '/wmspda/system/test') {
|
|
|
|
return dataAxios;
|
|
|
|
}
|
|
|
|
// 这个状态码是和后端约定的
|
|
|
|
const { code, status } = dataAxios;
|
|
|
|
// 根据 code 进行判断
|
|
|
|
if ((code === undefined && status === undefined) || status === null) {
|
|
|
|
// 如果没有 code 代表这不是项目后端开发的接口 比如可能是 D2Admin 请求最新版本
|
|
|
|
return dataAxios;
|
|
|
|
} else if (code !== undefined) {
|
|
|
|
// 有 code 代表这是一个后端接口 可以进行进一步的判断
|
|
|
|
switch (code) {
|
|
|
|
case null:
|
|
|
|
return dataAxios;
|
|
|
|
case '1':
|
|
|
|
// code === '1' 代表没有错误
|
|
|
|
return dataAxios;
|
|
|
|
case '2':
|
|
|
|
return dataAxios;
|
|
|
|
case 10:
|
|
|
|
return dataAxios;
|
|
|
|
case 0:
|
|
|
|
return dataAxios;
|
|
|
|
case 200:
|
|
|
|
return dataAxios;
|
|
|
|
case 401:
|
|
|
|
// [ 示例 ] 其它和后台约定的 code
|
|
|
|
store.dispatch('session/logout');
|
|
|
|
uni.setStorageSync('showLoginTips', true);
|
|
|
|
uni.switchTab({ url: '/pages/personal/index' });
|
|
|
|
errorCreate(dataAxios.message, response.config.custom.hideError);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// 不是正确的 code
|
|
|
|
errorCreate(dataAxios.msg || dataAxios.message, response.config.custom.hideError);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (status !== undefined) {
|
|
|
|
// 有 status 代表这是一个后端接口 可以进行进一步的判断
|
|
|
|
switch (status) {
|
|
|
|
case 'S':
|
|
|
|
// code === 'S' or '1' 代表没有错误
|
|
|
|
return dataAxios;
|
|
|
|
default:
|
|
|
|
// 不是正确的 code
|
|
|
|
errorCreate(dataAxios.msg, response.config.custom.hideError);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(error: any) => {
|
|
|
|
// 延时关闭加载中提示
|
|
|
|
loading = setTimeout(() => {
|
|
|
|
loading = undefined;
|
|
|
|
uni.hideLoading();
|
|
|
|
}, 500);
|
|
|
|
if (error?.data?.message) {
|
|
|
|
error.message = error.data.message;
|
|
|
|
} else if (error) {
|
|
|
|
switch (error.statusCode) {
|
|
|
|
/* eslint-disable */
|
|
|
|
case 400:
|
|
|
|
error.message = vm.$t('message.request_Tip1') as string;
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
error.message = vm.$t('message.request_Tip2') as string;
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
error.message = vm.$t('message.request_Tip3') as string;
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
error.message = (vm.$t('message.request_Tip4') as string) + error.config.url;
|
|
|
|
break;
|
|
|
|
case 408:
|
|
|
|
error.message = vm.$t('message.request_Tip5') as string;
|
|
|
|
break;
|
|
|
|
case 500:
|
|
|
|
error.message = vm.$t('message.request_Tip6') as string;
|
|
|
|
break;
|
|
|
|
case 501:
|
|
|
|
error.message = vm.$t('message.request_Tip7') as string;
|
|
|
|
break;
|
|
|
|
case 502:
|
|
|
|
error.message = vm.$t('message.request_Tip8') as string;
|
|
|
|
break;
|
|
|
|
case 503:
|
|
|
|
error.message = vm.$t('message.request_Tip9') as string;
|
|
|
|
break;
|
|
|
|
case 504:
|
|
|
|
error.message = vm.$t('message.request_Tip10') as string;
|
|
|
|
break;
|
|
|
|
case 505:
|
|
|
|
error.message = vm.$t('message.request_Tip11') as string;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errorLog(error, error?.config?.custom?.hideError);
|
|
|
|
return Promise.reject(error);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// 创建一个错误
|
|
|
|
function errorCreate(msg: string, hideError: boolean) {
|
|
|
|
const error = new Error(msg);
|
|
|
|
errorLog(error, hideError);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 记录和显示错误
|
|
|
|
function errorLog(error: Error, hideError: boolean) {
|
|
|
|
// 打印到控制台
|
|
|
|
let content;
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
console.error('>>>>>> Error >>>>>>');
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
if (error.message) {
|
|
|
|
content = error.message;
|
|
|
|
} else {
|
|
|
|
content = '网络请求超时';
|
|
|
|
}
|
|
|
|
// 显示提示
|
|
|
|
hideError ||
|
|
|
|
// uni.showToast({
|
|
|
|
// icon: 'none',
|
|
|
|
// title: error.message,
|
|
|
|
// });
|
|
|
|
|
|
|
|
uni.showModal({
|
|
|
|
title: '',
|
|
|
|
content: content,
|
|
|
|
confirmText: '确定',
|
|
|
|
cancelText: '取消',
|
|
|
|
showCancel: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default http as AxiosInstance;
|