109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
|
|
import axios from 'axios'
|
||
|
|
import store from '../store'
|
||
|
|
// 创建axios请求实例
|
||
|
|
// const request = axios.create({
|
||
|
|
// baseURL: axiosBaseUrl,
|
||
|
|
// timeout: 1000,
|
||
|
|
// headers: {
|
||
|
|
// 'Content-Type': 'application/json; charset=utf-8'
|
||
|
|
// }
|
||
|
|
// })
|
||
|
|
axios.defaults.baseURL = typeof axiosBaseUrl == "undefined" ? "" : axiosBaseUrl; // 设置跨域代理接口统一的前置地址
|
||
|
|
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
||
|
|
|
||
|
|
// 添加请求拦截器
|
||
|
|
axios.interceptors.request.use(function (request) {
|
||
|
|
// 在发送请求之前做些什么
|
||
|
|
if(request.method == 'post'){
|
||
|
|
request.params = {};
|
||
|
|
}
|
||
|
|
return request
|
||
|
|
}, function (error) {
|
||
|
|
// 对请求错误做些什么
|
||
|
|
return Promise.reject(error)
|
||
|
|
})
|
||
|
|
|
||
|
|
// 添加响应拦截器
|
||
|
|
axios.interceptors.response.use(function (response) {
|
||
|
|
// 只返回数据
|
||
|
|
return response.data
|
||
|
|
}, function (error) {
|
||
|
|
if(error.response == null){
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
const status = error.response.status
|
||
|
|
if (status >= 500) {
|
||
|
|
alert('服务繁忙请稍后再试')
|
||
|
|
} else if (status >= 400) {
|
||
|
|
alert(error.response.data.message)
|
||
|
|
}
|
||
|
|
// 对响应错误做点什么
|
||
|
|
console.dir(error)
|
||
|
|
return Promise.reject(error)
|
||
|
|
})
|
||
|
|
|
||
|
|
function _toChangeData(data){
|
||
|
|
let dataArray = [];
|
||
|
|
for(let key in data){
|
||
|
|
dataArray.push(encodeURIComponent(key)+"="+encodeURIComponent(data[key]));
|
||
|
|
}
|
||
|
|
return dataArray.join("&");
|
||
|
|
}
|
||
|
|
|
||
|
|
//可能需要更改,暂时用不到
|
||
|
|
const get = (params) => {
|
||
|
|
|
||
|
|
return axios({
|
||
|
|
method: "get",
|
||
|
|
url: params.url,
|
||
|
|
params: params.params
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const post = (params) => {
|
||
|
|
let postConfig = {
|
||
|
|
method: "post",
|
||
|
|
url: params.url,
|
||
|
|
data: params.data
|
||
|
|
}
|
||
|
|
//从vuex中获取sessionId
|
||
|
|
postConfig.data.sid = store.state.sessionId;
|
||
|
|
postConfig.data = _toChangeData(postConfig.data);
|
||
|
|
return axios(postConfig);
|
||
|
|
}
|
||
|
|
|
||
|
|
const aslp = (params) => {
|
||
|
|
// {
|
||
|
|
// portalUrl : "",
|
||
|
|
// authentication : store.state.sessionId,
|
||
|
|
// sourceAppId : "appid",
|
||
|
|
// aslp : "aslp://XXXX",
|
||
|
|
// params :{
|
||
|
|
// aa:1,
|
||
|
|
// bb:2
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
let datas = {
|
||
|
|
authentication : store.state.sessionId,
|
||
|
|
sourceAppId : params.sourceAppId,
|
||
|
|
aslp : params.aslp,
|
||
|
|
}
|
||
|
|
Object.assign(datas,params.params)
|
||
|
|
let postConfig = {
|
||
|
|
method: "post",
|
||
|
|
url: params.portalUrl,
|
||
|
|
params: datas
|
||
|
|
}
|
||
|
|
postConfig.data = _toChangeData(postConfig.data);
|
||
|
|
return axios(postConfig);
|
||
|
|
}
|
||
|
|
// const delete = (url, data) => request.delete(url, data)
|
||
|
|
// const head = (url, data) => request.head(url, data)
|
||
|
|
// const options = (url, data) => request.options(url, data)
|
||
|
|
// const put = (url, data) => request.put(url, data)
|
||
|
|
// const patch = (url, data) => request.patch(url, data)
|
||
|
|
export default {
|
||
|
|
get,
|
||
|
|
post,
|
||
|
|
aslp
|
||
|
|
}
|