'use strict' import Vue from 'vue' import axios from 'axios' // import { getEnv } from '@/util/util' import {getTime, getUid} from "@/unit/fn"; import {MessageBox} from "element-ui"; const ajax = axios.create({ timeout: 8000, transformResponse: [transformResponse], // headers: { // 'Content-Type': 'application/json', // 'X-Requested-With': 'XMLHttpRequest' // }, responseType: 'json' }); function transformResponse(res) { const data = JSON.parse(res) if (! Prototype.$message.error(' no message returned ') // throw new error(' data.code ') return} if ((data.code && data.code ===) "0") || data.success) { return data } else { if (data.data.code === 401) { return logout() } Vue. Prototype. $message. Error (data. MSG) throw new error (' interface return error ')}} ajax. The interceptors. Request. Use (function (config) { If (config.url.indexof ('/ DMS/API /v1') > -1) {config.data = {des: 'data-mark-server', SRC: 'data-mark-front', param: config.data, time: getTime(), msgId: getUid(16), Adding token if}} / / request header (window. LocalStorage. The getItem (" token ")) {config. Headers. Authorization = window.localStorage.getItem('token') } return config }, Function (error) {return Promise. Reject (error)}) ajax. The interceptors. Response. Use (function (res) {/ / here can make unified to return data processing if(res.config.url.indexOf('/dms/api/v1') > -1) { if (res.data.statusCode === 200) { return res.data.content } else { MessageBox. Alert (res) data. StatusDetail. ErrorMsg. Join (', '), 'tip' {showClose: false}) return Promise.reject(res.data.statusCode) } } return res.data }, function(error) { return Promise.reject(error) } ) const logout = () => { // const env = getEnv() window.localStorage.clear() window.location.replace('/extraction') } export default ajaxCopy the code
Write at the end:
1. After using transformResponse, use json.parse (res) to convert to an object
2. The interface of the returned data execution sequence: 1. 2 transformResponse interceptors. Response. Use