init
This commit is contained in:
219
src/utils/http.js
Normal file
219
src/utils/http.js
Normal file
@@ -0,0 +1,219 @@
|
||||
import axios from 'axios'
|
||||
import { formatDate } from './tools'
|
||||
|
||||
class Http {
|
||||
constructor() {
|
||||
this.config = {
|
||||
apiUrl: '',
|
||||
timeout: 300000
|
||||
}
|
||||
this.store = null
|
||||
}
|
||||
|
||||
init(config, store) {
|
||||
this.config = { ...this.config, ...config }
|
||||
this.store = store
|
||||
}
|
||||
|
||||
baseUrl() {
|
||||
return this.config.apiUrl
|
||||
}
|
||||
|
||||
ImgSrc(src) {
|
||||
return this.baseUrl() + src
|
||||
}
|
||||
|
||||
getHttpInstance(config) {
|
||||
let defaultConfig = {
|
||||
timeout: this.config.timeout,
|
||||
headers: {},
|
||||
baseURL: this.baseUrl(),
|
||||
responseType: 'json',
|
||||
transformResponse: [
|
||||
function(data) {
|
||||
return data
|
||||
}
|
||||
]
|
||||
}
|
||||
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', '')
|
||||
}
|
||||
if (window.rootVue && window.rootVue.$router) {
|
||||
window.rootVue.$router.push({ name: '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) {
|
||||
if (window.rootVue && window.rootVue.$Message) {
|
||||
window.rootVue.$Message.error({ content: msg, duration: 3 })
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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)
|
||||
const filename = res.headers.filename
|
||||
const a = document.createElement('a')
|
||||
const href = window.URL.createObjectURL(res.data)
|
||||
a.href = href
|
||||
a.download = filename
|
||||
a.click()
|
||||
window.URL.revokeObjectURL(url)
|
||||
}
|
||||
}
|
||||
|
||||
const http = new Http()
|
||||
|
||||
export default http
|
||||
|
||||
Reference in New Issue
Block a user