/*eslint-disable*/ import axios, { AxiosRequestConfig } from 'axios' import router from '@/router' import store from '@/store' const service: any = axios.create({ // @ts-ignore baseURL: window.globalConfig?.baseUrl, timeout: 15000, responseType: 'json', withCredentials: true }) // http request 拦截器 service.interceptors.request.use( (config: any) => { if (window.globalConfig.baseUrl !== config.baseURL && !config.customBaseUrl) { config.baseURL = window.globalConfig.baseUrl } if (!config.data) { config.data = {} } if (config.method == 'get') { config.params = config.data } if (!config.hasOwnProperty('showError')) { config.showError = true } if (config.loading) { config.loadingText ? window.$message.loading(config.loadingText, 0) : window.$message.loading('', 0) } const token = store.state.accesstoken ? `Bearer ${store.state.accesstoken}` : '' token && (config.headers.Authorization = token) // @ts-ignore if (!window.globalConfig.openTenant) { config.headers.tenantId = 0 } else { const tenantId = store.state.userInfo.tenantId || '' tenantId && (config.headers.tenantId = tenantId) } config.headers.requestId = new Date().getTime() config.headers.ysNetType = window.globalConfig.manually ? 1 : 0 return config }, error => { return Promise.reject(error) } ) // http response 拦截器 service.interceptors.response.use( (res: any): any => { if (res.config.blobDownLoad) { const data = res const blob = new Blob([data.data], { type: res.config.blobType ? res.config.blobType : 'application/vnd.ms-excel' }); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.setAttribute('style', 'display:none'); a.setAttribute('href', objectUrl); a.setAttribute('download', res.config.fileName.includes('.') ? '重命名文件' : res.config.fileName); a.click(); URL.revokeObjectURL(objectUrl); return } if (res) { !res.config.messDestroy && window.$message.destroy() if (res.data.code == 401) { noLogin() } else if (res.data.code == 403) { noPermission() } else if (res.data.code === 0) { return res.data } else if (res.data.code == 201) { return res.data } else { if (res.config.showError) { const timer = setTimeout(() => { window.$message.error(res.data.msg) clearTimeout(timer) }, 600) } return res.data } } }, error => { window.$message.destroy() if (error.response) { if (error.response.status == 401) { noLogin() } else if (error.response.status == 403) { noPermission() } else if (error.response.status == 500) { window.$message.error('服务器异常,请稍后再试') } else { window.$message.error(error.response.data.msg) } } else { window.$message.error('网络错误,请联系管理员') return {} } } ) function noLogin() { store.commit('ACCESSTOKEN', { accesstoken: '' }) if (window.globalConfig.isYSKT) { location.href = window.globalConfig.ysktOption.loginUrl } else { if (window.__MICRO_APP_ENVIRONMENT__) { window.microApp.dispatch({ code: 401}) } else { location.href = window.globalConfig.micLoginUrl + `/#/redirect/login?redirect=${encodeURIComponent(location.href)}&from=ownµapp=1` } } } function noPermission() { window.$message.error('暂无权限') } export default service