import Vue from 'vue' import axios from 'axios' import router from '@/router' import qs from 'qs' import merge from 'lodash/merge' import { clearLoginInfo } from '@/utils' const http = axios.create({ timeout: 1000 * 90, withCredentials: true, headers: { 'Content-Type': 'application/json; charset=utf-8' } }) /** * 請求攔截 */ http.interceptors.request.use( config => { config.headers['token'] = Vue.cookie.get('token') // 請求頭帶上token return config }, error => { return Promise.reject(error) } ) /** * 響應攔截 */ http.interceptors.response.use( response => { if (response.data && response.data.code === 401) { // 401, token失效 clearLoginInfo() router.push({ name: 'login' }) } return response }, error => { return Promise.reject(error) } ) /** * 請求地址處理 * @param {*} actionName action方法名稱 */ http.adornUrl = actionName => { // 非生產環境 && 開啓代理, 接口前綴統一使用[/proxyApi/]前綴作代理攔截! return ( (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName ) } /** * get請求參數處理 * @param {*} params 參數對象 * @param {*} openDefultParams 是否開啓默認參數? */ http.adornParams = (params = {}, openDefultParams = true) => { var defaults = { t: new Date().getTime() } return openDefultParams ? merge(defaults, params) : params } /** * post請求數據處理 * @param {*} data 數據對象 * @param {*} openDefultdata 是否開啓默認數據? * @param {*} contentType 數據格式 * json: 'application/json; charset=utf-8' * form: 'application/x-www-form-urlencoded; charset=utf-8' */ http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => { var defaults = { t: new Date().getTime() } data = openDefultdata ? merge(defaults, data) : data return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data) } /** * windPost請求 * @param {String} url [請求地址] * @param {Object} params [請求攜帶參數] */ http.windPost = function (url, params) { return new Promise((resolve, reject) => { http .post(http.adornUrl(url), qs.stringify(params)) .then(res => { resolve(res.data) }) .catch(error => { reject(error) }) }) } /** * windJsonPost請求 * @param {String} url [請求地址] * @param {Object} params [請求攜帶參數] */ http.windJsonPost = function (url, params) { return new Promise((resolve, reject) => { http .post(http.adornUrl(url), http.adornParams(params)) .then(res => { resolve(res.data) }) .catch(error => { reject(error) }) }) } /** * windGet請求 * @param {String} url [請求地址] * @param {Object} params [請求攜帶參數] */ http.windGet = function (url, params, fun = null) { let config = {} if (fun !== null) { config = { params: params, responseType: 'blob', onDownloadProgress (progress) { fun(progress) } } } else { config = { params: params } } return new Promise((resolve, reject) => { http .get(http.adornUrl(url), config) .then(res => { resolve(res.data) }) .catch(error => { reject(error) }) }) } http.windGets = function (url, params) { return new Promise((resolve, reject) => { http .get(http.adornUrl(url), { params: params }) .then(res => { resolve(res.data) }) .catch(error => { reject(error) }) }) } /** * windXXPost請求 * @param {String} url [請求地址] * @param {Object} params [請求攜帶參數] */ http.windXXPost = function (url, params) { return new Promise((resolve, reject) => { http({ url: http.adornUrl(url), method: 'post', params: http.adornParams(params) }) .then(res => { resolve(res.data) }) .catch(error => { reject(error) }) }) } /** * postDownload * @param {String} url [請求地址] * @param {Object} params [請求攜帶參數] */ // http.postDownload = function (url, params) { // http.defaults.timeout = // return new Promise((resolve, reject) => { // http // .post(http.adornUrl(url), qs.stringify(params)) // .then(res => { // resolve(res.data) // }) // .catch(error => { // reject(error) // }) // }) // } export default http