mirror of https://github.com/halo-dev/halo-admin
Add service util
parent
d2efcd9ee7
commit
bc017a30da
|
@ -0,0 +1,5 @@
|
||||||
|
import service from '@/utils/service'
|
||||||
|
|
||||||
|
const postApi = {}
|
||||||
|
|
||||||
|
export default postApi
|
|
@ -1,19 +0,0 @@
|
||||||
/**
|
|
||||||
* 弃用
|
|
||||||
*/
|
|
||||||
import { setStore, getStore, clearStore } from '@/utils/storage'
|
|
||||||
|
|
||||||
export const TokenKey = 'Access-Token'
|
|
||||||
|
|
||||||
export function getToken () {
|
|
||||||
return getStore(TokenKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function setToken (token) {
|
|
||||||
// key, token, timeout = 86400s
|
|
||||||
return setStore(TokenKey, token, 86400)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function removeToken () {
|
|
||||||
return clearStore(TokenKey)
|
|
||||||
}
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
import axios from 'axios'
|
||||||
|
import NProgress from 'nprogress'
|
||||||
|
import Vue from 'vue'
|
||||||
|
|
||||||
|
const service = axios.create({
|
||||||
|
baseURL:
|
||||||
|
process.env.NODE_ENV === 'production'
|
||||||
|
? 'https://ryanc.cc/'
|
||||||
|
: 'http://localhost:8090'
|
||||||
|
})
|
||||||
|
|
||||||
|
service.interceptors.request.use(
|
||||||
|
config => {
|
||||||
|
NProgress.start()
|
||||||
|
// TODO set token
|
||||||
|
return config
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
NProgress.remove()
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
service.interceptors.response.use(
|
||||||
|
response => {
|
||||||
|
NProgress.done()
|
||||||
|
return response
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
Vue.$log.error('Response failed', error)
|
||||||
|
NProgress.done()
|
||||||
|
|
||||||
|
const response = error.response
|
||||||
|
const status = response ? response.status : -1
|
||||||
|
Vue.$log.error('Server response status', status)
|
||||||
|
|
||||||
|
const data = response.data
|
||||||
|
if (data) {
|
||||||
|
// Business response
|
||||||
|
Vue.$log.error('Business response status', data.status)
|
||||||
|
if (data.status === 400) {
|
||||||
|
// TODO handle 400 status error
|
||||||
|
} else if (data.status === 401) {
|
||||||
|
// TODO handle 401 status error
|
||||||
|
} else if (data.status === 403) {
|
||||||
|
// TODO handle 403 status error
|
||||||
|
} else if (data.status === 404) {
|
||||||
|
// TODO handle 404 status error
|
||||||
|
} else if (data.status === 500) {
|
||||||
|
// TODO handle 500 status error
|
||||||
|
}
|
||||||
|
|
||||||
|
Vue.$message.error(data.message)
|
||||||
|
} else {
|
||||||
|
Vue.$message.error('Unkown reason')
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export default service
|
Loading…
Reference in New Issue