安装
利用npm安装npm install axios --save
封装
request.js:
import axios from 'axios'
import { Message } from 'element-ui'
import store from '../vuex/index'
const service = new axios.create({
timeout: 20000
})
service.interceptors.request.use(
config => {
if (config.url.indexOf('token=') === -1) {
config.url = `${config.url}?token=${window.sessionStorage.token.replace(/"/g, '')}`
}
return config
},
error => {
window.console.log(error)
Promise.reject(error)
}
)
service.interceptors.response.use(
response => {
if (
response.request.responseType === 'blob'
) {
return response
}
const res = response.data
if (res.baseRes) {
res.code = res.baseRes.code
res.desc = res.baseRes.desc
}
if (res.code !== 100) {
store.commit('updateLoading', false)
Message({
message: res.desc,
type: 'error',
duration: 3 * 1000
})
if (res.code === 919 || res.code === 918) {
setTimeout(() => {
Message({
message: response.data.baseRes.desc,
type: 'error'
})
window.location.href = 'xxx'
}, 3000)
} else {
return response.data
}
} else {
return response.data
}
},
error => {
store.commit('updateLoading', false)
Message({
message: error.message,
type: 'error',
duration: 3 * 1000
})
return Promise.reject(error)
}
)
export default service
api/common/index.js:
import request from '@/utils/request'
export function getSystemMenu (params) {
return request({
method: 'get',
url: `${window.msBaseUrl}services/systemMenu.json`,
params
})
}
export function getMallCategoryList (params) {
return request({
url: `${window.msNewBaseUrl}config/list`,
method: 'post',
data: params
})
}
页面使用:
import { getSystemMenu } from '@/api/common/index'
const params = {
skuId: this.formData.skuId,
platForm: 1
}
queryProdSkuId(params)
.then(
res => {
if (res.baseRes.code == 100) {
let data = res.baseRes.result
} else {
this.formData.skuId = null
}
},
err => {
}
)
.catch(err => {
})
|