/*
|
* @Descripttion: 配置axios
|
* @Author: TM丶
|
* @LastEditors: zxq
|
* @Date: 2019-03-30 09:07:29
|
* @LastEditTime: 2022-08-02 17:23:10
|
*/
|
import axios from 'axios';
|
import md5 from 'blueimp-md5';
|
import Store from "@/store/index";
|
import router from '@/router/index';
|
import { Notify} from 'vant';
|
import {SET_APP_LOADING,SET_USER_INFO,SET_SESSION_ID} from '@/store/mutations-types';
|
|
|
function createSign(params){
|
if(params.jttechSign){
|
delete params.jttechSign;
|
}
|
let keys = Object.keys(params),
|
paramsSign = '';
|
keys.forEach(key=>{
|
let val = params[key];
|
if(typeof val ==='string'){
|
if(val.trim().length>0){
|
paramsSign +=val;
|
}else{
|
delete params[key]
|
}
|
}else if(typeof val === 'number' && !isNaN(val)){
|
paramsSign +=val;
|
}else if(Array.isArray(val) && !val.length){
|
delete params[key]
|
}else if(val===null){
|
delete params[key]
|
}
|
|
})
|
let sessionId = Store.state.sessionId || '';
|
params.jttechSign = md5(paramsSign + sessionId);
|
return params;
|
}
|
|
|
function deleteCatchInfo(){
|
localStorage.removeItem('user_account')
|
localStorage.removeItem('user_pwd')
|
Store.commit(SET_USER_INFO,{})
|
Store.commit(SET_SESSION_ID,{})
|
}
|
|
// 错误处理
|
const resolveError = {
|
999(message){
|
Notify(message || '网络异常,请稍后重试');
|
},
|
// 90040006
|
99(){
|
deleteCatchInfo()
|
Notify('登录超时,请重新登录!');
|
router.push('/');
|
},
|
90040006(message){
|
deleteCatchInfo()
|
Notify(message || '登录超时,请重新登录!');
|
router.push('/');
|
},
|
// 密码错误
|
9990027(message){
|
Notify(message);
|
deleteCatchInfo()
|
},
|
// 密码错误
|
1340004(message){
|
Notify(message);
|
deleteCatchInfo()
|
router.push('/')
|
}
|
}
|
|
const $http = axios.create({
|
baseURL: '/aic',
|
timeout: 90000,
|
header: {
|
'Content-Type': 'application/json; charset=UTF-8'
|
}
|
})
|
|
// 添加请求拦截器
|
$http.interceptors.request.use(config => {
|
if(typeof config.data ==='undefined'){
|
config.data = {};
|
}
|
config.data = createSign(config.data);
|
|
if(!config.data.apploading){
|
Store.commit(SET_APP_LOADING,true);
|
}
|
|
return config;
|
}, error => {
|
Store.commit(SET_APP_LOADING,false);
|
return Promise.reject(error);
|
})
|
|
// 添加响应拦截器
|
$http.interceptors.response.use(response => {
|
Store.commit(SET_APP_LOADING,false);
|
return response.data;
|
}, error => {
|
Store.commit(SET_APP_LOADING,false);
|
let data = error.response.data;
|
|
let code = Math.abs(data.errorCode || data.status);
|
if(typeof resolveError[code] ==='function'){
|
resolveError[code](data.errMsg || data.message);
|
}else{
|
Notify(data.errMsg || data.message || '系统错误');
|
}
|
return Promise.reject(data);
|
})
|
|
export default $http;
|