import axios from 'axios' import { formatDate } from './tools' class Http { constructor() { this.config = {} this.store = null } init(config, store) { this.config = { timeout: 300000, ...config } this.store = store } baseUrl() { return this.config.apiUrl } ImgSrc(src) { return this.baseUrl() + src } getHttpInstance(config) { console.log('getHttpInstance', this.baseUrl()) let defaultConfig = { timeout: this.config.timeout, headers: {}, baseURL: this.baseUrl(), responseType: 'json' // 移除 transformResponse,使用 axios 默认的 JSON 解析 } let newConfig = Object.assign({}, defaultConfig, config) if (this.store && this.store.state.user) { newConfig.headers['admin-token'] = this.store.state.user.token } let instance = axios.create(newConfig) instance.interceptors.request.use( config => { return config }, error => { return Promise.reject(error) } ) instance.interceptors.response.use( response => { if (response.status === 200) { if (response.data && response.data.code === 0) { return response } else { this.hideLoad() let msg = response.data.message this.showError(msg) return Promise.reject(msg) } } else { this.hideLoad() return Promise.reject(response) } }, error => { this.hideLoad() if (error && error.response && error.response.status === 401) { if (this.store) { this.store.commit('user/setToken', '') // 使用 store 中的 router 实例跳转 if (window.framework && window.framework.router) { window.framework.router.push({ path: '/login' }) } } return Promise.reject(error) } let msg = error.message if (msg.indexOf('Network Error') > -1) { msg = '网络错误,请刷新后重试' } else if (msg.indexOf('timeout of') > -1) { msg = '请求超时,请稍后后重试' } this.showError(msg) return Promise.reject(error) } ) return instance } showLoad() { let loadWarp = document.getElementById('spin-box-one') if (loadWarp) { loadWarp.style.display = 'block' } } hideLoad() { let loadWarp = document.getElementById('spin-box-one') if (loadWarp) { loadWarp.style.display = 'none' } } showError(msg) { // 使用框架存储的 ViewUI 实例 if (window.framework && window.framework.ViewUI && window.framework.ViewUI.Message) { window.framework.ViewUI.Message.error({ content: msg, duration: 3 }) } else if (window.$Message) { window.$Message.error({ content: msg, duration: 3 }) } else { console.error(msg) } } formatParamete(param) { param = param || {} if (param) { for (let key in param) { if (param[key] && param[key].getFullYear) { param[key] = formatDate(param[key], 'YYYY-MM-DD HH:mm:ss') } } } param = JSON.parse(JSON.stringify(param)) return param } formatFormDataParam(param) { let formData = new FormData() Object.keys(param).forEach(key => { formData.append(key, param[key]) }) return formData } async get(url, param, config) { let instance = this.getHttpInstance() param = this.formatParamete(param) let promise = new Promise((resolve, reject) => { if (!config || !config.hideLoad) { this.showLoad() } instance.get(url, { params: param }).then(async response => { this.hideLoad() resolve(response.data) }).catch(error => { reject(error) }) }) return promise } async postFormData(url, data) { let instance = this.getHttpInstance({ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) let param = this.formatFormDataParam(data) let promise = new Promise((resolve, reject) => { instance.post(url, param).then(response => { this.hideLoad() resolve(response.data) }).catch(error => { reject(error) }) }) return promise } async post(url, param, config) { let instance = this.getHttpInstance(config) param = this.formatParamete(param) let promise = new Promise((resolve, reject) => { this.showLoad() instance.post(url, param).then(response => { this.hideLoad() resolve(response.data) }).catch(error => { reject(error) }) }) return promise } async fileExport(url, param, filename, is_down = true) { let formData = this.formatFormDataParam(param) let config = { headers: { 'admin-token': this.store ? this.store.state.user.token : '', 'Content-Type': 'application/json' }, baseURL: this.baseUrl(), url: url, responseType: 'blob' } let res = await axios.post(url, formData, config) // 直接下载 if (is_down) { window.framework.uiTool.downloadFile(res.data, filename) } return res } } const http = new Http() export default http