import store from './store/store' import { getHashQuery } from './utils' axios.defaults.timeout = config.timeout // 响应时间 axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded' axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? '/api' : config.baseURL // 配置接口地址 // axios.defaults.baseURL = config.baseURL // 配置接口地址 // axios.defaults.transformRequest = [function (data) { return qs.stringify(data) }] // POST传参序列化(添加请求拦截器) axios.interceptors.request.use((conf) => { if (location.href.indexOf('shareID') > -1) { if (conf.method === 'post') { // let data = qs.parse(conf.data) conf.data = { ...conf.data, shareID: getHashQuery('shareID') } } else if (conf.method === 'get') { conf.params = { ...conf.params, shareID: getHashQuery('shareID') } } } conf.params = { ...conf.params, _t: new Date().getTime() } if (store.state.user && store.state.user.code) { conf.headers.code = store.state.user.code if (conf.headers.noAuth) { delete conf.headers.noAuth } } else { if (process.env.NODE_ENV === 'development') { conf.headers.code = '4E400B1990904F46B649D5F0C00A3F85' } else if (conf.headers.noAuth) { delete conf.headers.noAuth } else { location.href = config.authorizaURL + '?path=' + encodeURIComponent(location.hash.slice(2, location.hash.length)) // 跳授权页 return Promise.reject('no-code') } } // if (['get', 'delete', 'GET', 'DELETE'].indexOf(conf.method) > -1) { // let ret={} // for (let it in conf.params) { // ret[it]= conf.params[it].replace(/\[/g,'%5B').replace(/\]/g,'%5D') // } // conf.params=ret // } return conf }, (error) => { Vue.$vux.toast.show({ text: '参数校验失败', type: 'warn', position: 'middle' }) return Promise.reject(error) }) // 返回状态判断(添加响应拦截器) axios.interceptors.response.use((res) => { // console.log(res.config.url.indexOf('/pay/getGroupItemSheet')=== -1) // 对响应数据做些事 if (res.data.code === 0) { return Promise.resolve(res.data) } else { // console.log(res) let times = 10 if (res.config.url.indexOf('/pay/getGroupItemSheet') === -1 && times > 0) { Vue.$vux.toast.show({ text: res.data.msg || '服务器错误', type: 'warn', position: 'middle' }) return Promise.reject(res) times-- } } }, (error) => { console.log(error) if (error !== 'no-code') { Vue.$vux.toast.show({ text: '服务器错误', type: 'warn', position: 'middle' }) } return Promise.reject(error) }) export function fetchPost (url, params, options = {}) { return new Promise((resolve, reject) => { axios.post(url, params, options) .then(response => { resolve(response) }, err => { reject(err) }) .catch((error) => { reject(error) }) }) } export function upLoadPost (url, params) { return new Promise((resolve, reject) => { axios.post(url, params, { headers: { 'Content-Type': 'multipart/form-data' } }) .then(response => { resolve(response) }, err => { reject(err) }) .catch((error) => { reject(error) }) }) } export function fetchGet (url, param, options) { return new Promise((resolve, reject) => { axios.get(url, { params: param, ...options }) .then(response => { resolve(response) }, err => { reject(err) }) .catch((error) => { reject(error) }) }) } export default { fetchPost, fetchGet }