|
- import axios from 'axios'
-
- import { Message, Notification } from 'element-ui' // 这里使用了element-ui的消息提示方法,也可自行定义
-
- axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
- axios.defaults.headers.post['Content-Type'] = 'text/plain;charset=UTF-8'
-
- let service = axios.create({
- baseURL: 'https://open.izhyin.com',//域名信息-测试
- timeout: 10000
- })
-
- // 请求拦截 可在请求头中加入token等
- service.interceptors.request.use(config => {
-
- return config
- }, error => {
- return Promise.reject(error)
- })
-
- // 响应拦截 对响应消息作初步的处理
- service.interceptors.response.use(resp => {
- if (resp.status != 200) {
- Message({
- type: 'error',
- message: resp.data.msg,
- duration: 5000
- })
- }
- return { status: resp.data.status, data: resp.data.result, msg: resp.data.message }
-
- }, error => {
-
- if (error.response) {
- switch (error.response.status) {
- case 400: {
- if (error.response && error.response.data && error.response.data.message) {
- Notification.error({
- title: '400错误',
- message: error.response.data.msg,
- duration: 5000,
- closable: true
- })
- }
- break
- }
- case 403: {
- Notification.error({
- title: '403错误',
- message: error.response.data.msg,
- duration: 5000,
- closable: true
- })
- }
- }
- }
- })
-
- export default service
|