import axios from 'axios'
|
// import { MessageBox, Message } from 'element-ui'
|
import { Loading } from 'element-ui'
|
import { getSessionId } from '@/utils/auth'
|
import sign from '@/utils/loginSign';
|
import {showTipOfStatuCode} from './statusCodeManage'
|
|
|
let loading //定义loading变量
|
function startLoading() { //使用Element loading-start 方法
|
loading = Loading.service({
|
lock: true,
|
text: '加载中……',
|
background: 'rgba(0, 0, 0, 0.7)'
|
})
|
}
|
function endLoading() { //使用Element loading-close 方法
|
loading.close()
|
}
|
//那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
|
//声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
|
//调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
|
let needLoadingRequestCount = 0
|
export function showFullScreenLoading() {
|
if (needLoadingRequestCount === 0) {
|
startLoading()
|
}
|
needLoadingRequestCount++
|
}
|
|
export function tryHideFullScreenLoading() {
|
if (needLoadingRequestCount <= 0) return
|
needLoadingRequestCount--
|
if (needLoadingRequestCount === 0) {
|
endLoading()
|
}
|
}
|
|
|
// import StatusCodeManage from './statusCodeManage';
|
// create an axios instance
|
const service = axios.create({
|
baseURL: '/flp',
|
timeout: 300000 // request timeout
|
})
|
|
// request interceptor
|
let configs
|
service.interceptors.request.use(
|
config => {
|
configs=config
|
if (config.method === 'post') {
|
// post 封装数据
|
if (typeof config.data !== 'object') {
|
config.data = {};
|
}
|
} else if (config.method === 'get') {
|
// get 封装数据
|
if (config.params === undefined) {
|
config.params = {};
|
}
|
// config.params['mblNo'] = config.mblNo;
|
}
|
let sessionID = getSessionId();
|
// 验签 用于所有没有登录并传递有电话号码的接口验签
|
if (sessionID === null) {
|
} else {
|
// 参数签名
|
// 必须由http或https开头的请求,才会进行自动签名组装
|
if (config.params) {
|
config.params = sign.signForFormDataOrUrl(
|
config.params,
|
true,
|
sessionID
|
);
|
} else if (config.data) {
|
config.data = sign.signForFormDataOrUrl(
|
config.data,
|
false,
|
sessionID
|
);
|
} else {
|
if (config.method === 'GET') {
|
config.params = sign.signForFormDataOrUrl(
|
config.data,
|
false,
|
sessionID
|
);
|
} else {
|
config.data = sign.signForFormDataOrUrl(
|
config.data,
|
false,
|
sessionID
|
);
|
}
|
}
|
}
|
// ================end ===================
|
// if (store.getters.token) {
|
// // let each request carry token
|
// // ['X-Token'] is a custom headers key
|
// // please modify it according to the actual situation
|
// config.headers['X-Token'] = getSessionId()
|
// }
|
showFullScreenLoading()
|
return config
|
},
|
error => {
|
// do something with request error
|
console.log(error) // for debug
|
tryHideFullScreenLoading()
|
return Promise.reject(error)
|
}
|
)
|
|
// response interceptor
|
service.interceptors.response.use(
|
/**
|
* If you want to get http information such as headers or status
|
* Please return response => response
|
*/
|
|
/**
|
* Determine the request status by custom code
|
* Here is just an example
|
* You can also judge the status by HTTP Status Code
|
*/
|
response => {
|
const res = response.data;
|
tryHideFullScreenLoading()
|
if(configs.method=='get'){
|
return response;
|
}else{
|
return res;
|
}
|
|
},
|
error => {
|
tryHideFullScreenLoading()
|
showTipOfStatuCode(error);
|
return Promise.reject(error);
|
}
|
)
|
|
export default service
|