1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import axios from 'axios'
- import qs from 'qs'
- import { baseURL } from './config.js'
- axios.interceptors.request.use(function (config) {
-
- return config
- }, function (error) {
-
- return Promise.reject(error)
- })
- axios.interceptors.response.use(function (response) {
-
-
- return response.data
- }, function (error) {
-
- return Promise.reject(error)
- })
- function errorState (response) {
-
-
- if (response && (response.status === 200 || response.status === 304 || response.status === 400)) {
-
- return response
- } else {
- alert('数据获取错误')
- }
- }
- function successState (res) {
-
-
- if (res.data.code === '000000') {
- alert('success')
- return res
- }
- }
- function apiAxios (method, url, params) {
- let httpDefault = {
- method: method,
- baseURL: baseURL,
- url: url,
-
-
- params: method === 'GET' || method === 'DELETE' ? params : null,
- data: method === 'POST' || method === 'PUT' ? qs.stringify(params) : null,
- timeout: 10000
- }
-
- return new Promise((resolve, reject) => {
- axios(httpDefault)
-
- .then((res) => {
- successState(res)
- resolve(res)
- }).catch((response) => {
- errorState(response)
- reject(response)
- })
- })
- }
- export default {
- install: function (Vue) {
- Vue.prototype.getAxios = (url, params) => apiAxios('GET', url, params)
- Vue.prototype.postAxios = (url, params) => apiAxios('POST', url, params)
- Vue.prototype.putAxios = (url, params) => apiAxios('PUT', url, params)
- Vue.prototype.delectAxios = (url, params) => apiAxios('DELECT', url, params)
- }
- }
|